Package org.apache.openjpa.util.asm
Class AsmHelper
java.lang.Object
org.apache.openjpa.util.asm.AsmHelper
Utility methods to deal with ASM bytecode
- Author:
- Mark Struberg
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final org.apache.xbean.asm9.Attribute[]static final org.apache.xbean.asm9.Type -
Method Summary
Modifier and TypeMethodDescriptionstatic 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.AbstractInsnNodestatic 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[]Create a byte[] of that class represented by the ClassNodeTracker
-
Field Details
-
TYPE_OBJECT
public static final org.apache.xbean.asm9.Type TYPE_OBJECT -
ATTRS
public static final org.apache.xbean.asm9.Attribute[] ATTRS
-
-
Method Details
-
readClassNode
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
-
toByteArray
Create a byte[] of that class represented by the ClassNodeTracker -
getMethodNode
-
getMethodNode
-
getReturnInsn
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
-
getStoreInsn
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
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
Get the internal names for the given classes- See Also:
-
Type.getInternalName(Class)
-
getParamTypes
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
- Parameters:
typeDesc- the internal type descriptor from the bytecode. SeeClassNode.nameincludeVoid- if the Void.class type also counts as primitive
-
getDescribedClass
Get the class from the described type- Parameters:
typeDesc-- Returns:
- described class or
nullif it could not be loaded
-
throwException
Helper method to add the code necessary to throw the given exception type, sans message. -
throwException
Helper method to add the code necessary to throw the given exception type, sans message. -
getClass
-
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.
-