com.tc.object.bytecode
Class ByteCodeUtil

java.lang.Object
  extended by com.tc.object.bytecode.ByteCodeUtil
All Implemented Interfaces:
Opcodes

public class ByteCodeUtil
extends java.lang.Object
implements Opcodes

Utility methods for working with byte code.


Field Summary
static java.lang.String DMI_METHOD_RENAME_PREFIX
           
static java.lang.String MANAGEABLE_CLASS
           
static java.lang.String MANAGEABLE_TYPE
           
static java.lang.String MANAGED_VALUES_GETTER
           
static java.lang.String MANAGED_VALUES_GETTER_DESCRIPTION
           
static java.lang.String MANAGED_VALUES_SETTER
           
static java.lang.String METHOD_RENAME_PREFIX
           
static java.lang.String NAMEDCLASSLOADER_CLASS
           
static java.lang.String NAMEDCLASSLOADER_TYPE
           
static java.lang.String SYNC_METHOD_RENAME_PREFIX
           
static java.lang.String TC_FIELD_PREFIX
           
static java.lang.String TC_METHOD_PREFIX
           
static java.lang.String TRANSPARENT_ACCESS_CLASS
           
static java.lang.String TRANSPARENT_ACCESS_TYPE
           
static java.lang.String VALUES_GETTER
           
static java.lang.String VALUES_GETTER_DESCRIPTION
           
static java.lang.String VALUES_SETTER
           
static java.lang.String VALUES_SETTER_DESCRIPTION
           
 
Fields inherited from interface com.tc.asm.Opcodes
AALOAD, AASTORE, ACC_ABSTRACT, ACC_ANNOTATION, ACC_BRIDGE, ACC_DEPRECATED, ACC_ENUM, ACC_FINAL, ACC_INTERFACE, ACC_NATIVE, ACC_PRIVATE, ACC_PROTECTED, ACC_PUBLIC, ACC_STATIC, ACC_STRICT, ACC_SUPER, ACC_SYNCHRONIZED, ACC_SYNTHETIC, ACC_TRANSIENT, ACC_VARARGS, ACC_VOLATILE, ACONST_NULL, ALOAD, ANEWARRAY, ARETURN, ARRAYLENGTH, ASTORE, ATHROW, BALOAD, BASTORE, BIPUSH, CALOAD, CASTORE, CHECKCAST, D2F, D2I, D2L, DADD, DALOAD, DASTORE, DCMPG, DCMPL, DCONST_0, DCONST_1, DDIV, DLOAD, DMUL, DNEG, DOUBLE, DREM, DRETURN, DSTORE, DSUB, DUP, DUP_X1, DUP_X2, DUP2, DUP2_X1, DUP2_X2, F_APPEND, F_CHOP, F_FULL, F_NEW, F_SAME, F_SAME1, F2D, F2I, F2L, FADD, FALOAD, FASTORE, FCMPG, FCMPL, FCONST_0, FCONST_1, FCONST_2, FDIV, FLOAD, FLOAT, FMUL, FNEG, FREM, FRETURN, FSTORE, FSUB, GETFIELD, GETSTATIC, GOTO, I2B, I2C, I2D, I2F, I2L, I2S, IADD, IALOAD, IAND, IASTORE, ICONST_0, ICONST_1, ICONST_2, ICONST_3, ICONST_4, ICONST_5, ICONST_M1, IDIV, IF_ACMPEQ, IF_ACMPNE, IF_ICMPEQ, IF_ICMPGE, IF_ICMPGT, IF_ICMPLE, IF_ICMPLT, IF_ICMPNE, IFEQ, IFGE, IFGT, IFLE, IFLT, IFNE, IFNONNULL, IFNULL, IINC, ILOAD, IMUL, INEG, INSTANCEOF, INTEGER, INVOKEDYNAMIC, INVOKEDYNAMIC_OWNER, INVOKEINTERFACE, INVOKESPECIAL, INVOKESTATIC, INVOKEVIRTUAL, IOR, IREM, IRETURN, ISHL, ISHR, ISTORE, ISUB, IUSHR, IXOR, JSR, L2D, L2F, L2I, LADD, LALOAD, LAND, LASTORE, LCMP, LCONST_0, LCONST_1, LDC, LDIV, LLOAD, LMUL, LNEG, LONG, LOOKUPSWITCH, LOR, LREM, LRETURN, LSHL, LSHR, LSTORE, LSUB, LUSHR, LXOR, MONITORENTER, MONITOREXIT, MULTIANEWARRAY, NEW, NEWARRAY, NOP, NULL, POP, POP2, PUTFIELD, PUTSTATIC, RET, RETURN, SALOAD, SASTORE, SIPUSH, SWAP, T_BOOLEAN, T_BYTE, T_CHAR, T_DOUBLE, T_FLOAT, T_INT, T_LONG, T_SHORT, TABLESWITCH, TOP, UNINITIALIZED_THIS, V1_1, V1_2, V1_3, V1_4, V1_5, V1_6, V1_7
 
