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 4d7bd747..535287a0 100644 --- a/src/main/java/org/spongepowered/asm/mixin/transformer/ClassInfo.java +++ b/src/main/java/org/spongepowered/asm/mixin/transformer/ClassInfo.java @@ -27,8 +27,10 @@ package org.spongepowered.asm.mixin.transformer; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; +import java.util.HashSet; import java.util.List; import java.util.Map; +import java.util.Set; import org.objectweb.asm.Opcodes; import org.objectweb.asm.tree.ClassNode; @@ -81,6 +83,26 @@ class ClassInfo extends TreeInfo { */ private final List methods; + /** + * Public and protected fields in this class + */ + private final List fields; + + /** + * Mixins which target this class + */ + private final Set mixins = new HashSet(); + + /** + * Mixin info if this class is a mixin itself + */ + private final MixinInfo mixin; + + /** + * True if this is a mixin rather than a class + */ + private final boolean isMixin; + /** * True if this is an interface */ @@ -121,9 +143,12 @@ class ClassInfo extends TreeInfo { "wait()V", "finalize()V" ); + this.fields = Collections.emptyList(); this.isInterface = false; this.interfaces = Collections.emptyList(); this.access = Opcodes.ACC_PUBLIC; + this.isMixin = false; + this.mixin = null; } /** @@ -135,9 +160,12 @@ class ClassInfo extends TreeInfo { this.name = classNode.name; this.superName = classNode.superName != null ? classNode.superName : ClassInfo.JAVA_LANG_OBJECT; this.methods = new ArrayList(); + this.fields = new ArrayList(); this.isInterface = ((classNode.access & Opcodes.ACC_INTERFACE) != 0); this.interfaces = Collections.unmodifiableList(classNode.interfaces); this.access = classNode.access; + this.isMixin = classNode instanceof MixinClassNode; + this.mixin = this.isMixin ? ((MixinClassNode)classNode).getMixin() : null; for (MethodNode method : classNode.methods) { if (!method.name.startsWith("<") @@ -150,16 +178,37 @@ class ClassInfo extends TreeInfo { String outerName = classNode.outerClass; if (outerName == null) { for (FieldNode field : classNode.fields) { - if ((field.access & Opcodes.ACC_SYNTHETIC) != 0 && field.name.startsWith("this$")) { - outerName = field.desc; - if (outerName.startsWith("L")) { - outerName = outerName.substring(1, outerName.length() - 1); + if ((field.access & Opcodes.ACC_SYNTHETIC) != 0) { + if (field.name.startsWith("this$")) { + outerName = field.desc; + if (outerName.startsWith("L")) { + outerName = outerName.substring(1, outerName.length() - 1); + } } + } else if ((field.access & Opcodes.ACC_PRIVATE) == 0) { + this.fields.add(field.name + "()" + field.desc); } } } - this.outerName = outerName ; + this.outerName = outerName; + } + + /** + * Add a mixin which targets this class + */ + public void addMixin(MixinInfo mixin) { + if (this.isMixin) { + throw new IllegalArgumentException("Cannot add target " + this.name + " for " + mixin.getClassName() + " because the target is a mixin"); + } + this.mixins.add(mixin); + } + + /** + * Get all mixins which target this class + */ + public Set getMixins() { + return Collections.unmodifiableSet(this.mixins); } /** @@ -242,6 +291,20 @@ class ClassInfo extends TreeInfo { return this.outerClass; } + /** + * Class targets + */ + protected List targets() { + if (this.mixin != null) { + List targets = new ArrayList(); + targets.add(this); + targets.addAll(this.mixin.getTargets()); + return targets; + } + + return ImmutableList.of(this); + } + /** * Test whether this class has the specified superclass in its hierarchy * @@ -252,13 +315,21 @@ class ClassInfo extends TreeInfo { return true; } + return this.hasSuperClass0(superClass); + } + + private boolean hasSuperClass0(String superClass) { ClassInfo superClassInfo = this.getSuperClass(); - while (superClassInfo != null) { + if (superClassInfo != null) { if (superClass.equals(superClassInfo.getName())) { return true; } - - superClassInfo = superClassInfo.getSuperClass(); + + for (ClassInfo superTarget : superClassInfo.targets()) { + if (superTarget.hasSuperClass0(superClass)) { + return true; + } + } } return false; @@ -269,13 +340,21 @@ class ClassInfo extends TreeInfo { return true; } + return this.isAssignableFrom0(superClass); + } + + private boolean isAssignableFrom0(ClassInfo superClass) { ClassInfo superClassInfo = this.getSuperClass(); - while (superClassInfo != null) { + if (superClassInfo != null) { if (superClass == superClassInfo) { return true; } - - superClassInfo = superClassInfo.getSuperClass(); + + for (ClassInfo superTarget : superClassInfo.targets()) { + if (superTarget.isAssignableFrom0(superClass)) { + return true; + } + } } return false; @@ -296,7 +375,12 @@ class ClassInfo extends TreeInfo { ClassInfo superClassInfo = this.getSuperClass(); if (superClassInfo != null) { - return superClassInfo.findMethodInHierarchy(name, desc, true); + for (ClassInfo superTarget : superClassInfo.targets()) { + String owner = superTarget.findMethodInHierarchy(name, desc, true); + if (owner != null) { + return owner; + } + } } return null; diff --git a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinClassNode.java b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinClassNode.java new file mode 100644 index 00000000..5038170d --- /dev/null +++ b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinClassNode.java @@ -0,0 +1,49 @@ +/* + * This file is part of Sponge, licensed under the MIT License (MIT). + * + * Copyright (c) SpongePowered.org + * Copyright (c) contributors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package org.spongepowered.asm.mixin.transformer; + +import org.objectweb.asm.Opcodes; +import org.objectweb.asm.tree.ClassNode; + +/** + * ClassNode for a MixinInfo + */ +public class MixinClassNode extends ClassNode { + + private final MixinInfo mixin; + + public MixinClassNode(MixinInfo mixin) { + this(Opcodes.ASM5, mixin); + } + + public MixinClassNode(int api, MixinInfo mixin) { + super(api); + this.mixin = mixin; + } + + public MixinInfo getMixin() { + return this.mixin; + } +} 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 e76f9f9a..c89feef0 100644 --- a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinInfo.java +++ b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinInfo.java @@ -31,6 +31,7 @@ import java.util.List; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; +import org.objectweb.asm.ClassReader; import org.objectweb.asm.Type; import org.objectweb.asm.tree.AnnotationNode; import org.objectweb.asm.tree.ClassNode; @@ -172,12 +173,12 @@ class MixinInfo extends TreeInfo implements Comparable, IMixinInfo { throw new InvalidMixinException(String.format("The mixin '%s' is missing an @Mixin annotation", this.className)); } - List targetClassNames = new ArrayList(); + List targets = new ArrayList(); List publicTargets = ASMHelper.getAnnotationValue(mixin, "value"); List privateTargets = ASMHelper.getAnnotationValue(mixin, "targets"); if (publicTargets != null) { - this.readTargets(targetClassNames, Lists.transform(publicTargets, new Function() { + this.readTargets(targets, Lists.transform(publicTargets, new Function() { @Override public String apply(Type input) { return input.getClassName(); @@ -186,10 +187,10 @@ class MixinInfo extends TreeInfo implements Comparable, IMixinInfo { } if (privateTargets != null) { - this.readTargets(targetClassNames, privateTargets, suppressPlugin, true); + this.readTargets(targets, privateTargets, suppressPlugin, true); } - return targetClassNames; + return targets; } /** @@ -207,6 +208,7 @@ class MixinInfo extends TreeInfo implements Comparable, IMixinInfo { } if (!outTargets.contains(targetInfo)) { outTargets.add(targetInfo); + targetInfo.addMixin(this); } } } @@ -300,17 +302,27 @@ class MixinInfo extends TreeInfo implements Comparable, IMixinInfo { */ @Override public ClassNode getClassNode(int flags) { - return TreeInfo.getClassNode(this.mixinBytes, flags); + MixinClassNode classNode = new MixinClassNode(this); + ClassReader classReader = new ClassReader(this.mixinBytes); + classReader.accept(classNode, flags); + return classNode; } /** - * Get the target classes for this mixin + * Get the target class names for this mixin */ @Override public List getTargetClasses() { return this.targetClassNames; } + /** + * Get the target class list for this mixin + */ + public List getTargets() { + return Collections.unmodifiableList(this.targetClasses); + } + /** * Get the mixin priority */