Skip to content

Commit 464b664

Browse files
committed
Align definePackage behavior with JRE modular classloaders
1 parent f4a3e66 commit 464b664

File tree

2 files changed

+1
-82
lines changed

2 files changed

+1
-82
lines changed

src/main/java/cpw/mods/cl/ModuleClassLoader.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -191,11 +191,8 @@ private Class<?> readerToClass(final ModuleReader reader, final ModuleReference
191191
if (bytes.length == 0) return null;
192192
var cname = name.replace('.','/')+".class";
193193
var modroot = this.resolvedRoots.get(ref.descriptor().name());
194-
ProtectionDomainHelper.tryDefinePackage(this, name, modroot.jar().getManifest(), t->modroot.jar().getManifest().getAttributes(t), this::definePackage); // Packages are dirctories, and can't be signed, so use raw attributes instead of signed.
195194
var cs = ProtectionDomainHelper.createCodeSource(toURL(ref.location()), modroot.jar().verifyAndGetSigners(cname, bytes));
196-
var cls = defineClass(name, bytes, 0, bytes.length, ProtectionDomainHelper.createProtectionDomain(cs, this));
197-
ProtectionDomainHelper.trySetPackageModule(cls.getPackage(), cls.getModule());
198-
return cls;
195+
return defineClass(name, bytes, 0, bytes.length, ProtectionDomainHelper.createProtectionDomain(cs, this));
199196
}
200197

201198
protected byte[] maybeTransformClassBytes(final byte[] bytes, final String name, final String context) {

src/main/java/cpw/mods/cl/ProtectionDomainHelper.java

Lines changed: 0 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -29,82 +29,4 @@ public static ProtectionDomain createProtectionDomain(CodeSource codeSource, Cla
2929
}
3030
}
3131

32-
private static final VarHandle PKG_MODULE_HANDLE;
33-
static {
34-
try {
35-
// Obtain VarHandle for NamedPackage#module via trusted lookup
36-
var trustedLookupField = MethodHandles.Lookup.class.getDeclaredField("IMPL_LOOKUP");
37-
trustedLookupField.setAccessible(true);
38-
MethodHandles.Lookup trustedLookup = (MethodHandles.Lookup) trustedLookupField.get(null);
39-
40-
Class<?> namedPackage = Class.forName("java.lang.NamedPackage");
41-
PKG_MODULE_HANDLE = trustedLookup.findVarHandle(namedPackage, "module", Module.class);
42-
} catch (Throwable t) {
43-
throw new RuntimeException("Error finding package module handle", t);
44-
}
45-
}
46-
47-
static void trySetPackageModule(Package pkg, Module module) {
48-
// Ensure named packages are bound to their module of origin
49-
// Necessary for loading package-info classes
50-
Module value = (Module) PKG_MODULE_HANDLE.get(pkg);
51-
if (value == null || !value.isNamed()) {
52-
try {
53-
PKG_MODULE_HANDLE.set(pkg, module);
54-
} catch (Throwable t) {
55-
throw new RuntimeException("Error setting package module", t);
56-
}
57-
}
58-
}
59-
60-
static Package tryDefinePackage(final ClassLoader classLoader, String name, Manifest man, Function<String, Attributes> trustedEntries, Function<String[], Package> definePackage) throws IllegalArgumentException
61-
{
62-
final var pname = name.substring(0, name.lastIndexOf('.'));
63-
if (classLoader.getDefinedPackage(pname) == null) {
64-
synchronized (classLoader) {
65-
if (classLoader.getDefinedPackage(pname) != null) return classLoader.getDefinedPackage(pname);
66-
67-
String path = pname.replace('.', '/').concat("/");
68-
String specTitle = null, specVersion = null, specVendor = null;
69-
String implTitle = null, implVersion = null, implVendor = null;
70-
71-
if (man != null) {
72-
Attributes attr = trustedEntries.apply(path);
73-
if (attr != null) {
74-
specTitle = attr.getValue(Attributes.Name.SPECIFICATION_TITLE);
75-
specVersion = attr.getValue(Attributes.Name.SPECIFICATION_VERSION);
76-
specVendor = attr.getValue(Attributes.Name.SPECIFICATION_VENDOR);
77-
implTitle = attr.getValue(Attributes.Name.IMPLEMENTATION_TITLE);
78-
implVersion = attr.getValue(Attributes.Name.IMPLEMENTATION_VERSION);
79-
implVendor = attr.getValue(Attributes.Name.IMPLEMENTATION_VENDOR);
80-
}
81-
attr = man.getMainAttributes();
82-
if (attr != null) {
83-
if (specTitle == null) {
84-
specTitle = attr.getValue(Attributes.Name.SPECIFICATION_TITLE);
85-
}
86-
if (specVersion == null) {
87-
specVersion = attr.getValue(Attributes.Name.SPECIFICATION_VERSION);
88-
}
89-
if (specVendor == null) {
90-
specVendor = attr.getValue(Attributes.Name.SPECIFICATION_VENDOR);
91-
}
92-
if (implTitle == null) {
93-
implTitle = attr.getValue(Attributes.Name.IMPLEMENTATION_TITLE);
94-
}
95-
if (implVersion == null) {
96-
implVersion = attr.getValue(Attributes.Name.IMPLEMENTATION_VERSION);
97-
}
98-
if (implVendor == null) {
99-
implVendor = attr.getValue(Attributes.Name.IMPLEMENTATION_VENDOR);
100-
}
101-
}
102-
}
103-
return definePackage.apply(new String[]{pname, specTitle, specVersion, specVendor, implTitle, implVersion, implVendor});
104-
}
105-
} else {
106-
return classLoader.getDefinedPackage(pname);
107-
}
108-
}
109-
11032
}

0 commit comments

Comments
 (0)