Constructor Summary
ByteCodeUtil()
           
 
Method Summary
static java.lang.String[] addInterface(java.lang.String[] existing, java.lang.String toAdd)
           
static java.lang.String[] addInterfaces(java.lang.String[] existing, java.lang.String[] toAdd)
          Given a set of existing interfaces, add some more (without duplicates)
static void addTypeSpecificParameterLoad(MethodVisitor c, Type type, int offset)
          Add instructions to load type-specific value from local variable onto stack.
static java.lang.String classNameToFileName(java.lang.String className)
          Translate class name to file name
static java.lang.String classNameToInternalName(java.lang.String className)
          Translate class name to an internal name as used by ASM
static java.lang.String codeToName(java.lang.String typeCode)
          Translate type code to type name
static void createParametersToArrayByteCode(MethodVisitor c, Type[] parameters)
          Add instructions to convert the local variables typed with parameters into an array assuming values start at local variable offset of 1
static void createParametersToArrayByteCode(MethodVisitor c, Type[] parameters, int offset)
          Add instructions to convert the parameters into an array
static java.lang.String fieldGetterMethod(java.lang.String fieldName)
          Get name of synthetic field getter method added by Terracotta
static java.lang.String fieldSetterMethod(java.lang.String fieldName)
          Get name of synthetic field setter method added by Terracotta
static java.lang.String generateAutolockName(ObjectID id)
          Get auto lock name for object identifier
static java.lang.String generateLiteralLockName(java.lang.String literalValueTypeStr, java.lang.Object obj)
          The first argument should be "LiteralValues.valueFor(obj).name()", but I didn't want to slurp in a whole mess of classes into the boot jar by including LiteralValues.
static java.lang.String generateNamedLockName(java.lang.Object obj)
          Get named lock name for the lock object
static java.lang.String generateVolatileLockName(ObjectID id, java.lang.String fieldName)
          Get volatile lock name
static byte[] getBytesForClass(java.lang.String className, java.lang.ClassLoader loader)
          Read the bytes defining the class
static byte[] getBytesForInputstream(java.io.InputStream is)
          Read input stream into a byte array using a 4k buffer.
static int getFirstLocalVariableOffset(int callingMethodModifier, java.lang.String desc)
          Get offset of first local variable after method args
static int getLocalVariableOffset(int methodModifier)
          Returns 0 if the method is static.
static boolean isAutolockName(java.lang.String lockName)
          Determine whether a lock is an autolock based on its name
static boolean isParent(java.lang.String fieldName)
          Check whether the field name indicates that this is an inner classes synthetic field referring to the parent "this" reference.
static boolean isPrimitive(Type t)
          Check whether the type is a primitve
static boolean isSynthetic(int access)
          Determine whether an access modifier code indicates synthetic
static boolean isSynthetic(java.lang.String fieldName)
          Determine whether a field is synthetic
static boolean isTCSynthetic(java.lang.String fieldName)
          Determine whether a field is synthetic and was added by Terracotta
static java.lang.String methodDescriptionToMethodArgument(java.lang.String desc)
          Turn method description with byte code types into a readable signature
static java.lang.String methodDescriptionToReturnType(java.lang.String desc)
          Get return type (class name) from method descriptor
static long objectIdFromLockName(java.lang.String lockName)
          Get lock ID from autolock name
static void prepareStackForMethodCall(int callingMethodModifier, java.lang.String desc, MethodVisitor c)
          Push this (if not static) and all method args onto stack
