|
3 | 3 |
|
4 | 4 | import static org.gradlex.javamodule.dependencies.internal.utils.ModuleNamingUtil.sourceSetToModuleName; |
5 | 5 |
|
| 6 | +import com.github.javaparser.JavaParser; |
| 7 | +import com.github.javaparser.ast.CompilationUnit; |
| 8 | +import com.github.javaparser.ast.modules.ModuleDeclaration; |
| 9 | +import com.github.javaparser.ast.modules.ModuleDirective; |
| 10 | +import com.github.javaparser.ast.modules.ModuleRequiresDirective; |
6 | 11 | import java.io.Serializable; |
7 | 12 | import java.util.ArrayList; |
8 | | -import java.util.Arrays; |
9 | 13 | import java.util.Collections; |
10 | 14 | import java.util.List; |
11 | 15 | import java.util.Objects; |
| 16 | +import java.util.Optional; |
12 | 17 | import org.jspecify.annotations.Nullable; |
13 | 18 |
|
14 | 19 | public class ModuleInfo implements Serializable { |
@@ -37,10 +42,45 @@ public String literal() { |
37 | 42 | private final List<String> requiresRuntime = new ArrayList<>(); |
38 | 43 |
|
39 | 44 | public ModuleInfo(String moduleInfoFileContent) { |
40 | | - boolean insideComment = false; |
41 | | - for (String line : moduleInfoFileContent.split("\n")) { |
42 | | - insideComment = parse(line, insideComment); |
| 45 | + Optional<CompilationUnit> result = |
| 46 | + new JavaParser().parse(moduleInfoFileContent).getResult(); |
| 47 | + if (!result.isPresent()) { |
| 48 | + return; |
43 | 49 | } |
| 50 | + if (!result.get().getModule().isPresent()) { |
| 51 | + return; |
| 52 | + } |
| 53 | + ModuleDeclaration moduleDeclaration = result.get().getModule().get(); |
| 54 | + moduleName = moduleDeclaration.getNameAsString(); |
| 55 | + processDirectives(moduleDeclaration.getDirectives()); |
| 56 | + } |
| 57 | + |
| 58 | + private void processDirectives(List<ModuleDirective> directives) { |
| 59 | + for (ModuleDirective d : directives) { |
| 60 | + if (d instanceof ModuleRequiresDirective) { |
| 61 | + ModuleRequiresDirective directive = (ModuleRequiresDirective) d; |
| 62 | + String identifier = directive.getNameAsString(); |
| 63 | + if (directive.isStatic() && directive.isTransitive()) { |
| 64 | + requiresStaticTransitive.add(identifier); |
| 65 | + } else if (directive.isTransitive()) { |
| 66 | + requiresTransitive.add(identifier); |
| 67 | + } else if (directive.isStatic()) { |
| 68 | + requiresStatic.add(identifier); |
| 69 | + } else if (isRuntime(directive)) { |
| 70 | + requiresRuntime.add(identifier); |
| 71 | + } else { |
| 72 | + requires.add(identifier); |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + private static boolean isRuntime(ModuleRequiresDirective directive) { |
| 79 | + return directive |
| 80 | + .getName() |
| 81 | + .getComment() |
| 82 | + .map(c -> "runtime".equals(c.getContent().trim())) |
| 83 | + .orElse(false); |
44 | 84 | } |
45 | 85 |
|
46 | 86 | public String getModuleName() { |
@@ -90,46 +130,6 @@ public String moduleNamePrefix(String projectName, String sourceSetName, boolean |
90 | 130 | return null; |
91 | 131 | } |
92 | 132 |
|
93 | | - /** |
94 | | - * @return true, if we are inside a multi-line comment after this line |
95 | | - */ |
96 | | - private boolean parse(String moduleLine, boolean insideComment) { |
97 | | - if (insideComment) { |
98 | | - return !moduleLine.contains("*/"); |
99 | | - } |
100 | | - |
101 | | - List<String> tokens = Arrays.asList(moduleLine |
102 | | - .replace(";", "") |
103 | | - .replace("{", "") |
104 | | - .replace("}", "") |
105 | | - .replace(RUNTIME_KEYWORD, "runtime") |
106 | | - .replaceAll("/\\*.*?\\*/", " ") |
107 | | - .trim() |
108 | | - .split("\\s+")); |
109 | | - int singleLineCommentStartIndex = tokens.indexOf("//"); |
110 | | - if (singleLineCommentStartIndex >= 0) { |
111 | | - tokens = tokens.subList(0, singleLineCommentStartIndex); |
112 | | - } |
113 | | - |
114 | | - if (tokens.contains("module")) { |
115 | | - moduleName = tokens.get(tokens.size() - 1); |
116 | | - } |
117 | | - if (tokens.size() > 1 && tokens.get(0).equals("requires")) { |
118 | | - if (tokens.size() > 3 && tokens.contains("static") && tokens.contains("transitive")) { |
119 | | - requiresStaticTransitive.add(tokens.get(3)); |
120 | | - } else if (tokens.size() > 2 && tokens.contains("transitive")) { |
121 | | - requiresTransitive.add(tokens.get(2)); |
122 | | - } else if (tokens.size() > 2 && tokens.contains("static")) { |
123 | | - requiresStatic.add(tokens.get(2)); |
124 | | - } else if (tokens.size() > 2 && tokens.contains("runtime")) { |
125 | | - requiresRuntime.add(tokens.get(2)); |
126 | | - } else { |
127 | | - requires.add(tokens.get(1)); |
128 | | - } |
129 | | - } |
130 | | - return moduleLine.lastIndexOf("/*") > moduleLine.lastIndexOf("*/"); |
131 | | - } |
132 | | - |
133 | 133 | @Override |
134 | 134 | public boolean equals(Object o) { |
135 | 135 | if (this == o) return true; |
|
0 commit comments