Skip to content

Commit

Permalink
fix module-info. better errors.
Browse files Browse the repository at this point in the history
  • Loading branch information
wagyourtail committed May 11, 2024
1 parent dd2e386 commit dba29bc
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ abstract class ShadeAPI @Inject constructor(@Internal val jvmdg: JVMDowngraderEx
if (apiParts.containsKey(ExtendedType(Type.getObjectType(node.name)))) {
rootParts.add(apiParts.getValue(ExtendedType(Type.getObjectType(node.name))))
} else {
if (node.name.equals("module-info")) continue;
rootParts += getApiParts(node, apiClasses, apiParts) {
when (it) {
in apiClasses -> {
Expand Down Expand Up @@ -175,7 +176,7 @@ abstract class ShadeAPI @Inject constructor(@Internal val jvmdg: JVMDowngraderEx
openZipFileSystem(tempOutput.toPath(), mapOf("create" to true)).use { fs ->
for (node in outputNodes.values) {
val path = fs.getPath(node.name + ".class")
path.parent.createDirectories()
path.parent?.createDirectories()
path.outputStream(StandardOpenOption.CREATE).use { os ->
val writer = ASMClassWriter(0) {
if (outputNodes.containsKey(it)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,9 @@ public void init() {
@Override
public ClassNode otherTransforms(ClassNode clazz) {
fixPrivateMethodsInInterfaces(clazz);
if (clazz.name.equals("module-info")) {
return null;
}
return clazz;
}

Expand Down
10 changes: 6 additions & 4 deletions src/main/java/xyz/wagyourtail/jvmdg/ClassDowngrader.java
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,10 @@ protected Set<ClassNode> downgrade(ClassNode clazz, boolean enableRuntime, Funct
}
Set<ClassNode> newClasses = new HashSet<>();
for (ClassNode c : classes) {
newClasses.add(downgrader.downgrade(c, newClasses, enableRuntime, getReadOnly));
ClassNode downgraded = downgrader.downgrade(c, newClasses, enableRuntime, getReadOnly);
if (downgraded != null) {
newClasses.add(downgraded);
}
}
classes = newClasses;
version = downgrader.outputVersion;
Expand Down Expand Up @@ -321,9 +324,8 @@ public ClassNode apply(String s) {
}
outputs.put(c.name, classNodeToBytes(c, getExtraRead));
}
} catch (InvocationTargetException | IllegalAccessException | ClassNotFoundException | NoSuchMethodException |
InstantiationException | IOException e) {
throw new RuntimeException(e);
} catch (Exception e) {
throw new RuntimeException("Failed to downgrade " + name.get(), e);
}
if (Constants.DEBUG) {
for (Map.Entry<String, byte[]> entry : outputs.entrySet()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public byte[] apply(String s) {
}
}
});
if (outputs == null || outputs.isEmpty()) {
if (outputs == null) {
// doesn't need downgrading
return defineClass(name, bytes, 0, bytes.length);
}
Expand All @@ -79,6 +79,9 @@ public byte[] apply(String s) {
}
try {
bytes = outputs.get(internalName);
if (bytes == null) {
throw new ClassNotFoundException("removed by downgrader: " + name);
}
return defineClass(name, bytes, 0, bytes.length);
} catch (ClassFormatError e) {
ClassDowngrader.currentVersionDowngrader.writeBytesToDebug(name, bytes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public byte[] apply(String s) {
}
}
});
if (outputs == null || outputs.isEmpty()) {
if (outputs == null) {
Files.copy(file, outFile, StandardCopyOption.REPLACE_EXISTING);
} else {
for (Map.Entry<String, byte[]> entry : outputs.entrySet()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@ public byte[] apply(String s) {
}
});
LOGGER.fine("transform size: " + (outputs == null ? null : outputs.size()));
if (outputs == null || outputs.isEmpty()) return bytes;
if (outputs == null) return bytes;
bytes = null;
for (Map.Entry<String, byte[]> entry : outputs.entrySet()) {
LOGGER.fine("Loading " + entry.getKey() + " into " + loader);
if (DUMP_CLASSES) {
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/xyz/wagyourtail/jvmdg/version/VersionProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,10 @@ public Type stubClass(Type desc) {
}

public ClassNode stubMethods(ClassNode owner, Set<ClassNode> extra, boolean enableRuntime, IOFunction<Type, Set<MemberNameAndDesc>> memberResolver, IOFunction<Type, List<Type>> superTypeResolver) throws IOException {
if (owner.name.equals("module-info")) {
return owner;
}

for (MethodNode method : new ArrayList<>(owner.methods)) {
MethodNode newMethod = stubMethods(method, owner, extra, enableRuntime, memberResolver, superTypeResolver);
if (newMethod != method) {
Expand Down Expand Up @@ -599,14 +603,22 @@ public List<Type> apply(Type o) throws IOException {
};

clazz = stubClasses(clazz, enableRuntime);
if (clazz == null) return null;
clazz = stubMethods(clazz, extra, enableRuntime, getMembers, getSuperTypes);
if (clazz == null) return null;
clazz = insertAbstractMethods(clazz, extra, getMembers, getSuperTypes);
if (clazz == null) return null;
clazz = otherTransforms(clazz, extra, getReadOnly);
if (clazz == null) return null;
clazz.version = inputVersion - 1;
return clazz;
}

public ClassNode insertAbstractMethods(ClassNode clazz, Set<ClassNode> extra, IOFunction<Type, Set<MemberNameAndDesc>> getMembers, IOFunction<Type, List<Type>> getSuperTypes) throws IOException {
if (clazz.name.equals("module-info")) {
return clazz;
}

Map<MemberNameAndDesc, Pair<Method, Stub>> members = getStubMapper(Type.getObjectType(clazz.name), getMembers, getSuperTypes).getAbstracts();
for (Map.Entry<MemberNameAndDesc, Pair<Method, Stub>> member : members.entrySet()) {
boolean contains = false;
Expand Down Expand Up @@ -654,6 +666,10 @@ public ClassNode otherTransforms(ClassNode clazz) {
}

public ClassNode stubClasses(ClassNode clazz, boolean enableRuntime) {
if (clazz.name.equals("module-info")) {
return clazz;
}

// super
Type type = Type.getObjectType(clazz.superName);
if (classStubs.containsKey(type)) {
Expand Down

0 comments on commit dba29bc

Please sign in to comment.