static java.lang.reflect.Method[] purgeTCMethods(java.lang.reflect.Method[] methods)
           
static void pushDefaultValue(int variable, MethodVisitor c, Type type)
          Assign the default value to the variable
static void pushInstanceVariable(MethodVisitor c, java.lang.String className, java.lang.String fieldName, java.lang.String description)
          Add instruction to retrieve specified field in the object on the stack and replace with the field value.
static void pushMethodArguments(int callingMethodModifier, java.lang.String desc, MethodVisitor c)
          Add instructions to load method args into the stack
static void pushThis(MethodVisitor c)
          Add instruction to retrieve "this" from the local vars and load onto the stack
static java.lang.String sortToPrimitiveMethodName(int sort)
          Convert from Type.getSort() to a primitive method name like "booleanValue".
static java.lang.String sortToWrapperName(int sort)
          Map from primite type to wrapper class type
static java.lang.String stripGeneratedLockHeader(java.lang.String lockName)
          Strip generated lock header from lock name
static void systemOutPrintln(MethodVisitor mv, java.lang.String msg)
          Add instructions to print msg to System.out
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

TC_FIELD_PREFIX

public static final java.lang.String TC_FIELD_PREFIX
See Also:
Constant Field Values

TC_METHOD_PREFIX

public static final java.lang.String TC_METHOD_PREFIX
See Also:
Constant Field Values

METHOD_RENAME_PREFIX

public static final java.lang.String METHOD_RENAME_PREFIX
See Also:
Constant Field Values

SYNC_METHOD_RENAME_PREFIX

public static final java.lang.String SYNC_METHOD_RENAME_PREFIX
See Also:
Constant Field Values

DMI_METHOD_RENAME_PREFIX

public static final java.lang.String DMI_METHOD_RENAME_PREFIX
See Also:
Constant Field Values

VALUES_GETTER

public static final java.lang.String VALUES_GETTER
See Also:
Constant Field Values

VALUES_GETTER_DESCRIPTION

public static final java.lang.String VALUES_GETTER_DESCRIPTION
See Also:
Constant Field Values

VALUES_SETTER

public static final java.lang.String VALUES_SETTER
See Also:
Constant Field Values

VALUES_SETTER_DESCRIPTION

public static final java.lang.String VALUES_SETTER_DESCRIPTION
See Also:
Constant Field Values

MANAGED_VALUES_GETTER

public static final java.lang.String MANAGED_VALUES_GETTER
See Also:
Constant Field Values

MANAGED_VALUES_GETTER_DESCRIPTION

public static final java.lang.String MANAGED_VALUES_GETTER_DESCRIPTION
See Also:
Constant Field Values

MANAGED_VALUES_SETTER

public static final java.lang.String MANAGED_VALUES_SETTER
See Also:
Constant Field Values

MANAGEABLE_CLASS

public static final java.lang.String MANAGEABLE_CLASS
See Also:
Constant Field Values

MANAGEABLE_TYPE

public static final java.lang.String MANAGEABLE_TYPE
See Also:
Constant Field Values

TRANSPARENT_ACCESS_CLASS

public static final java.lang.String TRANSPARENT_ACCESS_CLASS
See Also:
Constant Field Values

TRANSPARENT_ACCESS_TYPE

public static final java.lang.String TRANSPARENT_ACCESS_TYPE
See Also:
Constant Field Values

NAMEDCLASSLOADER_CLASS

public static final java.lang.String NAMEDCLASSLOADER_CLASS
See Also:
Constant Field Values

NAMEDCLASSLOADER_TYPE

public static final java.lang.String NAMEDCLASSLOADER_TYPE
See Also:
Constant Field Values
Constructor Detail

ByteCodeUtil

public ByteCodeUtil()
Method Detail

purgeTCMethods

public static java.lang.reflect.Method[] purgeTCMethods(java.lang.reflect.Method[] methods)

addInterface

public static java.lang.String[] addInterface(java.lang.String[] existing,
                                              java.lang.String toAdd)

addInterfaces

public static java.lang.String[] addInterfaces(java.lang.String[] existing,
                                               java.lang.String[] toAdd)
Given a set of existing interfaces, add some more (without duplicates)

Parameters:
existing - The existing interfaces
toAdd - The interfaces to add
Returns:
A set of interfaces containing all of existing and toAdd with no dups

