diff --git a/src/main/java/org/spongepowered/asm/mixin/transformer/ClassInfo.java b/src/main/java/org/spongepowered/asm/mixin/transformer/ClassInfo.java index d2a0f8ea..4707f1cd 100644 --- a/src/main/java/org/spongepowered/asm/mixin/transformer/ClassInfo.java +++ b/src/main/java/org/spongepowered/asm/mixin/transformer/ClassInfo.java @@ -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.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 */ diff --git a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinConfig.java b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinConfig.java index d38e57c0..ca29e119 100644 --- a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinConfig.java +++ b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinConfig.java @@ -79,6 +79,11 @@ class MixinConfig implements Comparable { */ private final transient List mixins = new ArrayList(); + /** + * Synthetic inner classes for mixins in this set + */ + private final transient Set syntheticInnerClasses = new HashSet(); + /** * Minimum version of the mixin subsystem required to correctly apply mixins * in this configuration. @@ -285,6 +290,9 @@ class MixinConfig implements Comparable { 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 { 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 diff --git a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinInfo.java b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinInfo.java index da18845a..8eb4a5b8 100644 --- a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinInfo.java +++ b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinInfo.java @@ -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, IMixinInfo { */ private final transient List softImplements = new ArrayList(); + /** + * Synthetic inner classes + */ + private final transient Set syntheticInnerClasses = new HashSet(); + /** * Initial ClassNode created for mixin validation, not used for actual * application @@ -183,6 +189,7 @@ class MixinInfo extends TreeInfo implements Comparable, 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, 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, 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, IMixinInfo { return this.targetClassNames; } + /** + * Get the synthetic inner classes for this mixin + */ + public Set getSyntheticInnerClasses() { + return Collections.unmodifiableSet(this.syntheticInnerClasses); + } + /** * Get the target class list for this mixin */ diff --git a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinTransformer.java b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinTransformer.java index e3b83b8f..c642b973 100644 --- a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinTransformer.java +++ b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinTransformer.java @@ -174,7 +174,7 @@ public class MixinTransformer extends TreeTransformer { * Mixin configuration bundle */ private final List configs = new ArrayList(); - + /** * 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 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);