Skip to content

Commit 0d58252

Browse files
committed
Improve module-info parsing (ignore comments)
Fixes #5
1 parent 362ec00 commit 0d58252

File tree

3 files changed

+89
-4
lines changed

3 files changed

+89
-4
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Java Module Testing Gradle Plugin - Changelog
22

3+
## Version 1.2
4+
* [#5](https://github.com/gradlex-org/java-module-testing/issues/5) Improve module-info parsing
5+
36
## Version 1.1
47
* Integrate with https://github.com/gradlex-org/java-module-dependencies/issues/19
58

src/main/java/org/gradlex/javamodule/testing/internal/ModuleInfoParser.java

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,31 @@ public String moduleName(Set<File> sourceFolders) {
4747
return null;
4848
}
4949

50-
private String moduleName(String moduleInfoFileContent) {
50+
static String moduleName(String moduleInfoFileContent) {
51+
boolean inComment = false;
52+
boolean moduleKeywordFound = false;
53+
5154
for(String line: moduleInfoFileContent.split("\n")) {
52-
List<String> tokens = Arrays.asList(line.replace("{","").trim().split("\\s+"));
53-
if (!"//".equals(tokens.get(0)) && tokens.contains("module")) {
54-
return tokens.get(tokens.size() - 1);
55+
String cleanedLine = line
56+
.replaceAll("/\\*.*\\*/", "") // open & close in this line
57+
.replaceAll("//.*", ""); // line comment
58+
inComment = inComment || cleanedLine.contains("/*");
59+
cleanedLine = cleanedLine.replaceAll("/\\*.*", ""); // open in this line
60+
inComment = inComment && !line.contains("*/");
61+
cleanedLine = cleanedLine.replaceAll(".*\\*/", "").trim(); // closing part of comment
62+
63+
if (inComment) {
64+
continue;
65+
}
66+
67+
List<String> tokens = Arrays.asList(cleanedLine.split("\\s+"));
68+
if (moduleKeywordFound && !tokens.isEmpty()) {
69+
return tokens.get(0);
70+
} else if (tokens.indexOf("module") == 0) {
71+
moduleKeywordFound = true;
72+
}
73+
if (tokens.size() > 1) {
74+
return tokens.get(1);
5575
}
5676
}
5777
return null;
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package org.gradlex.javamodule.testing.internal
2+
3+
import spock.lang.Specification
4+
5+
class ModuleInfoParseTest extends Specification {
6+
7+
def "ignores single line comments"() {
8+
given:
9+
def nameFromFile = ModuleInfoParser.moduleName('''
10+
// module some.thing.else
11+
module some.thing {
12+
requires transitive foo.bar.la;
13+
}
14+
''')
15+
16+
expect:
17+
nameFromFile == 'some.thing'
18+
}
19+
20+
def "ignores single line comments late in line"() {
21+
given:
22+
def nameFromFile = ModuleInfoParser.moduleName('''
23+
module some.thing { // module some.thing.else
24+
requires transitive foo.bar.la;
25+
}
26+
''')
27+
28+
expect:
29+
nameFromFile == 'some.thing'
30+
}
31+
32+
def "ignores multi line comments"() {
33+
given:
34+
def nameFromFile = ModuleInfoParser.moduleName('''
35+
/*
36+
module some.thing.else;
37+
*/
38+
module some.thing {
39+
requires static foo.bar.la;
40+
}
41+
''')
42+
43+
expect:
44+
nameFromFile == 'some.thing'
45+
}
46+
47+
def "ignores multi line comments between keywords"() {
48+
given:
49+
def nameFromFile = ModuleInfoParser.moduleName('''
50+
module /*module some.other*/ some.thing { /* module
51+
odd comment*/ requires transitive foo.bar.la;
52+
requires/* weird comment*/ static foo.bar.lo;
53+
requires /*something to say*/foo.bar.li; /*
54+
requires only.a.comment
55+
*/
56+
}
57+
''')
58+
59+
expect:
60+
nameFromFile == 'some.thing'
61+
}
62+
}

0 commit comments

Comments
 (0)