isPrimitive

public static boolean isPrimitive(Type t)
Check whether the type is a primitve

Parameters:
t - The ASM type
Returns:
True if primitive

sortToWrapperName

public static java.lang.String sortToWrapperName(int sort)
Map from primite type to wrapper class type

Parameters:
sort - Kind of primitve type as in Type.getSort()
Returns:
Wrapper class name, like "java/lang/Boolean"

codeToName

public static java.lang.String codeToName(java.lang.String typeCode)
Translate type code to type name

Parameters:
typeCode - Code from bytecode like B, C, etc
Returns:
Primitive type name: "byte", "char", etc

isAutolockName

public static boolean isAutolockName(java.lang.String lockName)
Determine whether a lock is an autolock based on its name

Parameters:
lockName - The lock name
Returns:
True if an autolock

objectIdFromLockName

public static long objectIdFromLockName(java.lang.String lockName)
Get lock ID from autolock name

Parameters:
lockName - The lock name
Returns:
Lock ID
Throws:
java.lang.IllegalArgumentException - If not an autolock

isSynthetic

public static boolean isSynthetic(java.lang.String fieldName)
Determine whether a field is synthetic

Parameters:
fieldName - The field name
Returns:
True if synthetic

isTCSynthetic

public static boolean isTCSynthetic(java.lang.String fieldName)
Determine whether a field is synthetic and was added by Terracotta

Parameters:
fieldName - The field name
Returns:
True if synthetic and added by Terracotta

isSynthetic

public static boolean isSynthetic(int access)
Determine whether an access modifier code indicates synthetic

Parameters:
access - Access modifier code
Returns:
True if synthetic flag is set

isParent

public static boolean isParent(java.lang.String fieldName)
Check whether the field name indicates that this is an inner classes synthetic field referring to the parent "this" reference.

Parameters:
fieldName - The field name
Returns:
True if this field refers to the parent this

pushThis

public static void pushThis(MethodVisitor c)
Add instruction to retrieve "this" from the local vars and load onto the stack

Parameters:
c - The current method visitor

pushInstanceVariable

public static void pushInstanceVariable(MethodVisitor c,
                                        java.lang.String className,
                                        java.lang.String fieldName,
                                        java.lang.String description)
Add instruction to retrieve specified field in the object on the stack and replace with the field value.

Parameters:
c - Current method visitor
className - The field class
fieldName - The field name
description - The field type

createParametersToArrayByteCode

public static void createParametersToArrayByteCode(MethodVisitor c,
                                                   Type[] parameters)
Add instructions to convert the local variables typed with parameters into an array assuming values start at local variable offset of 1

Parameters:
c - Method visitor
parameters - Paramater to convert

createParametersToArrayByteCode

public static void createParametersToArrayByteCode(MethodVisitor c,
                                                   Type[] parameters,
                                                   int offset)
Add instructions to convert the parameters into an array

Parameters:
c - Method visitor
parameters - Paramater types to convert
offset - Offset into local variables for values

addTypeSpecificParameterLoad

public static void addTypeSpecificParameterLoad(MethodVisitor c,
                                                Type type,
                                                int offset)
Add instructions to load type-specific value from local variable onto stack. Primitve values are wrapped into their wrapper object.

Parameters:
c - Method visitor
type - The type of the variable
offset - The local variable offset

pushMethodArguments

public static void pushMethodArguments(int callingMethodModifier,
                                       java.lang.String desc,
                                       MethodVisitor c)
Add instructions to load method args into the stack

Parameters:
callingMethodModifier - Calling method modifier
desc - Method descriptor
c - Current method visitor

getFirstLocalVariableOffset

public static int getFirstLocalVariableOffset(int callingMethodModifier,
                                              java.lang.String desc)
Get offset of first local variable after method args

Parameters:
callingMethodModifier - Calling method modifier
desc - Method descriptor
Returns:
First local variable offset

prepareStackForMethodCall

public static void prepareStackForMethodCall(int callingMethodModifier,
                                             java.lang.String desc,
                                             MethodVisitor c)
Push this (if not static) and all method args onto stack

Parameters:
callingMethodModifier - Calling method modifier
desc - Method descriptor
c - Calling method visitor

