mirror of
https://github.com/LegacyModdingMC/UniMix.git
synced 2026-11-24 00:09:48 +00:00
Discover and pass through synthetic inner classes in mixins, updates #31
This commit is contained in:
1 parent
4e11a4a8f6
commit
a012596e98
4 files changed
+130
-5
No files matched your search
@@ -242,6 +242,12 @@ class ClassInfo extends TreeInfo {
|
||||
*/
|
||||
private final String outerName;
|
||||
|
||||
/**
|
||||
* True either if this is not an inner class or if it is an inner class but
|
||||
* does not contain a reference to its outer class.
|
||||
*/
|
||||
private final boolean isProbablyStatic;
|
||||
|
||||
/**
|
||||
* Interfaces
|
||||
*/
|
||||
@@ -305,6 +311,7 @@ class ClassInfo extends TreeInfo {
|
||||
this.name = ClassInfo.JAVA_LANG_OBJECT;
|
||||
this.superName = null;
|
||||
this.outerName = null;
|
||||
this.isProbablyStatic = true;
|
||||
this.methods = ImmutableSet.<Method>of(
|
||||
new Method("getClass", "()Ljava/lang/Class;"),
|
||||
new Method("hashCode", "()I"),
|
||||
@@ -346,11 +353,13 @@ class ClassInfo extends TreeInfo {
|
||||
this.addMethod(method, this.isMixin);
|
||||
}
|
||||
|
||||
boolean isProbablyStatic = true;
|
||||
String outerName = classNode.outerClass;
|
||||
if (outerName == null) {
|
||||
for (FieldNode field : classNode.fields) {
|
||||
if ((field.access & Opcodes.ACC_SYNTHETIC) != 0) {
|
||||
if (field.name.startsWith("this$")) {
|
||||
if (field.name.startsWith("this$")) {
|
||||
isProbablyStatic = false;
|
||||
outerName = field.desc;
|
||||
if (outerName.startsWith("L")) {
|
||||
outerName = outerName.substring(1, outerName.length() - 1);
|
||||
@@ -362,6 +371,7 @@ class ClassInfo extends TreeInfo {
|
||||
}
|
||||
}
|
||||
|
||||
this.isProbablyStatic = isProbablyStatic;
|
||||
this.outerName = outerName;
|
||||
}
|
||||
|
||||
@@ -408,6 +418,20 @@ class ClassInfo extends TreeInfo {
|
||||
return (this.access & Opcodes.ACC_PUBLIC) != 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get whether this class has ACC_SYNTHETIC
|
||||
*/
|
||||
public boolean isSynthetic() {
|
||||
return (this.access & Opcodes.ACC_SYNTHETIC) != 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get whether this class is probably static (or is not an inner class)
|
||||
*/
|
||||
public boolean isProbablyStatic() {
|
||||
return this.isProbablyStatic;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get whether this class is an inner class
|
||||
*/
|
||||
|
||||
@@ -79,6 +79,11 @@ class MixinConfig implements Comparable<MixinConfig> {
|
||||
*/
|
||||
private final transient List<MixinInfo> mixins = new ArrayList<MixinInfo>();
|
||||
|
||||
/**
|
||||
* Synthetic inner classes for mixins in this set
|
||||
*/
|
||||
private final transient Set<String> syntheticInnerClasses = new HashSet<String>();
|
||||
|
||||
/**
|
||||
* Minimum version of the mixin subsystem required to correctly apply mixins
|
||||
* in this configuration.
|
||||
@@ -285,6 +290,9 @@ class MixinConfig implements Comparable<MixinConfig> {
|
||||
MixinInfo mixin = iter.next();
|
||||
try {
|
||||
mixin.validate();
|
||||
for (String innerClass : mixin.getSyntheticInnerClasses()) {
|
||||
this.syntheticInnerClasses.add(innerClass.replace('/', '.'));
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
this.logger.error(ex.getMessage(), ex);
|
||||
this.removeMixin(mixin);
|
||||
@@ -394,6 +402,28 @@ class MixinConfig implements Comparable<MixinConfig> {
|
||||
return this.verboseLogging ? Level.INFO : Level.DEBUG;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get whether this config's package matches the supplied class name
|
||||
*
|
||||
* @param className Class name to check
|
||||
* @return True if the specified class name is in this config's mixin
|
||||
* package
|
||||
*/
|
||||
public boolean packageMatch(String className) {
|
||||
return className.startsWith(this.mixinPackage);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get whether this config can allow passthrough for the specified class
|
||||
*
|
||||
* @param className Class name to check
|
||||
* @return True if the specified class name is in this config's passthrough
|
||||
* set
|
||||
*/
|
||||
public boolean canPassThrough(String className) {
|
||||
return this.syntheticInnerClasses.contains(className);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether this configuration bundle has a mixin for the specified
|
||||
* class
|
||||
|
||||
@@ -46,6 +46,7 @@ import org.objectweb.asm.tree.AbstractInsnNode;
|
||||
import org.objectweb.asm.tree.AnnotationNode;
|
||||
import org.objectweb.asm.tree.ClassNode;
|
||||
import org.objectweb.asm.tree.FieldNode;
|
||||
import org.objectweb.asm.tree.InnerClassNode;
|
||||
import org.objectweb.asm.tree.MethodInsnNode;
|
||||
import org.objectweb.asm.tree.MethodNode;
|
||||
import org.spongepowered.asm.mixin.Implements;
|
||||
@@ -139,6 +140,11 @@ class MixinInfo extends TreeInfo implements Comparable<MixinInfo>, IMixinInfo {
|
||||
*/
|
||||
private final transient List<InterfaceInfo> softImplements = new ArrayList<InterfaceInfo>();
|
||||
|
||||
/**
|
||||
* Synthetic inner classes
|
||||
*/
|
||||
private final transient Set<String> syntheticInnerClasses = new HashSet<String>();
|
||||
|
||||
/**
|
||||
* Initial ClassNode created for mixin validation, not used for actual
|
||||
* application
|
||||
@@ -183,6 +189,7 @@ class MixinInfo extends TreeInfo implements Comparable<MixinInfo>, IMixinInfo {
|
||||
this.detachedSuper = this.validateTargetClasses(this.validationClassNode);
|
||||
this.validateMixin(this.validationClassNode);
|
||||
this.readImplementations(this.validationClassNode);
|
||||
this.readInnerClasses(this.validationClassNode);
|
||||
this.prepare(this.validationClassNode);
|
||||
this.validationClassNode = null;
|
||||
}
|
||||
@@ -286,7 +293,7 @@ class MixinInfo extends TreeInfo implements Comparable<MixinInfo>, IMixinInfo {
|
||||
*/
|
||||
private void validateMixin(ClassNode classNode) {
|
||||
// isInner (shouldn't) return true for static inner classes
|
||||
if (this.classInfo.isInner()) {
|
||||
if (!this.classInfo.isProbablyStatic()) {
|
||||
throw new InvalidMixinException(this, "Inner class mixin must be declared static");
|
||||
}
|
||||
|
||||
@@ -345,6 +352,22 @@ class MixinInfo extends TreeInfo implements Comparable<MixinInfo>, IMixinInfo {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Read inner class definitions for the class and locate any synthetic inner
|
||||
* classes so that we can add them to the passthrough set in our parent
|
||||
* config.
|
||||
*/
|
||||
private void readInnerClasses(ClassNode classNode) {
|
||||
for (InnerClassNode inner : classNode.innerClasses) {
|
||||
if (inner.outerName.equals(this.classInfo.getName())) {
|
||||
ClassInfo innerClass = ClassInfo.forName(inner.name);
|
||||
if (innerClass.isSynthetic() && innerClass.isProbablyStatic()) {
|
||||
this.syntheticInnerClasses.add(inner.name);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Let's do this
|
||||
*/
|
||||
@@ -467,6 +490,13 @@ class MixinInfo extends TreeInfo implements Comparable<MixinInfo>, IMixinInfo {
|
||||
return this.targetClassNames;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the synthetic inner classes for this mixin
|
||||
*/
|
||||
public Set<String> getSyntheticInnerClasses() {
|
||||
return Collections.unmodifiableSet(this.syntheticInnerClasses);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the target class list for this mixin
|
||||
*/
|
||||
|
||||
@@ -174,7 +174,7 @@ public class MixinTransformer extends TreeTransformer {
|
||||
* Mixin configuration bundle
|
||||
*/
|
||||
private final List<MixinConfig> configs = new ArrayList<MixinConfig>();
|
||||
|
||||
|
||||
/**
|
||||
* True once initialisation is done. All mixin configs needs to be
|
||||
* initialised as late as possible in startup (so that other transformers
|
||||
@@ -234,7 +234,7 @@ public class MixinTransformer extends TreeTransformer {
|
||||
*/
|
||||
@Override
|
||||
public byte[] transform(String name, String transformedName, byte[] basicClass) {
|
||||
if (basicClass == null) {
|
||||
if (basicClass == null || transformedName == null) {
|
||||
return basicClass;
|
||||
}
|
||||
|
||||
@@ -253,7 +253,11 @@ public class MixinTransformer extends TreeTransformer {
|
||||
SortedSet<MixinInfo> mixins = null;
|
||||
|
||||
for (MixinConfig config : this.configs) {
|
||||
if (transformedName != null && transformedName.startsWith(config.getMixinPackage())) {
|
||||
if (config.packageMatch(transformedName)) {
|
||||
if (config.canPassThrough(transformedName)) {
|
||||
return this.passThrough(name, transformedName, basicClass);
|
||||
}
|
||||
|
||||
throw new NoClassDefFoundError(String.format("%s is a mixin class and cannot be referenced directly", transformedName));
|
||||
}
|
||||
|
||||
@@ -349,6 +353,39 @@ public class MixinTransformer extends TreeTransformer {
|
||||
this.logger.warn("Re-entrance detected from unknown source, this will cause serious problems.", new RuntimeException());
|
||||
}
|
||||
|
||||
/**
|
||||
* "Pass through" a synthetic inner class. Transforms package-private
|
||||
* members in the class into public so that they are accessible from their
|
||||
* new home in the target class
|
||||
*
|
||||
* @param name original class name
|
||||
* @param transformedName deobfuscated class name
|
||||
* @param basicClass class bytecode
|
||||
* @return public-ified class bytecode
|
||||
*/
|
||||
private byte[] passThrough(String name, String transformedName, byte[] basicClass) {
|
||||
ClassNode passThroughClass = this.readClass(basicClass, true);
|
||||
|
||||
// Make the class public
|
||||
passThroughClass.access |= Opcodes.ACC_PUBLIC;
|
||||
|
||||
// Make package-private fields public
|
||||
for (FieldNode field : passThroughClass.fields) {
|
||||
if ((field.access & (Opcodes.ACC_PRIVATE | Opcodes.ACC_PROTECTED)) == 0) {
|
||||
field.access |= Opcodes.ACC_PUBLIC;
|
||||
}
|
||||
}
|
||||
|
||||
// Make package-private methods public
|
||||
for (MethodNode method : passThroughClass.methods) {
|
||||
if ((method.access & (Opcodes.ACC_PRIVATE | Opcodes.ACC_PROTECTED)) == 0) {
|
||||
method.access |= Opcodes.ACC_PUBLIC;
|
||||
}
|
||||
}
|
||||
|
||||
return this.writeClass(transformedName, passThroughClass);
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply mixins for specified target class to the class described by the
|
||||
* supplied byte array.
|
||||
@@ -375,6 +412,10 @@ public class MixinTransformer extends TreeTransformer {
|
||||
targetClass.accept(new CheckClassAdapter(new ClassWriter(ClassWriter.COMPUTE_FRAMES)));
|
||||
}
|
||||
|
||||
return this.writeClass(transformedName, targetClass);
|
||||
}
|
||||
|
||||
private byte[] writeClass(String transformedName, ClassNode targetClass) {
|
||||
// Collapse tree to bytes
|
||||
byte[] bytes = this.writeClass(targetClass);
|
||||
|
||||
|
||||
Reference in new issue
Block a user