Remove a lot of redundant cruft from ASMHelper

This commit is contained in:
Mumfrey committed 2016-11-23 16:18:32 +00:00
1 parent 70e36777a4
commit dc18582c86
1 file changed
+5 -264
@@ -99,283 +99,24 @@ public class ASMHelper {
"I", //"B",
"I", //"S"
};
/**
* Generate a new method "boolean name()", which returns a constant value.
*
* @param clazz Class to add method to
* @param name Name of method
* @param retval Return value of method
*/
public static void generateBooleanMethodConst(ClassNode clazz, String name, boolean retval) {
MethodNode method = new MethodNode(Opcodes.ASM5, Opcodes.ACC_PUBLIC | Opcodes.ACC_SYNTHETIC, name, "()Z", null, null);
InsnList code = method.instructions;
code.add(ASMHelper.pushIntConstant(retval ? 1 : 0));
code.add(new InsnNode(Opcodes.IRETURN));
clazz.methods.add(method);
}
/**
* Generate a new method "int name()", which returns a constant value.
*
* @param clazz Class to add method to
* @param name Name of method
* @param retval Return value of method
*/
public static void generateIntegerMethodConst(ClassNode clazz, String name, short retval) {
MethodNode method = new MethodNode(Opcodes.ASM5, Opcodes.ACC_PUBLIC | Opcodes.ACC_SYNTHETIC, name, "()I", null, null);
InsnList code = method.instructions;
code.add(ASMHelper.pushIntConstant(retval));
code.add(new InsnNode(Opcodes.IRETURN));
clazz.methods.add(method);
}
/**
* Generate a forwarding method of the form
* "T name() { return this.forward(); }".
*
* @param clazz Class to generate new method on
* @param name Name of method to generate
* @param forwardname Name of method to call
* @param rettype Return type of method
*/
public static void generateSelfForwardingMethod(ClassNode clazz, String name, String forwardname, Type rettype) {
MethodNode method =
new MethodNode(Opcodes.ASM5, Opcodes.ACC_PUBLIC | Opcodes.ACC_SYNTHETIC, name, "()" + rettype.getDescriptor(), null, null);
ASMHelper.populateSelfForwardingMethod(method, forwardname, rettype, Type.getObjectType(clazz.name));
clazz.methods.add(method);
}
/**
* Generate a forwarding method of the form
* "static T name(S object) { return object.forward(); }".
*
* @param clazz Class to generate new method on
* @param name Name of method to generate
* @param forwardname Name of method to call
* @param rettype Return type of method
* @param argtype Argument type
*/
public static void generateStaticForwardingMethod(ClassNode clazz, String name, String forwardname, Type rettype, Type argtype) {
MethodNode method = new MethodNode(Opcodes.ASM5,
Opcodes.ACC_STATIC | Opcodes.ACC_PUBLIC | Opcodes.ACC_SYNTHETIC, name,
"()" + rettype.getDescriptor(), null, null);
ASMHelper.populateSelfForwardingMethod(method, forwardname, rettype, argtype);
clazz.methods.add(method);
}
/**
* Generate a forwarding method of the form
* "T name() { return Class.forward(this); }".
*
* @param clazz Class to generate new method on
* @param name Name of method to generate
* @param forwardname Name of method to call
* @param rettype Return type of method
* @param fowardtype Forward type
*/
public static void generateForwardingToStaticMethod(ClassNode clazz, String name, String forwardname, Type rettype, Type fowardtype) {
MethodNode method = new MethodNode(Opcodes.ASM5, Opcodes.ACC_PUBLIC | Opcodes.ACC_SYNTHETIC, name,
"()" + rettype.getDescriptor(), null, null);
ASMHelper.populateForwardingToStaticMethod(method, forwardname, rettype, Type.getObjectType(clazz.name), fowardtype);
clazz.methods.add(method);
}
/**
* Generate a forwarding method of the form
* "T name() { return Class.forward(this); }".
*
* @param clazz Class to generate new method on
* @param name Name of method to generate
* @param forwardname Name of method to call
* @param rettype Return type of method
* @param fowardtype Forward type
* @param thistype Type to treat 'this' as for overload searching purposes
*/
public static void generateForwardingToStaticMethod(ClassNode clazz, String name, String forwardname, Type rettype, Type fowardtype,
Type thistype) {
MethodNode
method =
new MethodNode(Opcodes.ASM5, Opcodes.ACC_PUBLIC | Opcodes.ACC_SYNTHETIC, name, "()" + rettype.getDescriptor(), null, null);
ASMHelper.populateForwardingToStaticMethod(method, forwardname, rettype, thistype, fowardtype);
clazz.methods.add(method);
}
/**
* Replace a method's code with a forward to another method on itself
* or the first argument of a static method, as the argument takes the
* place of this.
*
* @param method Method to replace code of
* @param forwardname Name of method to forward to
* @param thistype Type of object method is being replaced on
*/
public static void replaceSelfForwardingMethod(MethodNode method, String forwardname, Type thistype) {
Type methodType = Type.getMethodType(method.desc);
method.instructions.clear();
ASMHelper.populateSelfForwardingMethod(method, forwardname, methodType.getReturnType(), thistype);
}
/**
* Generate a forwarding method of the form
* "T name(S object) { return object.forward(); }".
*
* @param clazz Class to generate new method on
* @param name Name of method to generate
* @param forwardname Name of method to call
* @param rettype Return type of method
* @param argtype Type of object to call method on
*/
public static void generateForwardingMethod(ClassNode clazz, String name, String forwardname, Type rettype, Type argtype) {
MethodNode
method =
new MethodNode(Opcodes.ASM5, Opcodes.ACC_PUBLIC | Opcodes.ACC_SYNTHETIC, name, "()" + rettype.getDescriptor(), null, null);
ASMHelper.populateForwardingMethod(method, forwardname, rettype, argtype, Type.getObjectType(clazz.name));
clazz.methods.add(method);
}
/**
* Replace a method's code with a forward to an method on its first
* argument.
*
* @param method Method to replace code of
* @param forwardname Name of method to forward to
* @param thistype Type of object method is being replaced on
*/
public static void replaceForwardingMethod(MethodNode method, String forwardname, Type thistype) {
Type methodType = Type.getMethodType(method.desc);
method.instructions.clear();
ASMHelper.populateForwardingMethod(method, forwardname, methodType.getReturnType(), methodType.getArgumentTypes()[0], thistype);
}
/**
* Populate a forwarding method of the form
* "T name() { return Class.forward(this); }".
*
* @param method Method to generate code for
* @param forwardname Name of method to call
* @param rettype Return type of method
* @param thistype Type of object method is being generated on
* @param forwardtype Type to forward method to
*/
public static void populateForwardingToStaticMethod(MethodNode method, String forwardname, Type rettype, Type thistype, Type forwardtype) {
InsnList code = method.instructions;
code.add(new VarInsnNode(thistype.getOpcode(Opcodes.ILOAD), 0));
code.add(new MethodInsnNode(Opcodes.INVOKESTATIC, forwardtype.getInternalName(), forwardname, Type.getMethodDescriptor(rettype, thistype),
false));
code.add(new InsnNode(rettype.getOpcode(Opcodes.IRETURN)));
}
/**
* Populate a forwarding method of the form
* "T name() { return this.forward(); }". This is also valid for methods of
* the form "static T name(S object) { return object.forward() }".
*
* @param method Method to generate code for
* @param forwardname Name of method to call
* @param rettype Return type of method
* @param thistype Type of object method is being generated on
*/
public static void populateSelfForwardingMethod(MethodNode method, String forwardname, Type rettype, Type thistype) {
InsnList code = method.instructions;
code.add(new VarInsnNode(thistype.getOpcode(Opcodes.ILOAD), 0));
code.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, thistype.getInternalName(), forwardname, "()" + rettype.getDescriptor(), false));
code.add(new InsnNode(rettype.getOpcode(Opcodes.IRETURN)));
}
/**
* Populate a forwarding method of the form
* "T name(S object) { return object.forward(); }".
*
* @param method Method to generate code for
* @param forwardname Name of method to call
* @param rettype Return type of method
* @param argtype Type of object to call method on
* @param thistype Type of object method is being generated on
*/
public static void populateForwardingMethod(MethodNode method, String forwardname, Type rettype, Type argtype, Type thistype) {
InsnList code = method.instructions;
code.add(new VarInsnNode(argtype.getOpcode(Opcodes.ILOAD), 1));
code.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, argtype.getInternalName(), forwardname, "()" + rettype.getDescriptor(), false));
code.add(new InsnNode(rettype.getOpcode(Opcodes.IRETURN)));
}
/**
* Gets an instruction that pushes a integer onto the stack. The
* instruction uses the smallest push possible (ICONST_*, BIPUSH, SIPUSH or
* Integer constant).
*
* @param c the integer to push onto the stack
* @return insn node to insert
*/
public static AbstractInsnNode pushIntConstant(int c) {
if (c == -1) {
return new InsnNode(Opcodes.ICONST_M1);
} else if (c >= 0 && c <= 5) {
return new InsnNode(ASMHelper.CONSTANTS_INT[c + 1]);
} else if (c >= Byte.MIN_VALUE && c <= Byte.MAX_VALUE) {
return new IntInsnNode(Opcodes.BIPUSH, c);
} else if (c >= Short.MIN_VALUE && c <= Short.MAX_VALUE) {
return new IntInsnNode(Opcodes.SIPUSH, c);
} else {
return new LdcInsnNode(c);
}
}
/**
* Finds a method given the method descriptor
*
* @param clazz the class to scan
* @param classNode the class to scan
* @param name the method name
* @param desc the method descriptor
* @return discovered method node or null
*/
public static MethodNode findMethod(ClassNode clazz, String name, String desc) {
Iterator<MethodNode> i = clazz.methods.iterator();
while (i.hasNext()) {
MethodNode m = i.next();
if (m.name.equals(name) && m.desc.equals(desc)) {
return m;
public static MethodNode findMethod(ClassNode classNode, String name, String desc) {
for (MethodNode method : classNode.methods) {
if (method.name.equals(name) && method.desc.equals(desc)) {
return method;
}
}
return null;
}
/**
* Adds a method to a class, overwriting any matching method.
*
* @param clazz the class to scan
* @param method the method to add
*/
public static void addAndReplaceMethod(ClassNode clazz, MethodNode method) {
MethodNode m = ASMHelper.findMethod(clazz, method.name, method.desc);
if (m != null) {
clazz.methods.remove(m);
}
clazz.methods.add(method);
}
/**
* Runs textifier on the specified class node and dumps the output to the
* specified output stream