Support private mixin targets, AP improvements & minor fixes, closes #7

This commit is contained in:
Mumfrey committed 2015-01-18 00:05:51 +00:00
1 parent 2ef7cad8a9
commit 0f27299597
17 files changed
+566 -113

No files matched your search

@@ -39,7 +39,14 @@ public @interface Mixin {
/**
* Target class(es) for this mixin
*/
public Class<?>[] value();
public Class<?>[] value() default { };
/**
* Since specifying targets in {@link #value} requires that the classes be publicly visible, this property is provided to allow package-private,
* anonymous innner, and private inner classes to be referenced. Referencing an otherwise public class using this property is an error condition
* and will throw an exception at runtime. It is completely fine to specify both public and private targets for the same mixin however.
*/
public String[] targets() default { };
/**
* Priority for the mixin, relative to other mixins targetting the same classes
@@ -132,7 +132,7 @@ public abstract class InjectionInfo {
MemberInfo targetMember = MemberInfo.parse(method, this.mixin);
if (targetMember.owner != null && targetMember.owner.equals(this.mixin.getTargetClassRef())) {
if (targetMember.owner != null && !targetMember.owner.equals(this.mixin.getTargetClassRef())) {
throw new InvalidInjectionException(type + " annotation on " + this.method.name + " specifies a target class '"
+ targetMember.owner + "', which is not supported");
}
@@ -32,6 +32,7 @@ import java.util.Map;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.tree.ClassNode;
import org.objectweb.asm.tree.FieldNode;
import org.objectweb.asm.tree.MethodNode;
import com.google.common.collect.ImmutableList;
@@ -65,6 +66,11 @@ class ClassInfo extends TreeInfo {
*/
private final String superName;
/**
* Outer class name
*/
private final String outerName;
/**
* Interfaces
*/
@@ -75,15 +81,25 @@ class ClassInfo extends TreeInfo {
*/
private final List<String> methods;
/**
* True if this is an interface
*/
private final boolean isInterface;
/**
* Access flags
*/
private final int access;
/**
* Superclass reference, not initialised until required
*/
private ClassInfo superClass;
/**
* True if this is an interface
* Outer class reference, not initialised until required
*/
private boolean isInterface;
private ClassInfo outerClass;
/**
* Private constructor used to initialise the ClassInfo for {@link Object}
@@ -91,6 +107,7 @@ class ClassInfo extends TreeInfo {
private ClassInfo() {
this.name = ClassInfo.JAVA_LANG_OBJECT;
this.superName = null;
this.outerName = null;
this.methods = ImmutableList.<String>of(
"getClass()Ljava/lang/Class;",
"hashCode()I",
@@ -106,6 +123,7 @@ class ClassInfo extends TreeInfo {
);
this.isInterface = false;
this.interfaces = Collections.<String>emptyList();
this.access = Opcodes.ACC_PUBLIC;
}
/**
@@ -119,6 +137,7 @@ class ClassInfo extends TreeInfo {
this.methods = new ArrayList<String>();
this.isInterface = ((classNode.access & Opcodes.ACC_INTERFACE) != 0);
this.interfaces = Collections.unmodifiableList(classNode.interfaces);
this.access = classNode.access;
for (MethodNode method : classNode.methods) {
if (!method.name.startsWith("<")
@@ -127,6 +146,34 @@ class ClassInfo extends TreeInfo {
this.methods.add(method.name + method.desc);
}
}
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);
}
}
}
}
this.outerName = outerName ;
}
/**
* Get whether this class has ACC_PUBLIC
*/
public boolean isPublic() {
return (this.access & Opcodes.ACC_PUBLIC) != 0;
}
/**
* Get whether this class is an inner class
*/
public boolean isInner() {
return this.outerName != null;
}
/**
@@ -143,6 +190,15 @@ class ClassInfo extends TreeInfo {
return this.interfaces;
}
@Override
public String toString() {
return this.name;
}
public int getAccess() {
return this.access;
}
/**
* Get the class name (binary name)
*/
@@ -168,6 +224,24 @@ class ClassInfo extends TreeInfo {
return this.superClass;
}
/**
* Get the name of the outer class, or null if this is not an inner class
*/
public String getOuterName() {
return this.outerName;
}
/**
* Get the outer class info, can return null if the outer class cannot be resolved or if this is not an inner class
*/
public ClassInfo getOuterClass() {
if (this.outerClass == null && this.outerName != null) {
this.outerClass = ClassInfo.forName(this.outerName);
}
return this.outerClass;
}
/**
* Test whether this class has the specified superclass in its hierarchy
*
@@ -239,6 +313,25 @@ class ClassInfo extends TreeInfo {
return this.methods.contains(name + desc);
}
/* (non-Javadoc)
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object other) {
if (!(other instanceof ClassInfo)) {
return false;
}
return ((ClassInfo)other).name.equals(this.name);
}
/* (non-Javadoc)
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
return this.name.hashCode();
}
/**
* Return a ClassInfo for the supplied {@link ClassNode}. If a ClassInfo for the class was already defined, then the original ClassInfo is
* returned from the internal cache. Otherwise a new ClassInfo is created and returned.
@@ -108,7 +108,7 @@ class MixinConfig implements Comparable<MixinConfig> {
/**
* True to set the sourceFile property when applying mixins
*/
@SerializedName("referenceMap")
@SerializedName("refmap")
private String refMapperConfig;
/**
@@ -240,7 +240,7 @@ class MixinConfig implements Comparable<MixinConfig> {
if (mixin.getTargetClasses().size() > 0) {
MixinConfig.globalMixinList.add(mixinClass);
for (String targetClass : mixin.getTargetClasses()) {
this.mixinsFor(targetClass).add(mixin);
this.mixinsFor(targetClass.replace('/', '.')).add(mixin);
}
}
} catch (Exception ex) {
@@ -40,6 +40,10 @@ import org.spongepowered.asm.mixin.extensibility.IMixinConfigPlugin;
import org.spongepowered.asm.mixin.extensibility.IMixinInfo;
import org.spongepowered.asm.util.ASMHelper;
import com.google.common.base.Function;
import com.google.common.base.Functions;
import com.google.common.collect.Lists;
/**
* Runtime information bundle about a mixin
*/
@@ -83,7 +87,12 @@ class MixinInfo extends TreeInfo implements Comparable<MixinInfo>, IMixinInfo {
/**
* Mixin targets, read from the {@link Mixin} annotation on the mixin class
*/
private final List<String> targetClasses;
private final List<ClassInfo> targetClasses;
/**
* Names of target classes
*/
private final List<String> targetClassNames;
/**
* True if the superclass of the mixin is <b>not</b> the direct superclass of one or more targets
@@ -126,12 +135,30 @@ class MixinInfo extends TreeInfo implements Comparable<MixinInfo>, IMixinInfo {
this.mixinBytes = this.loadMixinClass(this.className, runTransformers);
ClassNode classNode = this.getClassNode(0);
this.classInfo = ClassInfo.fromClassNode(classNode);
this.classInfo = this.readInfo(classNode);
this.priority = this.readPriority(classNode);
this.targetClasses = this.readTargetClasses(classNode, suppressPlugin);
this.targetClassNames = Collections.unmodifiableList(Lists.transform(this.targetClasses, Functions.toStringFunction()));
this.detachedSuper = this.validateTargetClasses(classNode);
}
/**
* Initialises the class info and performs some pre-flight checks on the mixin
*
* @param classNode
* @return
*/
private ClassInfo readInfo(ClassNode classNode) {
ClassInfo classInfo = ClassInfo.fromClassNode(classNode);
// isInner (shouldn't) return true for static inner classes
if (classInfo.isInner()) {
throw new InvalidMixinException("Inner class mixin must be declared static");
}
return classInfo;
}
/**
* Read the target class names from the {@link Mixin} annotation
*
@@ -139,24 +166,52 @@ class MixinInfo extends TreeInfo implements Comparable<MixinInfo>, IMixinInfo {
* @param suppressPlugin
* @return
*/
private List<String> readTargetClasses(ClassNode classNode, boolean suppressPlugin) {
private List<ClassInfo> readTargetClasses(ClassNode classNode, boolean suppressPlugin) {
AnnotationNode mixin = ASMHelper.getInvisibleAnnotation(classNode, Mixin.class);
if (mixin == null) {
throw new InvalidMixinException(String.format("The mixin '%s' is missing an @Mixin annotation", this.className));
}
List<Type> targetClasses = ASMHelper.getAnnotationValue(mixin);
List<String> targetClassNames = new ArrayList<String>();
for (Type targetClass : targetClasses) {
String targetClassName = targetClass.getClassName();
if (this.plugin == null || suppressPlugin || this.plugin.shouldApplyMixin(targetClassName, this.className)) {
targetClassNames.add(targetClassName);
}
List<ClassInfo> targetClassNames = new ArrayList<ClassInfo>();
List<Type> publicTargets = ASMHelper.getAnnotationValue(mixin, "value");
List<String> privateTargets = ASMHelper.getAnnotationValue(mixin, "targets");
if (publicTargets != null) {
this.readTargets(targetClassNames, Lists.transform(publicTargets, new Function<Type, String>() {
@Override
public String apply(Type input) {
return input.getClassName();
};
}), suppressPlugin, false);
}
if (privateTargets != null) {
this.readTargets(targetClassNames, privateTargets, suppressPlugin, true);
}
return targetClassNames;
}
/**
* Reads a target list into the outTargets list
*/
private void readTargets(List<ClassInfo> outTargets, List<String> inTargets, boolean suppressPlugin, boolean checkPublic) {
for (String targetClassName : inTargets) {
if (this.plugin == null || suppressPlugin || this.plugin.shouldApplyMixin(targetClassName, this.className)) {
ClassInfo targetInfo = ClassInfo.forName(targetClassName);
if (targetInfo.isInterface()) {
throw new InvalidMixinException("@Mixin target " + targetClassName + " is an interface in " + this);
}
if (checkPublic && targetInfo.isPublic()) {
throw new InvalidMixinException("@Mixin target " + targetClassName + " is public in " + this + " and must be specified in value");
}
if (!outTargets.contains(targetInfo)) {
outTargets.add(targetInfo);
}
}
}
}
/**
* Read the priority from the {@link Mixin} annotation
*
@@ -176,8 +231,7 @@ class MixinInfo extends TreeInfo implements Comparable<MixinInfo>, IMixinInfo {
private boolean validateTargetClasses(ClassNode classNode) {
boolean detached = false;
for (String targetClassName : this.targetClasses) {
ClassInfo targetClass = ClassInfo.forName(targetClassName);
for (ClassInfo targetClass : this.targetClasses) {
if (classNode.superName.equals(targetClass.getSuperName())) {
continue;
@@ -185,7 +239,7 @@ class MixinInfo extends TreeInfo implements Comparable<MixinInfo>, IMixinInfo {
if (!targetClass.hasSuperClass(classNode.superName)) {
throw new InvalidMixinException("Super class '" + classNode.superName.replace('/', '.') + "' of " + this.name
+ " was not found in the hierarchy of target class '" + targetClassName + "'");
+ " was not found in the hierarchy of target class '" + targetClass + "'");
}
detached = true;
@@ -254,7 +308,7 @@ class MixinInfo extends TreeInfo implements Comparable<MixinInfo>, IMixinInfo {
*/
@Override
public List<String> getTargetClasses() {
return Collections.unmodifiableList(this.targetClasses);
return this.targetClassNames;
}
/**
@@ -60,7 +60,6 @@ import org.spongepowered.asm.mixin.MixinEnvironment;
import org.spongepowered.asm.mixin.Overwrite;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.extensibility.IMixinConfigPlugin;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.struct.InjectionInfo;
import org.spongepowered.asm.transformers.TreeTransformer;
import org.spongepowered.asm.util.ASMHelper;
@@ -45,9 +45,11 @@ import com.google.common.collect.ImmutableSet;
*/
abstract class TreeInfo {
private static final Set<String> reEntrantTransformers = ImmutableSet.<String>of(
private static final Set<String> excludeTransformers = ImmutableSet.<String>of(
"net.minecraftforge.fml.common.asm.transformers.EventSubscriptionTransformer",
"cpw.mods.fml.common.asm.transformers.EventSubscriptionTransformer"
"cpw.mods.fml.common.asm.transformers.EventSubscriptionTransformer",
"net.minecraftforge.fml.common.asm.transformers.TerminalTransformer",
"cpw.mods.fml.common.asm.transformers.TerminalTransformer"
);
private static IClassNameTransformer nameTransformer;
@@ -126,13 +128,13 @@ abstract class TreeInfo {
*
* @param name
* @param basicClass
* @return class bytecode after processing by all registered transformers except re-entrant transformers
* @return class bytecode after processing by all registered transformers except the excluded transformers
*/
private static byte[] applyTransformers(String name, byte[] basicClass) {
final List<IClassTransformer> transformers = Launch.classLoader.getTransformers();
for (final IClassTransformer transformer : transformers) {
if (!(transformer instanceof MixinTransformer) && !TreeInfo.reEntrantTransformers.contains(transformer.getClass().getName())) {
if (!(transformer instanceof MixinTransformer) && !TreeInfo.excludeTransformers.contains(transformer.getClass().getName())) {
basicClass = transformer.transform(name, name, basicClass);
}
}