FieldDesc injector support

This commit is contained in:
Clisprail
2014-04-18 13:20:51 +02:00
parent bd3205c5cc
commit 045550093a
6 changed files with 33 additions and 16 deletions
@@ -23,6 +23,18 @@ public class ASMUtils implements Opcodes {
} }
return null; return null;
} }
public static FieldNode getField(ClassNode node, String fieldName, String desc) {
if(desc == null) {
return getField(node, fieldName);
}
for (final FieldNode fieldNode : node.fields) {
if (fieldNode.name.equals(fieldName) && fieldNode.desc.equals(desc)) {
return fieldNode;
}
}
return null;
}
public static ClassNode getClass(String className) { public static ClassNode getClass(String className) {
Context context = Context.getInstance(); Context context = Context.getInstance();
@@ -41,12 +41,14 @@ public class AddGetterAdapter implements Opcodes, Injectable {
* - return type of method, can be null for default return * - return type of method, can be null for default return
* @param staticMethod * @param staticMethod
* - pass true if you want the method to be static * - pass true if you want the method to be static
* @param multiplier - if this field requires a multiplier * @param multiplier
* - if this field requires a multipli
*/ */
public AddGetterAdapter(final ClassNode into, public AddGetterAdapter(final ClassNode into,
final ClassNode fieldLocation, final FieldNode fieldNode, final ClassNode fieldLocation, final FieldNode fieldNode,
final String methodName, final String returnDesc, final String methodName, final String returnDesc,
final boolean staticMethod, final long multiplier) { final boolean staticMethod, final long multiplier) {
Core.verbose("Injecting getter method: " + methodName);
this.into = into; this.into = into;
this.fieldLocation = fieldLocation; this.fieldLocation = fieldLocation;
this.fieldNode = fieldNode; this.fieldNode = fieldNode;
@@ -141,24 +143,23 @@ public class AddGetterAdapter implements Opcodes, Injectable {
} }
} }
if(multiplier != 0) { if (multiplier != 0) {
if(fieldNode.desc.equals("I") || fieldNode.desc.equals("S")) { if (fieldNode.desc.equals("I") || fieldNode.desc.equals("S")) {
method.visitInsn(I2L); method.visitInsn(I2L);
} }
method.visitLdcInsn(new Long(multiplier)); method.visitLdcInsn(new Long(multiplier));
method.visitInsn(LMUL); method.visitInsn(LMUL);
if(returnDesc.equals("I") || returnDesc.equals("S")) { if (returnDesc.equals("I") || returnDesc.equals("S")) {
method.visitInsn(L2I); method.visitInsn(L2I);
} }
if(returnDesc.equals("S")) { if (returnDesc.equals("S")) {
method.visitInsn(I2S); method.visitInsn(I2S);
} }
} else if (fieldNode.desc.equals("J") && returnDesc.equals("I")) { } else if (fieldNode.desc.equals("J") && returnDesc.equals("I")) {
method.visitInsn(L2I); method.visitInsn(L2I);
} else if(fieldNode.desc.equals("I") && returnDesc.equals("J")) { } else if (fieldNode.desc.equals("I") && returnDesc.equals("J")) {
method.visitInsn(I2L); method.visitInsn(I2L);
} }
method.visitInsn(ASMUtils.getReturnOpcode(returnDesc)); method.visitInsn(ASMUtils.getReturnOpcode(returnDesc));
method.visitMaxs(1, 1); method.visitMaxs(1, 1);
@@ -32,13 +32,15 @@ public class Getter implements Injectable {
* @param returnDesc - return type of method, can be null for default return * @param returnDesc - return type of method, can be null for default return
* @param staticMethod - pass true if you want the method to be static * @param staticMethod - pass true if you want the method to be static
* @param multiplier - if there is one, otherwise 0L * @param multiplier - if there is one, otherwise 0L
* @param fieldDesc - desc of the field, null if there are no duplicate field names
*/ */
public Getter(final String into, final String fieldLocation, final String fieldNode, public Getter(final String into, final String fieldLocation, final String fieldNode,
final String methodName, final String returnDesc, final boolean staticMethod, final long multiplier) { final String methodName, final String returnDesc, final boolean staticMethod, final long multiplier,
final String fieldDesc) {
Core.verbose(methodName + "[" + fieldLocation + "." + fieldNode + "]"); Core.verbose(methodName + "[" + fieldLocation + "." + fieldNode + "]");
this.into = ASMUtils.getClass(into); this.into = ASMUtils.getClass(into);
this.fieldLocation = ASMUtils.getClass(fieldLocation); this.fieldLocation = ASMUtils.getClass(fieldLocation);
this.fieldNode = ASMUtils.getField(ASMUtils.getClass(fieldLocation), fieldNode); this.fieldNode = ASMUtils.getField(ASMUtils.getClass(fieldLocation), fieldNode, fieldDesc);
this.methodName = methodName; this.methodName = methodName;
this.returnDesc = returnDesc == null ? this.fieldNode.desc : returnDesc; this.returnDesc = returnDesc == null ? this.fieldNode.desc : returnDesc;
this.staticMethod = staticMethod; this.staticMethod = staticMethod;
@@ -21,18 +21,18 @@ public class Setter implements Injectable {
private String desc; private String desc;
private boolean methodStatic; private boolean methodStatic;
public Setter(final String fieldLocation, String into, final String fieldName, final String methodName, final String desc, final boolean methodStatic) { public Setter(final String fieldLocation, String into, final String fieldName, final String methodName, final String desc, final boolean methodStatic, final String fieldDesc) {
this.fieldLocation = ASMUtils.getClass(fieldLocation); this.fieldLocation = ASMUtils.getClass(fieldLocation);
into = (into == null) ? fieldLocation : into; into = (into == null) ? fieldLocation : into;
this.into = ASMUtils.getClass(into); this.into = ASMUtils.getClass(into);
this.field = ASMUtils.getField(this.fieldLocation, fieldName); this.field = ASMUtils.getField(this.fieldLocation, fieldName, fieldDesc);
this.name = methodName; this.name = methodName;
this.desc = (desc == null) ? this.field.desc : desc; this.desc = (desc == null) ? this.field.desc : desc;
this.methodStatic = methodStatic; this.methodStatic = methodStatic;
} }
public Setter(final String fieldLocation, final String fieldName, final String methodName) { public Setter(final String fieldLocation, final String fieldName, final String methodName) {
this(fieldLocation, null, fieldName, methodName, null, false); this(fieldLocation, null, fieldName, methodName, null, false, null);
} }
/** /**
@@ -119,7 +119,7 @@ public class JSONHookParser extends HookParser {
String clazz = o.containsKey("class") ? this.get(o, "class") : interfaces.get(this.get(o, "accessor")); String clazz = o.containsKey("class") ? this.get(o, "class") : interfaces.get(this.get(o, "accessor"));
String into = o.containsKey("into") ? this.get(o, "into") : clazz; String into = o.containsKey("into") ? this.get(o, "into") : clazz;
g[i] = new Getter(into, clazz, this.get(o, "field"), this.get(o, "method"), desc, o.containsKey("static") ? (boolean) o.get("static") : false, 0); g[i] = new Getter(into, clazz, this.get(o, "field"), this.get(o, "method"), desc, o.containsKey("static") ? (boolean) o.get("static") : false, 0, null);
} }
return g; return g;
} }
@@ -152,7 +152,7 @@ public class JSONHookParser extends HookParser {
String clazz = o.containsKey("class") ? this.get(o, "class") : interfaces.get(this.get(o, "accessor")); String clazz = o.containsKey("class") ? this.get(o, "class") : interfaces.get(this.get(o, "accessor"));
String into = o.containsKey("into") ? this.get(o, "into") : clazz; String into = o.containsKey("into") ? this.get(o, "into") : clazz;
s[i] = new Setter(into, clazz, this.get(o, "field"), this.get(o, "method"), desc, o.containsKey("static") ? (boolean) o.get("static") : false); s[i] = new Setter(into, clazz, this.get(o, "field"), this.get(o, "method"), desc, o.containsKey("static") ? (boolean) o.get("static") : false, null);
} }
return s; return s;
} }
@@ -161,6 +161,7 @@ public class XMLHookParser extends HookParser {
addGetter) : className; addGetter) : className;
final long multiplier = isSet("multiplier", addGetter) ? Long.parseLong(getValue("multiplier", addGetter)) : 0L; final long multiplier = isSet("multiplier", addGetter) ? Long.parseLong(getValue("multiplier", addGetter)) : 0L;
final String fieldName = getValue("field", addGetter); final String fieldName = getValue("field", addGetter);
final String fieldDesc = isSet("descfield", addGetter) ? getValue("descfield", addGetter) : null;
final String methodName = getValue("methodname", addGetter); final String methodName = getValue("methodname", addGetter);
boolean staticMethod = isSet("methstatic", addGetter) ? (getValue( boolean staticMethod = isSet("methstatic", addGetter) ? (getValue(
"methstatic", addGetter).equals("true")) : false; "methstatic", addGetter).equals("true")) : false;
@@ -185,7 +186,7 @@ public class XMLHookParser extends HookParser {
returnDesc = str.toString(); returnDesc = str.toString();
} }
final Getter get = new Getter(into, className, fieldName, final Getter get = new Getter(into, className, fieldName,
methodName, returnDesc, staticMethod, multiplier); methodName, returnDesc, staticMethod, multiplier, fieldDesc);
getterList.add(get); getterList.add(get);
} }
return getterList.toArray(new Getter[getterList.size()]); return getterList.toArray(new Getter[getterList.size()]);
@@ -233,6 +234,7 @@ public class XMLHookParser extends HookParser {
final String into = isSet("into", addSetter) ? getValue("into", final String into = isSet("into", addSetter) ? getValue("into",
addSetter) : className; addSetter) : className;
final String fieldName = getValue("field", addSetter); final String fieldName = getValue("field", addSetter);
final String fieldDesc = isSet("descfield", addSetter) ? getValue("descfield", addSetter) : null;
final String methodName = getValue("methodname", addSetter); final String methodName = getValue("methodname", addSetter);
boolean staticMethod = isSet("methstatic", addSetter) ? (getValue( boolean staticMethod = isSet("methstatic", addSetter) ? (getValue(
"methstatic", addSetter).equals("true")) : false; "methstatic", addSetter).equals("true")) : false;
@@ -257,7 +259,7 @@ public class XMLHookParser extends HookParser {
returnDesc = str.toString(); returnDesc = str.toString();
} }
final Setter get = new Setter(className, into, fieldName, final Setter get = new Setter(className, into, fieldName,
methodName, returnDesc, staticMethod); methodName, returnDesc, staticMethod, fieldDesc);
setterList.add(get); setterList.add(get);
} }
return setterList.toArray(new Setter[setterList.size()]); return setterList.toArray(new Setter[setterList.size()]);