View Javadoc
1   package org.codehaus.plexus.languages.java.jpms;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.File;
23  import java.io.IOException;
24  import java.nio.file.Path;
25  import java.util.LinkedHashSet;
26  import java.util.Set;
27  
28  import com.thoughtworks.qdox.JavaProjectBuilder;
29  import com.thoughtworks.qdox.model.JavaModule;
30  import com.thoughtworks.qdox.model.JavaModuleDescriptor;
31  
32  /**
33   * Extract information from module with QDox
34   * 
35   * @author Robert Scholte
36   * @since 1.0.0
37   */
38  public class QDoxModuleInfoParser
39  {
40  
41      public org.codehaus.plexus.languages.java.jpms.JavaModuleDescriptor fromSourcePath( Path modulePath )
42                      throws IOException
43      {
44          File moduleDescriptor = modulePath.toFile();
45  
46          org.codehaus.plexus.languages.java.jpms.JavaModuleDescriptor.Builder builder;
47          if ( moduleDescriptor.exists() )
48          {
49              JavaModuleDescriptor descriptor = new JavaProjectBuilder().addSourceFolder( moduleDescriptor.getParentFile() ).getDescriptor();
50  
51              builder = org.codehaus.plexus.languages.java.jpms.JavaModuleDescriptor.newModule( descriptor.getName() );
52              
53              for ( JavaModuleDescriptor.JavaRequires requires : descriptor.getRequires() )
54              {
55                  builder.requires( requires.getModule().getName() );
56              }
57              
58              for ( JavaModuleDescriptor.JavaExports exports : descriptor.getExports() )
59              {
60                  if ( exports.getTargets().isEmpty()  )
61                  {
62                      builder.exports( exports.getSource().getName() );
63                  }
64                  else
65                  {
66                      Set<String> targets = new LinkedHashSet<>();
67                      for ( JavaModule module : exports.getTargets() )
68                      {
69                          targets.add( module.getName() );
70                      }
71                      builder.exports( exports.getSource().getName(), targets );
72                  }
73              }
74          }
75          else
76          {
77              builder = org.codehaus.plexus.languages.java.jpms.JavaModuleDescriptor.newAutomaticModule( null );
78          }
79  
80          return builder.build();
81      }
82  
83  }