getLocalVariableOffset

public static int getLocalVariableOffset(int methodModifier)
Returns 0 if the method is static. 1 If the method is not.

Parameters:
methodModifier -
Returns:
0 if static, 1 if not

generateVolatileLockName

public static java.lang.String generateVolatileLockName(ObjectID id,
                                                        java.lang.String fieldName)
Get volatile lock name

Parameters:
id - Object identifier
field - Volatile field
Returns:
Lock name

generateAutolockName

public static java.lang.String generateAutolockName(ObjectID id)
Get auto lock name for object identifier

Parameters:
id - Identifier
Returns:
Auto lock name

generateNamedLockName

public static java.lang.String generateNamedLockName(java.lang.Object obj)
Get named lock name for the lock object

Parameters:
obj - Lock object
Returns:
Named lock name

generateLiteralLockName

public static java.lang.String generateLiteralLockName(java.lang.String literalValueTypeStr,
                                                       java.lang.Object obj)
The first argument should be "LiteralValues.valueFor(obj).name()", but I didn't want to slurp in a whole mess of classes into the boot jar by including LiteralValues. It's gross, but ManagerImpl just makes the call itself.

Parameters:
literalValueTypeStr - Literal value code
obj - The lock object

stripGeneratedLockHeader

public static java.lang.String stripGeneratedLockHeader(java.lang.String lockName)
Strip generated lock header from lock name

Parameters:
lockName - Lock name
Returns:
Real lock name

sortToPrimitiveMethodName

public static java.lang.String sortToPrimitiveMethodName(int sort)
Convert from Type.getSort() to a primitive method name like "booleanValue".

Parameters:
Type - kind
Returns:
Primitive method name

methodDescriptionToReturnType

public static java.lang.String methodDescriptionToReturnType(java.lang.String desc)
Get return type (class name) from method descriptor

Parameters:
desc - Method descriptor
Returns:
Return type class name

methodDescriptionToMethodArgument

public static java.lang.String methodDescriptionToMethodArgument(java.lang.String desc)
Turn method description with byte code types into a readable signature

Parameters:
desc - The bytecode description
Returns:
The method argument form

fieldGetterMethod

public static java.lang.String fieldGetterMethod(java.lang.String fieldName)
Get name of synthetic field getter method added by Terracotta

Parameters:
fieldName - The field name
Returns:
Getter method name

fieldSetterMethod

public static java.lang.String fieldSetterMethod(java.lang.String fieldName)
Get name of synthetic field setter method added by Terracotta

Parameters:
fieldName - The field name
Returns:
Setter method name

systemOutPrintln

public static void systemOutPrintln(MethodVisitor mv,
                                    java.lang.String msg)
Add instructions to print msg to System.out

Parameters:
mv - Method visitor
msg - Message to print

classNameToFileName

public static final java.lang.String classNameToFileName(java.lang.String className)
Translate class name to file name

Parameters:
className - The class name "java.lang.String"
Returns:
The file name on the classpath: "java/lang/String.class"

classNameToInternalName

public static final java.lang.String classNameToInternalName(java.lang.String className)
Translate class name to an internal name as used by ASM

Parameters:
className - The class name "java.lang.String"
Returns:
The internal name of the class as required by ASM: "Ljava/lang/String"

getBytesForClass

public static final byte[] getBytesForClass(java.lang.String className,
                                            java.lang.ClassLoader loader)
                                     throws java.lang.ClassNotFoundException
Read the bytes defining the class

Parameters:
className - The class
loader - The classloader
Returns:
The underlying bytes
Throws:
java.lang.ClassNotFoundException

getBytesForInputstream

public static final byte[] getBytesForInputstream(java.io.InputStream is)
                                           throws java.io.IOException
Read input stream into a byte array using a 4k buffer. Close stream when done.

Parameters:
is - Input stream
Returns:
Bytes read from stream
Throws:
java.io.IOException - If there is an error reading the stream

pushDefaultValue

public static void pushDefaultValue(int variable,
                                    MethodVisitor c,
                                    Type type)
Assign the default value to the variable

Parameters:
variable - The local variable to which the default value will be assigned
c - MethodVisitor
type - Type of the variable


Copyright © 2010 Terracotta, Inc.. All Rights Reserved.