Skip to content

Commit

Permalink
Merge pull request #5802 from pkriens/issue/5792-cleanup-jpms-module-…
Browse files Browse the repository at this point in the history
…name

Cleans up the module names according to JPMS rules.
  • Loading branch information
pkriens authored Oct 10, 2023
2 parents cb0a0a5 + e30400f commit eda505e
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 28 deletions.
38 changes: 19 additions & 19 deletions biz.aQute.bndlib.tests/test/test/jpms/JPMSModuleInfoPluginTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ public void moduleWithOptionsIgnore() throws Exception {

assertThat(moduleAttribute.requires).hasSize(3)
.anyMatch(e -> e.requires.equals("java.base"))
.anyMatch(e -> e.requires.equals("geronimo-jcdi_2.0_spec"))
.anyMatch(e -> e.requires.equals("geronimo.jcdi.2.0.spec"))
.anyMatch(e -> e.requires.equals("java.json"));

ModulePackagesAttribute modulePackagesAttribute = Arrays.stream(module_info.attributes)
Expand Down Expand Up @@ -212,7 +212,7 @@ public void moduleWithOptionsStatic() throws Exception {

assertThat(moduleAttribute.requires).hasSize(4)
.anyMatch(e -> e.requires.equals("java.base"))
.anyMatch(e -> e.requires.equals("geronimo-jcdi_2.0_spec"))
.anyMatch(e -> e.requires.equals("geronimo.jcdi.2.0.spec"))
.anyMatch(e -> e.requires.equals("java.json"))
.anyMatch(e -> e.requires.equals("java.json.bind"));

Expand Down Expand Up @@ -395,15 +395,15 @@ public void moduleWithAllDynamicImportsOrOptionalIsStatic() throws Exception {

assertThat(moduleAttribute.requires).hasSize(4)
.anyMatch(e -> e.requires.equals("java.base"))
.anyMatch(e -> e.requires.equals("geronimo-jcdi_2.0_spec"))
.anyMatch(e -> e.requires.equals("geronimo.jcdi.2.0.spec"))
.anyMatch(e -> e.requires.equals("java.json"))
.anyMatch(e -> e.requires.equals("java.json.bind"));

assertThat(moduleAttribute.requires).filteredOn(e -> e.requires.equals("java.json.bind"))
.flatExtracting(e -> Arrays.asList(e.requires_flags))
.containsExactlyInAnyOrder(ModuleAttribute.Require.ACC_STATIC_PHASE);

assertThat(moduleAttribute.requires).filteredOn(e -> e.requires.equals("geronimo-jcdi_2.0_spec"))
assertThat(moduleAttribute.requires).filteredOn(e -> e.requires.equals("geronimo.jcdi.2.0.spec"))
.flatExtracting(e -> Arrays.asList(e.requires_flags))
.containsExactlyInAnyOrder(ModuleAttribute.Require.ACC_TRANSITIVE);

Expand Down Expand Up @@ -459,15 +459,15 @@ public void moduleWithAllDynamicImportsIsStatic_2() throws Exception {

assertThat(moduleAttribute.requires).hasSize(4)
.anyMatch(e -> e.requires.equals("java.base"))
.anyMatch(e -> e.requires.equals("geronimo-jcdi_2.0_spec"))
.anyMatch(e -> e.requires.equals("geronimo.jcdi.2.0.spec"))
.anyMatch(e -> e.requires.equals("java.json"))
.anyMatch(e -> e.requires.equals("java.json.bind"));

assertThat(moduleAttribute.requires).filteredOn(e -> e.requires.equals("java.json.bind"))
.flatExtracting(e -> Arrays.asList(e.requires_flags))
.containsExactlyInAnyOrder(ModuleAttribute.Require.ACC_STATIC_PHASE);

assertThat(moduleAttribute.requires).filteredOn(e -> e.requires.equals("geronimo-jcdi_2.0_spec"))
assertThat(moduleAttribute.requires).filteredOn(e -> e.requires.equals("geronimo.jcdi.2.0.spec"))
.flatExtracting(e -> Arrays.asList(e.requires_flags))
.containsExactlyInAnyOrder(ModuleAttribute.Require.ACC_TRANSITIVE);

Expand Down Expand Up @@ -524,15 +524,15 @@ public void moduleWithAllDynamicImportsIsStatic() throws Exception {

assertThat(moduleAttribute.requires).hasSize(4)
.anyMatch(e -> e.requires.equals("java.base"))
.anyMatch(e -> e.requires.equals("geronimo-jcdi_2.0_spec"))
.anyMatch(e -> e.requires.equals("geronimo.jcdi.2.0.spec"))
.anyMatch(e -> e.requires.equals("java.json"))
.anyMatch(e -> e.requires.equals("java.json.bind"));

assertThat(moduleAttribute.requires).filteredOn(e -> e.requires.equals("java.json.bind"))
.flatExtracting(e -> Arrays.asList(e.requires_flags))
.containsExactlyInAnyOrder(ModuleAttribute.Require.ACC_STATIC_PHASE);

assertThat(moduleAttribute.requires).filteredOn(e -> e.requires.equals("geronimo-jcdi_2.0_spec"))
assertThat(moduleAttribute.requires).filteredOn(e -> e.requires.equals("geronimo.jcdi.2.0.spec"))
.flatExtracting(e -> Arrays.asList(e.requires_flags))
.containsExactlyInAnyOrder(ModuleAttribute.Require.ACC_TRANSITIVE);

Expand Down Expand Up @@ -588,15 +588,15 @@ public void moduleWithAllResolutionOptionalImportsIsStatic() throws Exception {

assertThat(moduleAttribute.requires).hasSize(4)
.anyMatch(e -> e.requires.equals("java.base"))
.anyMatch(e -> e.requires.equals("geronimo-jcdi_2.0_spec"))
.anyMatch(e -> e.requires.equals("geronimo.jcdi.2.0.spec"))
.anyMatch(e -> e.requires.equals("java.json"))
.anyMatch(e -> e.requires.equals("java.json.bind"));

assertThat(moduleAttribute.requires).filteredOn(e -> e.requires.equals("java.json.bind"))
.flatExtracting(e -> Arrays.asList(e.requires_flags))
.containsExactlyInAnyOrder(ModuleAttribute.Require.ACC_STATIC_PHASE);

assertThat(moduleAttribute.requires).filteredOn(e -> e.requires.equals("geronimo-jcdi_2.0_spec"))
assertThat(moduleAttribute.requires).filteredOn(e -> e.requires.equals("geronimo.jcdi.2.0.spec"))
.flatExtracting(e -> Arrays.asList(e.requires_flags))
.containsExactlyInAnyOrder(ModuleAttribute.Require.ACC_TRANSITIVE);

Expand Down Expand Up @@ -652,15 +652,15 @@ public void moduleWithNoImportsIsStatic() throws Exception {
assertThat(moduleAttribute.requires).hasSize(5)
.anyMatch(e -> e.requires.equals("java.base"))
.anyMatch(e -> e.requires.equals("java.management"))
.anyMatch(e -> e.requires.equals("geronimo-jcdi_2.0_spec"))
.anyMatch(e -> e.requires.equals("geronimo.jcdi.2.0.spec"))
.anyMatch(e -> e.requires.equals("java.json"))
.anyMatch(e -> e.requires.equals("java.json.bind"));

assertThat(moduleAttribute.requires).filteredOn(e -> e.requires.equals("java.management"))
.flatExtracting(e -> Arrays.asList(e.requires_flags))
.containsExactlyInAnyOrder(ModuleAttribute.Require.ACC_STATIC_PHASE);

assertThat(moduleAttribute.requires).filteredOn(e -> e.requires.equals("geronimo-jcdi_2.0_spec"))
assertThat(moduleAttribute.requires).filteredOn(e -> e.requires.equals("geronimo.jcdi.2.0.spec"))
.flatExtracting(e -> Arrays.asList(e.requires_flags))
.containsExactlyInAnyOrder(ModuleAttribute.Require.ACC_TRANSITIVE);

Expand Down Expand Up @@ -716,7 +716,7 @@ public void moduleManualConfiguration() throws Exception {
assertThat(moduleAttribute.requires).hasSize(5)
.anyMatch(e -> e.requires.equals("bar"))
.anyMatch(e -> e.requires.equals("java.base"))
.anyMatch(e -> e.requires.equals("geronimo-jcdi_2.0_spec"))
.anyMatch(e -> e.requires.equals("geronimo.jcdi.2.0.spec"))
.anyMatch(e -> e.requires.equals("java.json"))
.anyMatch(e -> e.requires.equals("java.json.bind"));

Expand All @@ -728,7 +728,7 @@ public void moduleManualConfiguration() throws Exception {
.flatExtracting(e -> Arrays.asList(e.requires_flags))
.containsExactlyInAnyOrder(0);

assertThat(moduleAttribute.requires).filteredOn(e -> e.requires.equals("geronimo-jcdi_2.0_spec"))
assertThat(moduleAttribute.requires).filteredOn(e -> e.requires.equals("geronimo.jcdi.2.0.spec"))
.flatExtracting(e -> Arrays.asList(e.requires_flags))
.containsExactlyInAnyOrder(ModuleAttribute.Require.ACC_TRANSITIVE);

Expand Down Expand Up @@ -793,7 +793,7 @@ public void moduleRequiresModuleB() throws Exception {

assertThat(moduleAttribute.requires).hasSize(4)
.anyMatch(e -> e.requires.equals("java.base"))
.anyMatch(e -> e.requires.equals("geronimo-jcdi_2.0_spec"))
.anyMatch(e -> e.requires.equals("geronimo.jcdi.2.0.spec"))
.anyMatch(e -> e.requires.equals("java.json"))
.anyMatch(e -> e.requires.equals("java.json.bind"));

Expand All @@ -805,7 +805,7 @@ public void moduleRequiresModuleB() throws Exception {
.flatExtracting(e -> Arrays.asList(e.requires_flags))
.containsExactlyInAnyOrder(0);

assertThat(moduleAttribute.requires).filteredOn(e -> e.requires.equals("geronimo-jcdi_2.0_spec"))
assertThat(moduleAttribute.requires).filteredOn(e -> e.requires.equals("geronimo.jcdi.2.0.spec"))
.flatExtracting(e -> Arrays.asList(e.requires_flags))
.containsExactlyInAnyOrder(ModuleAttribute.Require.ACC_TRANSITIVE);

Expand Down Expand Up @@ -869,7 +869,7 @@ public void moduleRequiresModuleA() throws Exception {

assertThat(moduleAttribute.requires).hasSize(3)
.anyMatch(e -> e.requires.equals("java.base"))
.anyMatch(e -> e.requires.equals("geronimo-jcdi_2.0_spec"))
.anyMatch(e -> e.requires.equals("geronimo.jcdi.2.0.spec"))
.anyMatch(e -> e.requires.equals("java.json"));

assertThat(moduleAttribute.uses).hasSize(0);
Expand Down Expand Up @@ -931,9 +931,9 @@ public void moduleRequiresA() throws Exception {

assertThat(moduleAttribute.requires).hasSize(2)
.anyMatch(e -> e.requires.equals("java.base"))
.anyMatch(e -> e.requires.equals("geronimo-jcdi_2.0_spec"));
.anyMatch(e -> e.requires.equals("geronimo.jcdi.2.0.spec"));

assertThat(moduleAttribute.requires).filteredOn(e -> e.requires.equals("geronimo-jcdi_2.0_spec"))
assertThat(moduleAttribute.requires).filteredOn(e -> e.requires.equals("geronimo.jcdi.2.0.spec"))
.flatExtracting(e -> Arrays.asList(e.requires_flags))
.containsExactlyInAnyOrder(ModuleAttribute.Require.ACC_TRANSITIVE);

Expand Down
27 changes: 27 additions & 0 deletions biz.aQute.bndlib/src/aQute/bnd/osgi/JPMSModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import java.util.jar.Manifest;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import aQute.bnd.build.model.EE;
import aQute.bnd.classfile.ModuleAttribute;
Expand Down Expand Up @@ -490,4 +492,29 @@ public int getNextRelease(int release) {
return tailSet.first();
}

/**
* Cleanup a bsn so that it matches a JPMS module name
*
* @param bsn a symbolic name or null
* @return a name that matches the JPMS specification for a module name or
* null if the input was null
*/
public static String cleanupName(String bsn) {
if (bsn == null)
return null;
if ( bsn.endsWith(".jar"))
bsn= bsn.substring(0, bsn.length()-4);

Pattern STRIP_SUFFIX_P = Pattern.compile("-\\d+\\..*$");
Matcher m = STRIP_SUFFIX_P.matcher(bsn);
if (m.find()) {
bsn = bsn.substring(0, m.start());
}

String[] split = bsn.split("[^A-Za-z0-9]+");
return Stream.of(split)
.filter(str -> !str.isEmpty())
.collect(Collectors.joining("."));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
import java.util.Set;
import java.util.function.Supplier;
import java.util.jar.Manifest;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.slf4j.Logger;
Expand Down Expand Up @@ -227,28 +226,27 @@ private String getModuleName(Analyzer analyzer, JPMSModule m, Parameters moduleI
return null;
}

moduleName = jar.getName();
Matcher matcher = mangledModuleName.matcher(moduleName);
if (matcher.matches()) {
moduleName = matcher.group(1);
}
moduleName = JPMSModule.cleanupName(jar.getName());
final String name = moduleName;
moduleName = moduleInfoOptions.stream()
.filterValue(attrs -> name.equals(attrs.get(SUBSTITUTE_ATTRIBUTE)))
.mapValue(attrs -> attrs.get(SUBSTITUTE_ATTRIBUTE))
.mapValue(JPMSModule::cleanupName)
.filterValue(s -> name.equals(s))
.keys()
.findFirst()
.orElse(moduleName);

if (logger.isDebugEnabled())
logger.debug("Using module name '{}' for: {}", moduleName, jar);
}
return moduleName;
return JPMSModule.cleanupName(moduleName);
}

private String name(Analyzer analyzer) {
return analyzer.getProperty(AUTOMATIC_MODULE_NAME, analyzer.getBsn());
return analyzer.getProperty(AUTOMATIC_MODULE_NAME, JPMSModule.cleanupName(analyzer.getBsn()));
}


private void packages(ModuleInfoBuilder builder, Analyzer analyzer) {
MapStream.ofNullable(analyzer.getJar()
.getDirectories())
Expand Down
29 changes: 29 additions & 0 deletions biz.aQute.bndlib/test/aQute/bnd/osgi/JPMSModuleTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package aQute.bnd.osgi;

import static org.assertj.core.api.Assertions.assertThat;

import org.junit.jupiter.api.Test;

class JPMSModuleTest {

@Test
void cleanupTest() {
assertThat(JPMSModule.cleanupName("foo-1.0.jar")).isEqualTo("foo");
assertThat(JPMSModule.cleanupName("bar-foo.jar")).isEqualTo("bar.foo");
assertThat(JPMSModule.cleanupName("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"))
.isEqualTo("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");
assertThat(JPMSModule.cleanupName("foo.jar")).isEqualTo("foo");
assertThat(JPMSModule.cleanupName("foo")).isEqualTo("foo");
assertThat(JPMSModule.cleanupName("foo.bar")).isEqualTo("foo.bar");
assertThat(JPMSModule.cleanupName("-foo.bar")).isEqualTo("foo.bar");
assertThat(JPMSModule.cleanupName("-foo.bar-")).isEqualTo("foo.bar");
assertThat(JPMSModule.cleanupName("-foo......................bar")).isEqualTo("foo.bar");
assertThat(JPMSModule.cleanupName("foo.--.bar")).isEqualTo("foo.bar");
assertThat(JPMSModule.cleanupName("-------------------------foo.--.bar")).isEqualTo("foo.bar");
assertThat(JPMSModule.cleanupName("-------------------------foo.-🙂-.bar")).isEqualTo("foo.bar");
assertThat(JPMSModule.cleanupName("foo🙂bar")).isEqualTo("foo.bar");
assertThat(JPMSModule.cleanupName("\n\nfoo🙂bar")).isEqualTo("foo.bar");
assertThat(JPMSModule.cleanupName(null)).isNull();
}

}

0 comments on commit eda505e

Please sign in to comment.