Skip to content

Commit

Permalink
Fix calculating moduleNames for multi-release-jar-dependencies
Browse files Browse the repository at this point in the history
Fixes #5327

Signed-off-by: Christoph Läubrich <laeubi@laeubi-soft.de>
  • Loading branch information
laeubi committed Aug 26, 2022
1 parent 2d1a4c6 commit e2aafa9
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 2 deletions.
3 changes: 2 additions & 1 deletion biz.aQute.bndlib.tests/bnd.bnd
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
slf4j.simple;version=latest,\
org.apiguardian:apiguardian-api;version=latest,\
${junit},\
${mockito}
${mockito},\
com.google.gson

-runtrace: true
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import aQute.bnd.classfile.ClassFile;
import aQute.bnd.classfile.ModuleAttribute;
import aQute.bnd.classfile.ModuleAttribute.Require;
import aQute.bnd.classfile.ModuleMainClassAttribute;
import aQute.bnd.classfile.ModulePackagesAttribute;
import aQute.bnd.osgi.Builder;
Expand Down Expand Up @@ -1377,4 +1378,36 @@ public void simpleModule() throws Exception {
}
}

@Test
public void multiReleaseDependency() throws Exception {
try (Builder b = new Builder()) {
b.setProperty(Constants.JPMS_MODULE_INFO, "foo");
b.setProperty(Constants.BUNDLE_SYMBOLICNAME, "foo");
b.setProperty(Constants.BUNDLE_VERSION, "1.2.7");
b.setProperty(Constants.PRIVATEPACKAGE, "test.jpms.k.*");
b.addClasspath(new File("bin_test"));
// the module name of this is 'com.google.gson' but it is also
// compiled for java 8, so the default module name would be 'gson'
// if multi-release is not handled correctly
b.addClasspath(IO.getFile("testresources/gson-2.9.1.jar"));
Jar jar = b.build();
assertTrue(b.check());
Resource moduleInfo = jar.getResource(Constants.MODULE_INFO_CLASS);
assertNotNull(moduleInfo);

ClassFile module_info = ClassFile.parseClassFile(new DataInputStream(moduleInfo.openInputStream()));

assertThat(module_info.this_class).isEqualTo("module-info");

ModuleAttribute moduleAttribute = Arrays.stream(module_info.attributes)
.filter(ModuleAttribute.class::isInstance)
.map(ModuleAttribute.class::cast)
.findFirst()
.orElse(null);
assertThat(moduleAttribute.requires).hasSize(2)
.anyMatch(e -> e.requires.equals("java.base"))
.anyMatch(e -> e.requires.equals("com.google.gson"));
}
}

}
16 changes: 16 additions & 0 deletions biz.aQute.bndlib.tests/test/test/jpms/k/Foo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package test.jpms.k;

import com.google.gson.Gson;

public class Foo {

public void serialize() {
Gson gson = new Gson();
String json = gson.toJson(new MyObj());
}

public class MyObj {
public String a;
public int b;
}
}
3 changes: 3 additions & 0 deletions biz.aQute.bndlib.tests/test/test/jpms/k/package-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@org.osgi.annotation.bundle.Export
@org.osgi.annotation.versioning.Version("1.0.0")
package test.jpms.k;
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import java.util.Map.Entry;
import java.util.Objects;
import java.util.Optional;
import java.util.OptionalInt;
import java.util.Set;
import java.util.function.Supplier;
import java.util.regex.Matcher;
Expand Down Expand Up @@ -222,7 +223,18 @@ public void verify(final Analyzer analyzer) throws Exception {
}

private String getModuleName(Analyzer analyzer, Jar jar, Parameters moduleInfoOptions) throws Exception {
String moduleName = jar.getModuleName();
// we need to use the highest EE of the analyzed jar
int targetRelease = Optional.ofNullable(EE.parse(analyzer.getHighestEE()
.getEE()))
// but at minimum Java 9
.filter(ee -> {
OptionalInt target = ee.getReleaseTarget();
return target.isPresent() && target.getAsInt() > 8;
})
.orElse(EE.JavaSE_9)
.getReleaseTarget()
.getAsInt();
String moduleName = jar.getModuleName(targetRelease);
if (moduleName == null) {
if (jar.getSource() != null && jar.getSource()
.isDirectory()) {
Expand Down

0 comments on commit e2aafa9

Please sign in to comment.