Package org.apache.openjpa.util.asm
Class AsmHelper
- java.lang.Object
-
- org.apache.openjpa.util.asm.AsmHelper
-
public final class AsmHelper extends Object
Utility methods to deal with ASM bytecode- Author:
- Mark Struberg
-
-
Field Summary
Fields Modifier and Type Field Description static org.apache.xbean.asm9.Attribute[]ATTRSstatic org.apache.xbean.asm9.TypeTYPE_OBJECT
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static Class<?>getClass(ClassLoader classLoader, String internalTypeName)static byte[]getClassBytes(String typeName)static intgetCorrespondingLoadInsn(int storeInsnOpcode)Get the corresponding LOAD instruction for the given STORE instruction.static intgetCorrespondingStoreInsn(int loadInsnOpcode)Get the corresponding STORE instruction for the given LOAD instruction.static Class<?>getDescribedClass(ClassLoader classLoader, String typeDesc)Get the class from the described typestatic String[]getInternalNames(Class<?>[] classes)Get the internal names for the given classesstatic org.apache.xbean.asm9.tree.AbstractInsnNodegetLoadConstantInsn(Object val)static intgetLoadInsn(Class<?> type)Calclates the proper LOAD instruction opcode for the given type.static intgetLocalVarPos(org.apache.xbean.asm9.tree.MethodNode meth)Calculate the next local variable position.static Optional<org.apache.xbean.asm9.tree.MethodNode>getMethodNode(org.apache.xbean.asm9.tree.ClassNode classNode, Method meth)static Optional<org.apache.xbean.asm9.tree.MethodNode>getMethodNode(org.apache.xbean.asm9.tree.ClassNode classNode, String methodName, Class<?> returnType, Class<?>... paramTypes)static intgetParamIndex(org.apache.xbean.asm9.tree.MethodNode methodNode, int varPos)Determine the 0-based index of the parameter of LOAD or STORE position varPosstatic org.apache.xbean.asm9.Type[]getParamTypes(Class<?>[] params)get the ASM Types for the given classesstatic intgetReturnInsn(Class<?> type)Calclates the proper Return instruction opcode for the given classstatic intgetStoreInsn(Class<?> type)Calclates the proper STORE instruction opcode for the given typestatic booleanisLoadInsn(org.apache.xbean.asm9.tree.AbstractInsnNode insn)static booleanisPrimitive(String typeDesc, boolean includeVoid)static booleanisThisInsn(org.apache.xbean.asm9.tree.AbstractInsnNode insn)static org.apache.xbean.asm9.tree.ClassNodereadClassNode(Class<?> clazz)Read the binary bytecode from the class with the given namestatic org.apache.xbean.asm9.tree.ClassNodereadClassNode(ClassLoader classLoader, String className)Read the binary bytecode from the class with the given namestatic org.apache.xbean.asm9.tree.InsnListthrowException(Class type)Helper method to add the code necessary to throw the given exception type, sans message.static org.apache.xbean.asm9.tree.InsnListthrowException(Class type, String msg)Helper method to add the code necessary to throw the given exception type, sans message.static byte[]toByteArray(ClassNodeTracker cnt)Create a byte[] of that class represented by the ClassNodeTracker
-
-
-
Method Detail
-
readClassNode
public static org.apache.xbean.asm9.tree.ClassNode readClassNode(Class<?> clazz)
Read the binary bytecode from the class with the given name- Parameters:
clazz- the class to read into the ClassNode- Returns:
- the ClassNode constructed from that class
-
readClassNode
public static org.apache.xbean.asm9.tree.ClassNode readClassNode(ClassLoader classLoader, String className) throws ClassNotFoundException
Read the binary bytecode from the class with the given name- Parameters:
classLoader- the ClassLoader to useclassName- the fully qualified class name to read. e.g. "org.mycorp.mypackage.MyEntity"- Returns:
- the ClassNode constructed from that class
- Throws:
ClassNotFoundException
-
getClassBytes
public static byte[] getClassBytes(String typeName)
-
toByteArray
public static byte[] toByteArray(ClassNodeTracker cnt)
Create a byte[] of that class represented by the ClassNodeTracker
-
getMethodNode
public static Optional<org.apache.xbean.asm9.tree.MethodNode> getMethodNode(org.apache.xbean.asm9.tree.ClassNode classNode, Method meth)
-
getMethodNode
public static Optional<org.apache.xbean.asm9.tree.MethodNode> getMethodNode(org.apache.xbean.asm9.tree.ClassNode classNode, String methodName, Class<?> returnType, Class<?>... paramTypes)
-
getReturnInsn
public static int getReturnInsn(Class<?> type)
Calclates the proper Return instruction opcode for the given class- Parameters:
type- the type to get returned- Returns:
- the proper Opcode RETURN, ARETURN, IRETURN, etc
-
getLoadConstantInsn
public static org.apache.xbean.asm9.tree.AbstractInsnNode getLoadConstantInsn(Object val)
-
getStoreInsn
public static int getStoreInsn(Class<?> type)
Calclates the proper STORE instruction opcode for the given type- Parameters:
type- the type to get stored- Returns:
- the proper Opcode ISTORE, ASTORE, LSTORE, etc
-
getLoadInsn
public static int getLoadInsn(Class<?> type)
Calclates the proper LOAD instruction opcode for the given type. This is the appropriate bytecode instruction to load a value from a variable to the stack.- Parameters:
type- the type to get loaded- Returns:
- the proper Opcode ILOAD, ALOAD, LLOAD, etc
-
getInternalNames
public static String[] getInternalNames(Class<?>[] classes)
Get the internal names for the given classes- See Also:
Type.getInternalName(Class)
-
getParamTypes
public static org.apache.xbean.asm9.Type[] getParamTypes(Class<?>[] params)
get the ASM Types for the given classes- See Also:
Type.getType(Method)
-
getParamIndex
public static int getParamIndex(org.apache.xbean.asm9.tree.MethodNode methodNode, int varPos)Determine the 0-based index of the parameter of LOAD or STORE position varPos- Parameters:
methodNode-varPos- the position on the stack- Returns:
- the index of the parameter which corresponds to this varPos
-
isLoadInsn
public static boolean isLoadInsn(org.apache.xbean.asm9.tree.AbstractInsnNode insn)
- Returns:
- true if the instruction is an LOAD instruction
-
isThisInsn
public static boolean isThisInsn(org.apache.xbean.asm9.tree.AbstractInsnNode insn)
- Returns:
- true if the instruction is an ALOAD_0
-
getCorrespondingLoadInsn
public static int getCorrespondingLoadInsn(int storeInsnOpcode)
Get the corresponding LOAD instruction for the given STORE instruction.- Parameters:
storeInsnOpcode- e.g.Opcodes.ISTORE- Throws:
IllegalArgumentException- if the given opcode is not a STORE instruction
-
getCorrespondingStoreInsn
public static int getCorrespondingStoreInsn(int loadInsnOpcode)
Get the corresponding STORE instruction for the given LOAD instruction.- Parameters:
loadInsnOpcode- e.g.Opcodes.FLOAD- Throws:
IllegalArgumentException- if the given opcode is not a STORE instruction
-
isPrimitive
public static boolean isPrimitive(String typeDesc, boolean includeVoid)
- Parameters:
typeDesc- the internal type descriptor from the bytecode. SeeClassNode.nameincludeVoid- if the Void.class type also counts as primitive
-
getDescribedClass
public static Class<?> getDescribedClass(ClassLoader classLoader, String typeDesc)
Get the class from the described type- Parameters:
typeDesc-- Returns:
- described class or
nullif it could not be loaded
-
throwException
public static org.apache.xbean.asm9.tree.InsnList throwException(Class type)
Helper method to add the code necessary to throw the given exception type, sans message.
-
throwException
public static org.apache.xbean.asm9.tree.InsnList throwException(Class type, String msg)
Helper method to add the code necessary to throw the given exception type, sans message.
-
getClass
public static Class<?> getClass(ClassLoader classLoader, String internalTypeName)
-
getLocalVarPos
public static int getLocalVarPos(org.apache.xbean.asm9.tree.MethodNode meth)
Calculate the next local variable position. For a non-static method the position 0 on the stack is the this pointer. After that there are all the method parameters. For a static method the method parameters begin at position zero. This method does calculate the first unused stack position which can be used for xLOAD/xSTORE opterations, e.g.int nextVarPos = AsmHelper.getLocalVarPos(myMethodNode); instructions.add(AsmHelper.getLoadConstantInsn(4711)); instructions.add(new VarInsnNode(Opcodes.ISTORE, nextVarPos);- Returns:
- the 0-based position on the stack at which the local variables can be located.
-
-