Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 82 additions & 0 deletions biz.aQute.bndlib.tests/test/test/ClazzTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,20 @@
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.Serializable;
import java.lang.annotation.ElementType;
import java.lang.annotation.Target;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.atomic.AtomicReference;
import java.util.jar.Attributes;
import java.util.jar.Manifest;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledForJreRange;
Expand All @@ -30,16 +34,21 @@
import aQute.bnd.osgi.ClassDataCollector;
import aQute.bnd.osgi.Clazz;
import aQute.bnd.osgi.Clazz.FieldDef;
import aQute.bnd.osgi.Clazz.JAVA;
import aQute.bnd.osgi.Clazz.MethodDef;
import aQute.bnd.osgi.Clazz.QUERY;
import aQute.bnd.osgi.Constants;
import aQute.bnd.osgi.Descriptors;
import aQute.bnd.osgi.Descriptors.PackageRef;
import aQute.bnd.osgi.EmbeddedResource;
import aQute.bnd.osgi.FileResource;
import aQute.bnd.osgi.Instruction;
import aQute.bnd.osgi.Jar;
import aQute.bnd.osgi.Macro;
import aQute.bnd.osgi.Resource;
import aQute.bnd.xmlattribute.XMLAttributeFinder;
import aQute.lib.io.IO;
import aQute.lib.manifest.ManifestUtil;

public class ClazzTest {

Expand Down Expand Up @@ -1111,4 +1120,77 @@ public void kotlin_LambdaClass() throws Exception {
}
}


/**
* This test creates a fake java .class file with an imaginary large major
* version (10000) to test how our Analyzer handles class files of a yet
* unknown future JDK.
*/
@Test
void testUnknownJDKClass() throws Exception {


Comment on lines +1131 to +1132
Copy link

Copilot AI Feb 2, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unnecessary blank lines. Remove the extra blank line to maintain consistent formatting with the rest of the test file.

Copilot uses AI. Check for mistakes.
File file = IO.getFile("bin_test/test/ClazzTest$Inner.class");
byte[] fakeClassBytes = Files.readAllBytes(file.toPath());

int newMajor = 10_000;
int expected = 9955; // newMajor - 45;
// Patch major version (u2 big-endian)
// https://docs.oracle.com/javase/specs/jvms/se21/html/jvms-4.html
fakeClassBytes[6] = (byte) ((newMajor >> 8) & 0xFF);
fakeClassBytes[7] = (byte) (newMajor & 0xFF);

try (Jar jar = new Jar("future");
Analyzer a = new Analyzer(jar)) {

Resource r = new EmbeddedResource(fakeClassBytes, fakeClassBytes.length);

Clazz c = new Clazz(a, "", r);
c.parseClassFile(new ByteArrayInputStream(fakeClassBytes), new ClassDataCollector() {});
a.getClassspace()
.put(c.getClassName(), c);

assertThat(c.getMajorVersion()).isEqualTo(newMajor);

Manifest calcManifest = a.calcManifest();
ManifestUtil.write(calcManifest, System.out);
Attributes mainAttributes = calcManifest.getMainAttributes();
assertThat(mainAttributes.getValue(Constants.REQUIRE_CAPABILITY))
.contains("(&(osgi.ee=JavaSE)(version=" + expected + "))");
assertThat(a.getEEs()).containsExactly(JAVA.UNKNOWN);


Comment on lines +1161 to +1162
Copy link

Copilot AI Feb 2, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unnecessary blank lines. Remove the extra blank lines to maintain consistent formatting with the rest of the test file.

Copilot uses AI. Check for mistakes.
}
catch (Exception e) {
e.printStackTrace();
throw e;
}

}

@Test
void testBuildEEFilter_lenientKnown() {
// lenient = true but version within known range
String result = Clazz.JAVA.buildEEFilterLenient(55); // within range
assertEquals("(&(osgi.ee=JavaSE)(version=11))", result);
}


Copy link

Copilot AI Feb 2, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unnecessary blank line. Remove the extra blank line to maintain consistent formatting with the rest of the test file.

Suggested change

Copilot uses AI. Check for mistakes.
@Test
void testBuildEEFilter_lenientTooLow() {
// version < 0 -> UNKNOWN
String result = Clazz.JAVA.buildEEFilterLenient(40);
assertEquals("(&(osgi.ee=JavaSE)(version=UNKNOWN))", result);
Copy link

Copilot AI Feb 2, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test expects a filter with version=UNKNOWN, which is semantically incorrect for OSGi. The version attribute in OSGi filters expects a numeric value. The test should be updated to either expect the original UNKNOWN filter format "(osgi.ee=UNKNOWN)" or to test a different edge case, since a major version of 40 (predating Java 1.1 which starts at 45) represents an invalid class file.

Suggested change
assertEquals("(&(osgi.ee=JavaSE)(version=UNKNOWN))", result);
assertEquals("(osgi.ee=UNKNOWN)", result);

Copilot uses AI. Check for mistakes.
}

