Skip to content

Commit

Permalink
fix #279 determine module name of annotated module
Browse files Browse the repository at this point in the history
  • Loading branch information
xzel23 committed Nov 6, 2024
1 parent 3f8b7be commit a911923
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 3 deletions.
6 changes: 5 additions & 1 deletion src/main/groovy/org/beryx/jlink/util/Util.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,11 @@ class Util {

private static final String IMPORT_DECLARATION = 'import' + IGNORE + '(static' + IGNORE + ')?' + QUALIFIED_NAME + '(\\.\\*' + ')?' + IGNORE + ';'
private static final String IMPORT_DECLARATIONS = '(' + IMPORT_DECLARATION + IGNORE + ')*'
private static final String MODULE_DECLARATION = '(?s)' + IGNORE + IMPORT_DECLARATIONS + '(open' + IGNORE + ')?' + 'module' + IGNORE + '(?<MODULE>' + QUALIFIED_NAME + ').*?'

private static final String ANNOTATION = '(@' + QUALIFIED_NAME + '((' + WS + ')*(\\([^)]*\\)))?)'
private static final String MODULE_ANNOTATIONS = '(' + ANNOTATION + IGNORE + ')*'

private static final String MODULE_DECLARATION = '(?s)' + IGNORE + IMPORT_DECLARATIONS + MODULE_ANNOTATIONS + '(open' + IGNORE + ')?' + 'module' + IGNORE + '(?<MODULE>' + QUALIFIED_NAME + ').*?'

private static final Pattern PATTERN = Pattern.compile(MODULE_DECLARATION)

Expand Down
15 changes: 15 additions & 0 deletions src/test/groovy/org/beryx/jlink/JlinkPluginSpec.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,21 @@ class JlinkPluginSpec extends Specification {
checkOutput(result, 'helloBom', '{"from":"Alice","to":"Bob","greeting":"Hello"}')
}

def "should create runtime image of project with annotations on module declaration"() {
when:
File buildFile = setUpBuild('hello-annotated-module')
BuildResult result = GradleRunner.create()
.withDebug(true)
.withGradleVersion('7.6')
.withProjectDir(testProjectDir.toFile())
.withPluginClasspath()
.withArguments(JlinkPlugin.TASK_NAME_JLINK, "-is")
.build();

then:
checkOutput(result, 'helloAnnotatedModule', 'Hello annotated module!')
}

def "should create image of project with multiple launchers"() {
when:

Expand Down
2 changes: 2 additions & 0 deletions src/test/groovy/org/beryx/jlink/UtilSpec.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ class UtilSpec extends Specification {
'/*my module*/\nmodule /*---*/ a.b.c // declaration\n{\n exports a.b.c;\n}' | 'a.b.c'
'import x.y.Z;//comment\nimport x.y.W;\nmodule /*---*/ a.b.c' | 'a.b.c'
'import x.y.z.*;\nimport x.y/*WW*/./*ZZ*/w.*;\nmodule //x.y.z\n/*-->*/a.b.c' | 'a.b.c'
'import x.y.z.*;\n@Annotation\nmodule //x.y.z\n/*-->*/a.b.c' | 'a.b.c'
'import x.y.z.*;\n@Annotation("text")\nmodule //x.y.z\n/*-->*/a.b.c' | 'a.b.c'
}

@Unroll
Expand Down
31 changes: 31 additions & 0 deletions src/test/resources/hello-annotated-module/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
plugins {
id 'org.beryx.jlink'
id 'org.javamodularity.moduleplugin' version '1.8.12'
}

repositories {
mavenCentral()
}

sourceCompatibility = 17
targetCompatibility = 17

dependencies {
implementation 'org.jspecify:jspecify:1.0.0'
}

application {
mainClass = 'org.beryx.modular.annotatedmodule.HelloAnnotatedModule'
}
jar {
manifest {
attributes 'Implementation-Title': 'helloAnnotatedModule',
'Main-Class': application.mainClass
}
}

jlink {
launcher {
name = 'helloAnnotatedModule'
}
}
1 change: 1 addition & 0 deletions src/test/resources/hello-annotated-module/settings.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rootProject.name = 'helloAnnotatedModule'
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import org.jspecify.annotations.NullMarked;

@NullMarked
module org.beryx.modular.annotatedmodule {
requires org.jspecify;
opens org.beryx.modular.annotatedmodule;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.beryx.modular.annotatedmodule;

import java.io.StringReader;

public class HelloAnnotatedModule {
public static void main(String[] args) throws Exception {
System.out.println("Hello annotated module!");
}
}
4 changes: 2 additions & 2 deletions src/test/resources/hello-toolchain/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ application {

java {
toolchain {
languageVersion = JavaLanguageVersion.of(23)
vendor = JvmVendorSpec.ADOPTIUM
languageVersion = JavaLanguageVersion.of(24)
//vendor = JvmVendorSpec.ADOPTIUM
}
}

Expand Down

0 comments on commit a911923

Please sign in to comment.