Initial impl of Annotation Processor, references #4

This commit is contained in:
Mumfrey committed 2015-01-04 18:20:57 +00:00
1 parent e3211bbfee
commit 9bdcf084ca
17 files changed
+1565 -24

No files matched your search

@@ -81,7 +81,8 @@ public @interface At {
public String[] args() default { };
/**
* Target member used by INVOKE, INVOKE_STRING and FIELD
* Target member used by INVOKE, INVOKE_STRING and FIELD. This <b>must be specified as a fully-qualified member path</b> including the class name
* and signature. Failing to fully-qualify the target member will result in an error at obfuscation time.
*/
public String target() default "";
@@ -42,6 +42,7 @@ import org.spongepowered.asm.mixin.injection.points.BeforeStringInvoke;
import org.spongepowered.asm.mixin.injection.points.JumpInsnPoint;
import org.spongepowered.asm.mixin.injection.points.MethodHead;
import org.spongepowered.asm.mixin.injection.struct.InjectionPointData;
import org.spongepowered.asm.mixin.transformer.MixinData;
import org.spongepowered.asm.util.ASMHelper;
import com.google.common.base.Joiner;
@@ -262,14 +263,14 @@ public abstract class InjectionPoint {
/**
* Parse an InjectionPoint from the supplied {@link At} annotation
*/
public static InjectionPoint parse(At at) {
return InjectionPoint.parse(at.value(), at.shift(), at.by(), Arrays.asList(at.args()), at.target(), at.ordinal(), at.opcode());
public static InjectionPoint parse(MixinData mixin, At at) {
return InjectionPoint.parse(mixin, at.value(), at.shift(), at.by(), Arrays.asList(at.args()), at.target(), at.ordinal(), at.opcode());
}
/**
* Parse an InjectionPoint from the supplied {@link At} annotation supplied as an AnnotationNode instance
*/
public static InjectionPoint parse(AnnotationNode node) {
public static InjectionPoint parse(MixinData mixin, AnnotationNode node) {
String at = ASMHelper.<String>getAnnotationValue(node, "value");
List<String> args = ASMHelper.<List<String>>getAnnotationValue(node, "args");
String target = ASMHelper.<String>getAnnotationValue(node, "target", "");
@@ -282,14 +283,14 @@ public abstract class InjectionPoint {
args = ImmutableList.<String>of();
}
return InjectionPoint.parse(at, shift, by, args, target, ordinal, opcode);
return InjectionPoint.parse(mixin, at, shift, by, args, target, ordinal, opcode);
}
/**
* Parse and instantiate an InjectionPoint from the supplied information. Returns null if an InjectionPoint could not be created.
*/
public static InjectionPoint parse(String at, At.Shift shift, int by, List<String> args, String target, int ordinal, int opcode) {
InjectionPointData data = new InjectionPointData(args, target, ordinal, opcode);
public static InjectionPoint parse(MixinData mixin, String at, At.Shift shift, int by, List<String> args, String target, int ordinal, int opcode) {
InjectionPointData data = new InjectionPointData(mixin, args, target, ordinal, opcode);
InjectionPoint point = null;
if (BeforeFieldAccess.CODE.equals(at)) {
@@ -38,6 +38,7 @@ import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.InjectionPoint;
import org.spongepowered.asm.mixin.injection.InvalidInjectionException;
import org.spongepowered.asm.mixin.injection.callback.CallbackInjector;
import org.spongepowered.asm.mixin.transformer.MixinData;
import org.spongepowered.asm.util.ASMHelper;
@@ -55,7 +56,7 @@ public class InjectionInfo {
* Annotated method
*/
private final MethodNode method;
/**
* Annotated method is static
*/
@@ -93,17 +94,17 @@ public class InjectionInfo {
* @param method Method
* @param injectAnnotation Annotation to parse
*/
public InjectionInfo(ClassNode classNode, MethodNode method, AnnotationNode injectAnnotation) {
public InjectionInfo(ClassNode classNode, MethodNode method, MixinData mixin, AnnotationNode injectAnnotation) {
this.classNode = classNode;
this.method = method;
this.isStatic = ASMHelper.methodIsStatic(method);
this.parse(injectAnnotation);
this.parse(mixin, injectAnnotation);
}
/**
* Parse the info from the supplied annotation
*/
private void parse(AnnotationNode injectAnnotation) {
private void parse(MixinData mixin, AnnotationNode injectAnnotation) {
if (injectAnnotation == null) {
return;
}
@@ -118,9 +119,9 @@ public class InjectionInfo {
throw new InvalidInjectionException("@Inject annotation on " + this.method.name + " is missing 'at' value(s)");
}
MemberInfo targetMember = MemberInfo.parse(method);
MemberInfo targetMember = MemberInfo.parse(method, mixin);
if (targetMember.owner != null) {
if (targetMember.owner != null && targetMember.owner.equals(mixin.getTargetClassRef())) {
throw new InvalidInjectionException("@Inject annotation on " + this.method.name + " specifies a target class '" + targetMember.owner
+ "', which is not supported");
}
@@ -128,14 +129,14 @@ public class InjectionInfo {
this.findMethods(targetMember);
if (this.targets.size() == 0) {
throw new InvalidInjectionException("@Inject annotation on " + this.method.name + " could not find specified method '" + method + "'");
throw new InvalidInjectionException("@Inject annotation on " + this.method.name + " could not find '" + targetMember.name + "'");
}
this.cancellable = ASMHelper.<Boolean>getAnnotationValue(injectAnnotation, "cancellable", false);
this.captureLocals = ASMHelper.<Boolean>getAnnotationValue(injectAnnotation, "captureLocals", false);
for (AnnotationNode at : ats) {
InjectionPoint injectionPoint = InjectionPoint.parse(at);
InjectionPoint injectionPoint = InjectionPoint.parse(mixin, at);
if (injectionPoint != null) {
this.injectionPoints.add(injectionPoint);
}
@@ -29,6 +29,7 @@ import java.util.List;
import java.util.Map;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.transformer.MixinData;
/**
@@ -41,6 +42,11 @@ public class InjectionPointData {
*/
private final Map<String, String> args = new HashMap<String, String>();
/**
* Mixin
*/
private final MixinData mixin;
/**
* Target
*/
@@ -56,7 +62,8 @@ public class InjectionPointData {
*/
private final int opcode;
public InjectionPointData(List<String> args, String target, int ordinal, int opcode) {
public InjectionPointData(MixinData mixin, List<String> args, String target, int ordinal, int opcode) {
this.mixin = mixin;
this.target = target;
this.ordinal = Math.max(-1, ordinal);
this.opcode = opcode;
@@ -95,7 +102,7 @@ public class InjectionPointData {
}
public MemberInfo get(String key) {
return MemberInfo.parse(this.get(key, ""));
return MemberInfo.parse(this.get(key, ""), this.mixin);
}
private int parseInt(String string, int defaultValue) {
@@ -115,7 +122,7 @@ public class InjectionPointData {
}
public MemberInfo getTarget() {
return MemberInfo.parse(this.target);
return MemberInfo.parse(this.target, this.mixin);
}
public int getOrdinal() {
@@ -24,9 +24,12 @@
*/
package org.spongepowered.asm.mixin.injection.struct;
import net.minecraftforge.srg2source.rangeapplier.MethodData;
import org.objectweb.asm.tree.AbstractInsnNode;
import org.objectweb.asm.tree.FieldInsnNode;
import org.objectweb.asm.tree.MethodInsnNode;
import org.spongepowered.asm.mixin.transformer.MixinData;
/**
@@ -137,12 +140,69 @@ public class MemberInfo {
}
}
/**
* Initialise a MemberInfo using the supplied MethodData object
*/
public MemberInfo(MethodData methodData) {
int slashPos = methodData.name.lastIndexOf('/');
this.owner = methodData.name.substring(0, slashPos);
this.name = methodData.name.substring(slashPos + 1);
this.desc = methodData.sig;
this.matchAll = false;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return String.format("[OWNER=%s,NAME=%s,DESC=%s,ALL=%s]", this.owner, this.name, this.desc, this.matchAll);
String owner = this.owner != null ? "L" + this.owner + ";" : "";
String name = this.name != null ? this.name : "";
String qualifier = this.matchAll ? "*" : "";
String desc = this.desc != null ? this.desc : "";
String separator = desc.startsWith("(") ? "" : (this.desc != null ? ":" : "");
return owner + name + qualifier + separator + desc;
}
/**
* Return this MemberInfo as an SRG mapping
*/
public String toSrg() {
if (!this.isFullyQualified()) {
throw new RuntimeException("Cannot convert unqalified reference to SRG mapping");
}
if (this.desc.startsWith("(")) {
return this.owner + "/" + this.name + " " + this.desc;
}
return this.owner + "/" + this.name;
}
public MethodData asMethodData() {
if (!this.isFullyQualified()) {
throw new RuntimeException("Cannot convert unqalified reference to MethodData");
}
if (this.isField()) {
throw new RuntimeException("Cannot convert a non-method reference to MethodData");
}
return new MethodData(this.owner + "/" + this.name, this.desc);
}
/**
* Get whether this reference is fully qualified
*/
public boolean isFullyQualified() {
return this.owner != null && this.name != null && this.desc != null;
}
/**
* Get whether this MemberInfo is definitely a field, the output of this method is undefined if {@link #isFullyQualified} returns false
*/
public boolean isField() {
return this.desc != null && !this.desc.startsWith("(");
}
/**
@@ -188,9 +248,36 @@ public class MemberInfo {
* Parse a MemberInfo from a string
*/
public static MemberInfo parse(String name) {
return MemberInfo.parse(name, null, null);
}
/**
* Parse a MemberInfo from a string
*/
public static MemberInfo parse(String name, MixinData mixin) {
return MemberInfo.parse(name, mixin.getReferenceMapper(), mixin.getClassRef());
}
/**
* Parse a MemberInfo from a string
*/
public static MemberInfo parse(String name, ReferenceMapper refMapper, String mixinClass) {
String desc = null;
String owner = null;
String n = name;
if (refMapper != null) {
name = refMapper.remap(mixinClass, name);
}
System.err.println("==============================================================================");
System.err.println("==============================================================================");
System.err.println("==============================================================================");
System.err.println("Remapped [" + n + "] to [" + name + "] for " + mixinClass);
System.err.println("==============================================================================");
System.err.println("==============================================================================");
System.err.println("==============================================================================");
int lastDotPos = name.lastIndexOf('.');
int semiColonPos = name.indexOf(';');
if (lastDotPos > -1) {
@@ -207,7 +294,7 @@ public class MemberInfo {
desc = name.substring(parenPos);
name = name.substring(0, parenPos);
} else if (colonPos > -1) {
desc = name.substring(colonPos);
desc = name.substring(colonPos + 1);
name = name.substring(0, colonPos);
}
@@ -222,4 +309,15 @@ public class MemberInfo {
return new MemberInfo(name, owner, desc, matchAll);
}
public static MemberInfo fromSrgField(String srgName, String desc) {
int slashPos = srgName.lastIndexOf('/');
String owner = srgName.substring(0, slashPos);
String name = srgName.substring(slashPos + 1);
return new MemberInfo(name, owner, desc, false);
}
public static MemberInfo fromSrgMethod(MethodData methodData) {
return new MemberInfo(methodData);
}
}
@@ -0,0 +1,103 @@
/*
* This file is part of Sponge, licensed under the MIT License (MIT).
*
* Copyright (c) SpongePowered.org <http://www.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.injection.struct;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
public final class ReferenceMapper implements Serializable {
private static final long serialVersionUID = 1L;
public static final String DEFAULT_RESOURCE = "mixin.refmap.json";
private final Map<String, Map<String, String>> mappings;
public ReferenceMapper() {
this(new HashMap<String, Map<String, String>>());
}
public ReferenceMapper(Map<String, Map<String, String>> mappings) {
this.mappings = mappings;
}
public String remap(String className, String reference) {
if (className == null) {
for (Map<String, String> mappings : this.mappings.values()) {
if (mappings.containsKey(reference)) {
return mappings.get(reference);
}
}
}
Map<String, String> classMappings = this.mappings.get(className);
if (classMappings == null) {
return reference;
}
return classMappings.get(reference);
}
public void addMapping(String className, String reference, String newReference) {
Map<String, String> classMappings = this.mappings.get(className);
if (classMappings == null) {
classMappings = new HashMap<String, String>();
this.mappings.put(className, classMappings);
}
classMappings.put(reference, newReference);
}
public void write(Appendable writer) {
new GsonBuilder().setPrettyPrinting().create().toJson(this, writer);
}
public static ReferenceMapper read(String resource) {
Reader reader = null;
try {
reader = new InputStreamReader(ReferenceMapper.class.getResourceAsStream("/" + resource));
return ReferenceMapper.read(reader);
} catch (Exception ex) {
return new ReferenceMapper();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException ex) {
// don't really care
}
}
}
}
public static ReferenceMapper read(Reader reader) {
return new Gson().fromJson(reader, ReferenceMapper.class);
}
}
@@ -30,6 +30,8 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.spongepowered.asm.mixin.injection.struct.ReferenceMapper;
import com.google.gson.Gson;
import com.google.gson.annotations.SerializedName;
@@ -62,6 +64,17 @@ class MixinConfig {
@SerializedName("setSourceFile")
private boolean setSourceFile = false;
/**
* True to set the sourceFile property when applying mixins
*/
@SerializedName("referenceMap")
private String refMapperConfig;
/**
* Reference mapper for injectors
*/
private transient ReferenceMapper refMapper;
/**
* Keep track of initialisation state
*/
@@ -98,6 +111,12 @@ class MixinConfig {
ex.printStackTrace();
}
}
if (this.refMapperConfig == null) {
this.refMapperConfig = ReferenceMapper.DEFAULT_RESOURCE;
}
this.refMapper = ReferenceMapper.read(this.refMapperConfig);
}
/**
@@ -121,6 +140,13 @@ class MixinConfig {
return this.setSourceFile;
}
/**
* Get the reference remapper for injectors
*/
public ReferenceMapper getReferenceMapper() {
return this.refMapper;
}
/**
* Check whether this configuration bundle has a mixin for the specified class
*
@@ -39,7 +39,9 @@ import org.objectweb.asm.tree.ClassNode;
import org.objectweb.asm.tree.MethodInsnNode;
import org.objectweb.asm.tree.MethodNode;
import org.spongepowered.asm.mixin.Implements;
import org.spongepowered.asm.mixin.InvalidMixinException;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.struct.ReferenceMapper;
import org.spongepowered.asm.util.ASMHelper;
@@ -91,6 +93,31 @@ public class MixinData {
public ClassNode getClassNode() {
return this.classNode;
}
/**
* Get the mixin class name
*/
public String getClassName() {
return this.info.getClassName();
}
/**
* Get the internal mixin class name
*/
public String getClassRef() {
return this.info.getClassRef();
}
/**
* Get the target class reference
*/
public String getTargetClassRef() {
List<String> targetClasses = this.info.getTargetClasses();
if (targetClasses.size() != 1) {
throw new InvalidMixinException("Multiple targets found for " + this.getClassName() + " but exactly one is required");
}
return targetClasses.get(0);
}
/**
* Get all interfaces for this mixin
@@ -105,6 +132,13 @@ public class MixinData {
public boolean shouldSetSourceFile() {
return this.info.getParent().shouldSetSourceFile();
}
/**
* Get the reference mapper for this mixin
*/
public ReferenceMapper getReferenceMapper() {
return this.info.getParent().getReferenceMapper();
}
/**
* Prepare the mixin, applies any pre-processing transformations
@@ -390,7 +390,7 @@ public class MixinTransformer extends TreeTransformer {
continue;
}
InjectionInfo injectInfo = new InjectionInfo(targetClass, method, injectAnnotation);
InjectionInfo injectInfo = new InjectionInfo(targetClass, method, mixin, injectAnnotation);
if (injectInfo.isValid()) {
injectInfo.inject();
}