@Test
void testBuildEEFilter_lenientTooHigh() {
// version >= JAVA.values.length - 1 -> eeFilter(version)
int maxmajor = Clazz.JAVA.values()[Clazz.JAVA.values().length - 2].getMajor();
int tooHigh = maxmajor + 10;
int expected = tooHigh - 45;
String result = Clazz.JAVA.buildEEFilterLenient(tooHigh);
// version = max. known version + 1
assertEquals("(&(osgi.ee=JavaSE)(version=" + expected + "))", result);
}
}
27 changes: 21 additions & 6 deletions biz.aQute.bndlib/src/aQute/bnd/osgi/Analyzer.java
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ public class Analyzer extends Processor {
private final static Logger logger = LoggerFactory.getLogger(Analyzer.class);
private final static Version frameworkR7 = new Version("1.9");
private final SortedSet<Clazz.JAVA> ees = new TreeSet<>();
private int highestMajorJavaVersion = 0;
Copy link

Copilot AI Feb 2, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inconsistent field alignment and extra tab character before the equals sign. The field declaration should follow the same alignment pattern as the other fields in this class (compare with line 120 above).

Suggested change
private int highestMajorJavaVersion = 0;
private int highestMajorJavaVersion = 0;

Copilot uses AI. Check for mistakes.

// Bundle parameters
private Jar dot;
Expand Down Expand Up @@ -231,8 +232,14 @@ private void analyze0() throws Exception {
classspace.values()
.stream()
.filter(c -> !c.isModule())
.map(Clazz::getFormat)
.forEach(ees::add);
.forEach(c -> {
ees.add(c.getFormat());
int majorVersion = c.getMajorVersion();
if(majorVersion > highestMajorJavaVersion) {
Copy link

Copilot AI Feb 2, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing space after if keyword. The codebase convention is to use if ( with a space before the opening parenthesis.

Suggested change
if(majorVersion > highestMajorJavaVersion) {
if (majorVersion > highestMajorJavaVersion) {

Copilot uses AI. Check for mistakes.
highestMajorJavaVersion = majorVersion;
}

Copy link

Copilot AI Feb 2, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unnecessary blank line inside the forEach block. This should be removed to maintain consistency with the coding style in the rest of the file.

Suggested change

Copilot uses AI. Check for mistakes.
});

try (ClassDataCollectors cds = new ClassDataCollectors(this)) {
List<ClassParser> parsers = getPlugins(ClassParser.class);
Expand Down Expand Up @@ -1301,9 +1308,17 @@ public Jar findClasspathEntry(String bsn, String r) {
* highest found profile is added. This only works for java packages.
*/
private String doEEProfiles(JAVA highest) throws IOException {

String highestFilter;
if (highest == aQute.bnd.osgi.Clazz.JAVA.UNKNOWN) {
highestFilter = aQute.bnd.osgi.Clazz.JAVA.buildEEFilterLenient(highestMajorJavaVersion);
} else {
highestFilter = highest.getFilter();
}

String ee = getProperty(EEPROFILE);
if (ee == null)
return highest.getFilter();
return highestFilter;

ee = ee.trim();

Expand All @@ -1312,7 +1327,7 @@ private String doEEProfiles(JAVA highest) throws IOException {
if (ee.equals(EEPROFILE_AUTO_ATTRIBUTE)) {
profiles = highest.getProfiles();
if (profiles == null)
return highest.getFilter();
return highestFilter;
} else {
profiles = OSGiHeader.parseProperties(ee)
.stream()
Expand Down Expand Up @@ -1349,11 +1364,11 @@ private String doEEProfiles(JAVA highest) throws IOException {
//
// Ouch, outside any profile
//
return highest.getFilter();
return highestFilter;
}
}

String filter = highest.getFilter();
String filter = highestFilter;
if (!found.isEmpty())
filter = filter.replaceAll("JavaSE", "JavaSE/" + found.last());
// TODO a more elegant way to build the filter, we now assume JavaSE
Expand Down
29 changes: 28 additions & 1 deletion biz.aQute.bndlib/src/aQute/bnd/osgi/Clazz.java
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ public Map<String, Set<String>> getProfiles() throws IOException {
this.major = ordinal() + 45;
String version = Integer.toString(ordinal() + 1);
this.ee = "JavaSE-" + version;
this.filter = "(&(osgi.ee=JavaSE)(version=" + version + "))";
this.filter = eeFilter(version);
}

JAVA(String ee, String filter) {
Expand Down Expand Up @@ -248,6 +248,28 @@ public String getEE() {
return ee;
}

/**
* Returns an eeFilter String also supporting yet unknown JDKs (lenient)
*/
public static String buildEEFilterLenient(int major) {

// lenient: We try to return a useful filter
// even for yet unknown JDKs
int version = major - 45;
if ((version < 0)) {
Copy link

Copilot AI Feb 2, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unnecessary parentheses around the condition. The expression if (version < 0) is clearer and follows standard Java style.

Suggested change
if ((version < 0)) {
if (version < 0) {

Copilot uses AI. Check for mistakes.
return eeFilter("UNKNOWN");
Copy link

Copilot AI Feb 2, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The filter string "(&(osgi.ee=JavaSE)(version=UNKNOWN))" is semantically incorrect for OSGi. In OSGi filter syntax, the version attribute expects a numeric value, not the string "UNKNOWN". For class files with invalid major versions (less than 45), consider either returning the original UNKNOWN filter "(osgi.ee=UNKNOWN)" or handling this as an error case, since such class files would be invalid according to the JVM specification.

Suggested change
return eeFilter("UNKNOWN");
return "(osgi.ee=UNKNOWN)";

Copilot uses AI. Check for mistakes.
} else if (version >= (JAVA.values.length - 1)) {
// yet UNKNOWN
return eeFilter(Integer.toString(version));
}

return format(major).getFilter();
}

private static String eeFilter(String version) {
return "(&(osgi.ee=JavaSE)(version=" + version + "))";
}

public String getFilter() {
return filter;
}
Expand Down Expand Up @@ -1936,6 +1958,11 @@ public JAVA getFormat() {

}

public int getMajorVersion() {
return classFile.major_version;

}

public static String objectDescriptorToFQN(String string) {
if ((string.startsWith("L") || string.startsWith("T")) && string.endsWith(";"))
return string.substring(1, string.length() - 1)
Expand Down
Loading