Package org.apache.openjpa.util.asm
Class AsmHelper
- java.lang.Object
-
- org.apache.openjpa.util.asm.AsmHelper
-
public final class AsmHelper extends java.lang.ObjectUtility 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 java.lang.Class<?>getClass(java.lang.ClassLoader classLoader, java.lang.String internalTypeName)static byte[]getClassBytes(java.lang.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 java.lang.Class<?>getDescribedClass(java.lang.ClassLoader classLoader, java.lang.String typeDesc)Get the class from the described typestatic java.lang.String[]getInternalNames(java.lang.Class<?>[] classes)Get the internal names for the given classesstatic org.apache.xbean.asm9.tree.AbstractInsnNodegetLoadConstantInsn(java.lang.Object val)static intgetLoadInsn(java.lang.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 java.util.Optional<org.apache.xbean.asm9.tree.MethodNode>getMethodNode(org.apache.xbean.asm9.tree.ClassNode classNode, java.lang.reflect.Method meth)static java.util.Optional<org.apache.xbean.asm9.tree.MethodNode>getMethodNode(org.apache.xbean.asm9.tree.ClassNode classNode, java.lang.String methodName, java.lang.Class<?> returnType, java.lang.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(java.lang.Class<?>[] params)get the ASM Types for the given classesstatic intgetReturnInsn(java.lang.Class<?> type)Calclates the proper Return instruction opcode for the given classstatic intgetStoreInsn(java.lang.Class<?> type)Calclates the proper STORE instruction opcode for the given typestatic booleanisLoadInsn(org.apache.xbean.asm9.tree.AbstractInsnNode insn)static booleanisPrimitive(java.lang.String typeDesc, boolean includeVoid)static booleanisThisInsn(org.apache.xbean.asm9.tree.AbstractInsnNode insn)static org.apache.xbean.asm9.tree.ClassNodereadClassNode(java.lang.Class<?> clazz)Read the binary bytecode from the class with the given namestatic org.apache.xbean.asm9.tree.ClassNodereadClassNode(java.lang.ClassLoader classLoader, java.lang.String className)Read the binary bytecode from the class with the given namestatic org.apache.xbean.asm9.tree.InsnListthrowException(java.lang.Class type)Helper method to add the code necessary to throw the given exception type, sans message.static org.apache.xbean.asm9.tree.InsnListthrowException(java.lang.Class type, java.lang.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(java.lang.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(java.lang.ClassLoader classLoader, java.lang.String className) throws java.lang.ClassNotFoundExceptionRead 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:
java.lang.ClassNotFoundException
-
getClassBytes
public static byte[] getClassBytes(java.lang.String typeName)
-
toByteArray
public static byte[] toByteArray(ClassNodeTracker cnt)
Create a byte[] of that class represented by the ClassNodeTracker
-
getMethodNode
public static java.util.Optional<org.apache.xbean.asm9.tree.MethodNode> getMethodNode(org.apache.xbean.asm9.tree.ClassNode classNode, java.lang.reflect.Method meth)
-
getMethodNode
public static java.util.Optional<org.apache.xbean.asm9.tree.MethodNode> getMethodNode(org.apache.xbean.asm9.tree.ClassNode classNode, java.lang.String methodName, java.lang.Class<?> returnType, java.lang.Class<?>... paramTypes)
-
getReturnInsn
public static int getReturnInsn(java.lang.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(java.lang.Object val)
-
getStoreInsn
public static int getStoreInsn(java.lang.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(java.lang.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 java.lang.String[] getInternalNames(java.lang.Class<?>[] classes)
Get the internal names for the given classes- See Also:
Type.getInternalName(Class)
-
getParamTypes
public static org.apache.xbean.asm9.Type[] getParamTypes(java.lang.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:
java.lang.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:
java.lang.IllegalArgumentException- if the given opcode is not a STORE instruction
-
isPrimitive
public static boolean isPrimitive(java.lang.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 java.lang.Class<?> getDescribedClass(java.lang.ClassLoader classLoader, java.lang.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(java.lang.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(java.lang.Class type, java.lang.String msg)Helper method to add the code necessary to throw the given exception type, sans message.
-
getClass
public static java.lang.Class<?> getClass(java.lang.ClassLoader classLoader, java.lang.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.
-
-