Skip to content

Commit 68bec28

Browse files
committed
Use javaparser.org to parse module-info.java
1 parent 262d9d0 commit 68bec28

File tree

4 files changed

+57
-41
lines changed

4 files changed

+57
-41
lines changed

build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ configurations.compileClasspath {
1111

1212
dependencies {
1313
implementation("org.ow2.asm:asm:9.9")
14+
implementation("com.github.javaparser:javaparser-core:3.27.1")
1415
compileOnly("org.gradlex:extra-java-module-info:1.13.1")
1516
compileOnly("com.autonomousapps:dependency-analysis-gradle-plugin:3.5.1")
1617
}

gradle/verification-keyring.keys

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1754,3 +1754,16 @@ CgkQuhmUn6JgkhZmBQEA3b5QhIg4LhToSGJ0sI3mPr270z+Sefyl/L8s2i7ZJKEA
17541754
/1su4aPLl+FaeuZHpInOy991PXFh+IJICL1irc2DfV4G
17551755
=stfL
17561756
-----END PGP PUBLIC KEY BLOCK-----
1757+
1758+
id 6DE9B8077FBB2F8A019F4904BD17A565509DEE20
1759+
1760+
-----BEGIN PGP PUBLIC KEY BLOCK-----
1761+
1762+
xjMEZl2j9RYJKwYBBAHaRw8BAQdAPzZyNkr92xKYzBrTOsN8Fwy9l2W0ez4Hu0t3
1763+
/MoohtG0H2plYW4tcGllcnJlLmxlcmJzY2hlckBqcGVyZi5jb23OOARmXaP1Egor
1764+
BgEEAZdVAQUBAQdAoTUzjZPhQLyzRo9jIO8TrgC+mfazNL5gB+fOWhB//T4DAQgH
1765+
wn4EGBYKACYWIQRt6bgHf7svigGfSQS9F6VlUJ3uIAUCZl2j9QIbDAUJBaOKqwAK
1766+
CRC9F6VlUJ3uIIpxAP9MihNcqlK2wPp2uURiLmw16dN3o50gxWeLMjBxethg0gD/
1767+
VLRHao8huHsPY9XMrgbHSNLZOT7geBJOdvwrTRTy3A0=
1768+
=BzAO
1769+
-----END PGP PUBLIC KEY BLOCK-----

gradle/verification-metadata.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@
1717
<trust file="asciidoctor-gradle-jvm-4.0.5.jar"/>
1818
</trusted-artifacts>
1919
<trusted-keys>
20-
<!-- PRODUCTION CODE: ASM -->
20+
<!-- PRODUCTION CODE -->
2121
<!-- ✅ Signed by Eric Bruneton - ASM -->
2222
<trusted-key id="A5BD02B93E7A40482EB1D66A5F69AD087600B22C" group="org.ow2.asm" name="asm"/>
23+
<!-- ✅ Signed by Jean Pierre Lerbscher - JAVA PARSER -->
24+
<trusted-key id="6DE9B8077FBB2F8A019F4904BD17A565509DEE20" group="com.github.javaparser" name="javaparser-core"/>
2325
<!-- ✅ Signed by Tony Robalik - DAGP -->
2426
<trusted-key id="CF4B3A3F53BEF9A2CE2CBFB895962C5E716C39AA" group="com.autonomousapps" name="dependency-analysis-gradle-plugin"/>
2527
<!-- ✅ Signed by Square (squareup.com) -->

src/main/java/org/gradlex/javamodule/dependencies/internal/utils/ModuleInfo.java

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,17 @@
33

44
import static org.gradlex.javamodule.dependencies.internal.utils.ModuleNamingUtil.sourceSetToModuleName;
55

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;
611
import java.io.Serializable;
712
import java.util.ArrayList;
8-
import java.util.Arrays;
913
import java.util.Collections;
1014
import java.util.List;
1115
import java.util.Objects;
16+
import java.util.Optional;
1217
import org.jspecify.annotations.Nullable;
1318

1419
public class ModuleInfo implements Serializable {
@@ -37,10 +42,17 @@ public String literal() {
3742
private final List<String> requiresRuntime = new ArrayList<>();
3843

3944
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;
4349
}
50+
if (!result.get().getModule().isPresent()) {
51+
return;
52+
}
53+
ModuleDeclaration moduleDeclaration = result.get().getModule().get();
54+
moduleName = moduleDeclaration.getNameAsString();
55+
processDirectives(moduleDeclaration.getDirectives());
4456
}
4557

4658
public String getModuleName() {
@@ -90,44 +102,32 @@ public String moduleNamePrefix(String projectName, String sourceSetName, boolean
90102
return null;
91103
}
92104

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));
105+
private void processDirectives(List<ModuleDirective> directives) {
106+
for (ModuleDirective d : directives) {
107+
if (d instanceof ModuleRequiresDirective) {
108+
ModuleRequiresDirective directive = (ModuleRequiresDirective) d;
109+
String identifier = directive.getNameAsString();
110+
if (directive.isStatic() && directive.isTransitive()) {
111+
requiresStaticTransitive.add(identifier);
112+
} else if (directive.isTransitive()) {
113+
requiresTransitive.add(identifier);
114+
} else if (directive.isStatic()) {
115+
requiresStatic.add(identifier);
116+
} else if (isRuntime(directive)) {
117+
requiresRuntime.add(identifier);
118+
} else {
119+
requires.add(identifier);
120+
}
128121
}
129122
}
130-
return moduleLine.lastIndexOf("/*") > moduleLine.lastIndexOf("*/");
123+
}
124+
125+
private static boolean isRuntime(ModuleRequiresDirective directive) {
126+
return directive
127+
.getName()
128+
.getComment()
129+
.map(c -> "runtime".equals(c.getContent().trim()))
130+
.orElse(false);
131131
}
132132

133133
@Override

0 commit comments

Comments
 (0)