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.IOException;
23  import java.nio.file.Files;
24  import java.nio.file.Path;
25  import java.util.ArrayList;
26  import java.util.LinkedHashSet;
27  import java.util.List;
28  import java.util.Set;
29  
30  import com.github.javaparser.StaticJavaParser;
31  import com.github.javaparser.ast.expr.Name;
32  import com.github.javaparser.ast.modules.ModuleDeclaration;
33  import com.github.javaparser.ast.modules.ModuleDirective;
34  import com.github.javaparser.ast.modules.ModuleExportsDirective;
35  import com.github.javaparser.ast.modules.ModuleProvidesDirective;
36  import com.github.javaparser.ast.modules.ModuleRequiresDirective;
37  import com.github.javaparser.ast.modules.ModuleUsesDirective;
38  
39  /**
40   * Extracts information from a source module descriptor.
41   *
42   * @author Robert Scholte
43   * @since 1.0.0
44   */
45  class SourceModuleInfoParser {
46  
47      public JavaModuleDescriptor fromSourcePath(Path modulePath) throws IOException {
48          JavaModuleDescriptor.Builder builder;
49          if (Files.exists(modulePath)) {
50              ModuleDeclaration descriptor = StaticJavaParser.parse(modulePath)
51                      .getModule()
52                      .orElseThrow(() -> new IOException("Module declaration not found in " + modulePath));
53  
54              builder = JavaModuleDescriptor.newModule(descriptor.getName().asString());
55  
56              for (ModuleDirective directive : descriptor.getDirectives()) {
57                  if (directive instanceof ModuleRequiresDirective) {
58                      addRequires(builder, (ModuleRequiresDirective) directive);
59                  } else if (directive instanceof ModuleExportsDirective) {
60                      addExports(builder, (ModuleExportsDirective) directive);
61                  } else if (directive instanceof ModuleUsesDirective) {
62                      ModuleUsesDirective uses = (ModuleUsesDirective) directive;
63                      builder.uses(uses.getName().asString());
64                  } else if (directive instanceof ModuleProvidesDirective) {
65                      ModuleProvidesDirective provides = (ModuleProvidesDirective) directive;
66                      List<String> providers = new ArrayList<>(provides.getWith().size());
67                      for (Name provider : provides.getWith()) {
68                          providers.add(provider.asString());
69                      }
70                      builder.provides(provides.getName().asString(), providers);
71                  }
72              }
73          } else {
74              builder = JavaModuleDescriptor.newAutomaticModule(null);
75          }
76  
77          return builder.build();
78      }
79  
80      private static void addRequires(JavaModuleDescriptor.Builder builder, ModuleRequiresDirective requires) {
81          if (requires.isStatic() || requires.isTransitive()) {
82              Set<JavaModuleDescriptor.JavaRequires.JavaModifier> modifiers = new LinkedHashSet<>(2);
83              if (requires.isStatic()) {
84                  modifiers.add(JavaModuleDescriptor.JavaRequires.JavaModifier.STATIC);
85              }
86              if (requires.isTransitive()) {
87                  modifiers.add(JavaModuleDescriptor.JavaRequires.JavaModifier.TRANSITIVE);
88              }
89              builder.requires(modifiers, requires.getName().asString());
90          } else {
91              builder.requires(requires.getName().asString());
92          }
93      }
94  
95      private static void addExports(JavaModuleDescriptor.Builder builder, ModuleExportsDirective exports) {
96          if (exports.getModuleNames().isEmpty()) {
97              builder.exports(exports.getName().asString());
98          } else {
99              Set<String> targets = new LinkedHashSet<>();
100             for (Name module : exports.getModuleNames()) {
101                 targets.add(module.asString());
102             }
103             builder.exports(exports.getName().asString(), targets);
104         }
105     }
106 }