Top Description Inners Fields Constructors Methods
org.codehaus.groovy.classgen.asm

public Class BytecodeHelper

extends Object
implements Opcodes
Class Inheritance
All Implemented Interfaces
org.objectweb.asm.Opcodes
Imports
org.codehaus.groovy.ast.ClassHelper, .ClassNode, .CompileUnit, .GenericsType, .MethodNode, .Parameter, org.codehaus.groovy.classgen.asm.util.TypeUtil, org.codehaus.groovy.reflection.ReflectionCache, org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation, org.objectweb.asm.Label, .MethodVisitor, .Opcodes, java.lang.reflect.Modifier

A helper class for bytecode generation with AsmClassGenerator.

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
private abstract static class
private static class
private static class

Field Summary

Modifier and TypeField and Description
private static String

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
private static void
public static boolean
box(MethodVisitor mv, ClassNode type)

Deprecated
Generates the bytecode to autobox the current value on the stack.
public static boolean
box(MethodVisitor mv, Class<T> type)

Deprecated
Generates the bytecode to autobox the current value on the stack.
private static void
box(MethodVisitor mv, String typeDescription)

private static void
private static void
public static void
convertPrimitiveToBoolean(MethodVisitor
method visitor
mv
,
ClassNode
primitive type to convert
type
)

Converts a primitive type to boolean.

public static void
public static void
doCast(MethodVisitor mv, Class<T> type)

public static void
doCastToPrimitive(MethodVisitor
method visitor
mv
,
ClassNode
the wrapped number type
sourceType
,
ClassNode
the primitive target type
targetType
)

Given a wrapped number type (Byte, Integer, Short, ...), generates bytecode to convert it to a primitive number (int, long, double) using calls to wrapped.[targetType]Value()

public static void
doCastToWrappedType(MethodVisitor
method visitor
mv
,
ClassNode
the primitive number type
sourceType
,
ClassNode
the wrapped target type
targetType
)

Given a primitive number type (byte, integer, short, ...), generates bytecode to convert it to a wrapped number (Integer, Long, Double) using calls to [WrappedType].valueOf

public static void
public static String
formatNameForClassLoading(String name)

returns a name that Class.forName() can take.

public static String

Returns:

the ASM internal name of the type
getClassInternalName
(ClassNode t)

public static String

Returns:

the ASM internal name of the type
getClassInternalName
(Class<T> t)

public static String

Returns:

the ASM internal name of the type
getClassInternalName
(String name)

public static String[]

Returns:

an array of ASM internal names of the type
getClassInternalNames
(ClassNode[] names)

public static String

Returns:

the ASM type description for class loading
getClassLoadingTypeDescription
(ClassNode c)

array types are special: eg.: String[]: classname: [Ljava.lang.String; Object: classname: java.lang.Object int[] : classname: [I unlike getTypeDescription '.' is not replaced by '/'.

public static String
public static String
public static String
private static void
public static String

Returns:

a method descriptor as defined in section JVMS section 4.3.3
getMethodDescriptor
(MethodNode
the method node for which to create the descriptor
methodNode
)

Returns a method descriptor for the given org.codehaus.groovy.ast.MethodNode.

public static String

Returns:

the ASM method type descriptor
getMethodDescriptor
(ClassNode returnType, Parameter[] parameters)

public static String

Returns:

the ASM method type descriptor
getMethodDescriptor
(ClassNode returnType, ClassNode[] parameterTypes)

public static String

Returns:

the ASM method type descriptor
getMethodDescriptor
(Class<T> returnType, Class<T>[] paramTypes)

public static String
public static String

Returns:

the ASM type description
getTypeDescription
(ClassNode c)

array types are special: eg.: String[]: classname: [Ljava/lang/String; int[]: [I

private static String

Returns:

the ASM type description
getTypeDescription
(ClassNode c, boolean end)

array types are special: eg.: String[]: classname: [Ljava/lang/String; int[]: [I

private static boolean
private static boolean
public static int

Returns:

hashcode of the string
hashCode
(String
the string for which to compute the hashcode
str
)

Computes a hash code for a string.

public static boolean

Returns:

true if the bytecode can be optimized
isClassLiteralPossible
(ClassNode
the classnode for which we want to know if bytecode optimization is possible
classNode
)

Tells if a class node is candidate for class literal bytecode optimization.

public static boolean

Returns:

true if both classes share the same compilation unit
isSameCompilationUnit
(ClassNode
class a
a
,
ClassNode
class b
b
)

Returns true if the two classes share the same compilation unit.

public static void
load(MethodVisitor mv, ClassNode type, int idx)

public static void
negateBoolean(MethodVisitor mv)

Negate a boolean on stack.

public static void
pushConstant(MethodVisitor mv, int value)

public static void
store(MethodVisitor mv, ClassNode type, int idx)

public static void
unbox(MethodVisitor mv, ClassNode type)

Generates the bytecode to unbox the current value on the stack.

public static void
unbox(MethodVisitor mv, Class<T> type)

Generates the bytecode to unbox the current value on the stack.

private static void
unbox(MethodVisitor mv, String typeName, String typeDescription)

private static boolean
public static void
visitClassLiteral(MethodVisitor mv, ClassNode classNode)

Visits a class literal.

private static void
writeGenericsBounds(StringBuilder ret, GenericsType type, boolean writeInterfaceMarker)

private static void
writeGenericsBoundType(StringBuilder ret, ClassNode printType, boolean writeInterfaceMarker)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

DTT_CLASSNAMEback to summary
private static String DTT_CLASSNAME

Constructor Detail

BytecodeHelperback to summary
public BytecodeHelper()

Method Detail

addSubTypesback to summary
private static void addSubTypes(StringBuilder ret, GenericsType[] types, String start, String end)
boxback to summary
public static boolean box(MethodVisitor mv, ClassNode type)

Deprecated

Generates the bytecode to autobox the current value on the stack.

Annotations
@Deprecated
boxback to summary
public static boolean box(MethodVisitor mv, Class<T> type)

Deprecated

Generates the bytecode to autobox the current value on the stack.

Annotations
@Deprecated
boxback to summary
private static void box(MethodVisitor mv, String typeDescription)
convertDoubleToBooleanback to summary
private static void convertDoubleToBoolean(MethodVisitor mv)
convertFloatToBooleanback to summary
private static void convertFloatToBoolean(MethodVisitor mv)
convertPrimitiveToBooleanback to summary
public static void convertPrimitiveToBoolean(MethodVisitor mv, ClassNode type)

Converts a primitive type to boolean.

Parameters
mv:MethodVisitor

method visitor

type:ClassNode

primitive type to convert

doCastback to summary
public static void doCast(MethodVisitor mv, ClassNode type)
doCastback to summary
public static void doCast(MethodVisitor mv, Class<T> type)
doCastToPrimitiveback to summary
public static void doCastToPrimitive(MethodVisitor mv, ClassNode sourceType, ClassNode targetType)

Given a wrapped number type (Byte, Integer, Short, ...), generates bytecode to convert it to a primitive number (int, long, double) using calls to wrapped.[targetType]Value()

Parameters
mv:MethodVisitor

method visitor

sourceType:ClassNode

the wrapped number type

targetType:ClassNode

the primitive target type

doCastToWrappedTypeback to summary
public static void doCastToWrappedType(MethodVisitor mv, ClassNode sourceType, ClassNode targetType)

Given a primitive number type (byte, integer, short, ...), generates bytecode to convert it to a wrapped number (Integer, Long, Double) using calls to [WrappedType].valueOf

Parameters
mv:MethodVisitor

method visitor

sourceType:ClassNode

the primitive number type

targetType:ClassNode

the wrapped target type

doReturnback to summary
public static void doReturn(MethodVisitor mv, ClassNode type)
formatNameForClassLoadingback to summary
public static String formatNameForClassLoading(String name)

returns a name that Class.forName() can take. Notably for arrays: [I, [Ljava.lang.String; etc Regular object type: java.lang.String

getClassInternalNameback to summary
public static String getClassInternalName(ClassNode t)
Returns:String

the ASM internal name of the type

getClassInternalNameback to summary
public static String getClassInternalName(Class<T> t)
Returns:String

the ASM internal name of the type

getClassInternalNameback to summary
public static String getClassInternalName(String name)
Returns:String

the ASM internal name of the type

getClassInternalNamesback to summary
public static String[] getClassInternalNames(ClassNode[] names)
Returns:String[]

an array of ASM internal names of the type

getClassLoadingTypeDescriptionback to summary
public static String getClassLoadingTypeDescription(ClassNode c)

array types are special: eg.: String[]: classname: [Ljava.lang.String; Object: classname: java.lang.Object int[] : classname: [I unlike getTypeDescription '.' is not replaced by '/'. it seems that makes problems for the class loading if '.' is replaced by '/'

Returns:String

the ASM type description for class loading

getGenericsBoundsback to summary
public static String getGenericsBounds(ClassNode type)
getGenericsMethodSignatureback to summary
public static String getGenericsMethodSignature(MethodNode node)
getGenericsSignatureback to summary
public static String getGenericsSignature(ClassNode node)
getGenericsTypeSpecback to summary
private static void getGenericsTypeSpec(StringBuilder ret, GenericsType[] genericsTypes)
getMethodDescriptorback to summary
public static String getMethodDescriptor(MethodNode methodNode)

Returns a method descriptor for the given org.codehaus.groovy.ast.MethodNode.

Parameters
methodNode:MethodNode

the method node for which to create the descriptor

Returns:String

a method descriptor as defined in section JVMS section 4.3.3

getMethodDescriptorback to summary
public static String getMethodDescriptor(ClassNode returnType, Parameter[] parameters)
Returns:String

the ASM method type descriptor

getMethodDescriptorback to summary
public static String getMethodDescriptor(ClassNode returnType, ClassNode[] parameterTypes)
Returns:String

the ASM method type descriptor

getMethodDescriptorback to summary
public static String getMethodDescriptor(Class<T> returnType, Class<T>[] paramTypes)
Returns:String

the ASM method type descriptor

getTypeDescriptionback to summary
public static String getTypeDescription(Class<T> c)
getTypeDescriptionback to summary
public static String getTypeDescription(ClassNode c)

array types are special: eg.: String[]: classname: [Ljava/lang/String; int[]: [I

Returns:String

the ASM type description

getTypeDescriptionback to summary
private static String getTypeDescription(ClassNode c, boolean end)

array types are special: eg.: String[]: classname: [Ljava/lang/String; int[]: [I

Returns:String

the ASM type description

hasGenericsback to summary
private static boolean hasGenerics(Parameter[] param)
hasGenericsback to summary
private static boolean hasGenerics(ClassNode type)
hashCodeback to summary
public static int hashCode(String str)

Computes a hash code for a string. The purpose of this hashcode is to be constant independently of the JDK being used.

Parameters
str:String

the string for which to compute the hashcode

Returns:int

hashcode of the string

isClassLiteralPossibleback to summary
public static boolean isClassLiteralPossible(ClassNode classNode)

Tells if a class node is candidate for class literal bytecode optimization. If so, bytecode may use LDC instructions instead of static constant Class fields to retrieve class literals.

Parameters
classNode:ClassNode

the classnode for which we want to know if bytecode optimization is possible

Returns:boolean

true if the bytecode can be optimized

isSameCompilationUnitback to summary
public static boolean isSameCompilationUnit(ClassNode a, ClassNode b)

Returns true if the two classes share the same compilation unit.

Parameters
a:ClassNode

class a

b:ClassNode

class b

Returns:boolean

true if both classes share the same compilation unit

loadback to summary
public static void load(MethodVisitor mv, ClassNode type, int idx)
negateBooleanback to summary
public static void negateBoolean(MethodVisitor mv)

Negate a boolean on stack.

pushConstantback to summary
public static void pushConstant(MethodVisitor mv, int value)
storeback to summary
public static void store(MethodVisitor mv, ClassNode type, int idx)
unboxback to summary
public static void unbox(MethodVisitor mv, ClassNode type)

Generates the bytecode to unbox the current value on the stack.

unboxback to summary
public static void unbox(MethodVisitor mv, Class<T> type)

Generates the bytecode to unbox the current value on the stack.

unboxback to summary
private static void unbox(MethodVisitor mv, String typeName, String typeDescription)
usesGenericsInClassSignatureback to summary
private static boolean usesGenericsInClassSignature(ClassNode node)
visitClassLiteralback to summary
public static void visitClassLiteral(MethodVisitor mv, ClassNode classNode)

Visits a class literal. If the type of the classnode is a primitive type, the generated bytecode will be a GETSTATIC Integer.TYPE. If the classnode is not a primitive type, we will generate a LDC instruction.

writeGenericsBoundsback to summary
private static void writeGenericsBounds(StringBuilder ret, GenericsType type, boolean writeInterfaceMarker)
writeGenericsBoundTypeback to summary
private static void writeGenericsBoundType(StringBuilder ret, ClassNode printType, boolean writeInterfaceMarker)
org.codehaus.groovy.classgen.asm back to summary

private Class BytecodeHelper.LoadVarHandler

extends PrimitiveTypeHandler
Class Inheritance

Field Summary

Modifier and TypeField and Description
private int
private MethodVisitor

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
protected void
protected void
protected void
handleIntType()

Implements abstract org.codehaus.groovy.classgen.asm.BytecodeHelper.PrimitiveTypeHandler.handleIntType.

boolean, char, byte, int, short types are handle in the same way
protected void
protected void
protected void
Inherited from org.codehaus.groovy.classgen.asm.BytecodeHelper.PrimitiveTypeHandler:
handle

Field Detail

idxback to summary
private int idx
mvback to summary
private MethodVisitor mv

Constructor Detail

LoadVarHandlerback to summary
public LoadVarHandler(MethodVisitor mv, ClassNode type, int idx)

Method Detail

handleDoubleTypeback to summary
protected void handleDoubleType()

Implements abstract org.codehaus.groovy.classgen.asm.BytecodeHelper.PrimitiveTypeHandler.handleDoubleType.

Annotations
@Override
handleFloatTypeback to summary
protected void handleFloatType()

Implements abstract org.codehaus.groovy.classgen.asm.BytecodeHelper.PrimitiveTypeHandler.handleFloatType.

Annotations
@Override
handleIntTypeback to summary
protected void handleIntType()

Implements abstract org.codehaus.groovy.classgen.asm.BytecodeHelper.PrimitiveTypeHandler.handleIntType.

Doc from org.codehaus.groovy.classgen.asm.BytecodeHelper.PrimitiveTypeHandler.handleIntType.

boolean, char, byte, int, short types are handle in the same way

Annotations
@Override
handleLongTypeback to summary
protected void handleLongType()

Implements abstract org.codehaus.groovy.classgen.asm.BytecodeHelper.PrimitiveTypeHandler.handleLongType.

Annotations
@Override
handleRefTypeback to summary
protected void handleRefType()

Implements abstract org.codehaus.groovy.classgen.asm.BytecodeHelper.PrimitiveTypeHandler.handleRefType.

Annotations
@Override
handleVoidTypeback to summary
protected void handleVoidType()

Implements abstract org.codehaus.groovy.classgen.asm.BytecodeHelper.PrimitiveTypeHandler.handleVoidType.

Annotations
@Override
org.codehaus.groovy.classgen.asm back to summary

private abstract Class BytecodeHelper.PrimitiveTypeHandler

extends Object
Class Inheritance
  • java.lang.Object
  • org.codehaus.groovy.classgen.asm.BytecodeHelper.PrimitiveTypeHandler
Known Direct Subclasses
org.codehaus.groovy.classgen.asm.BytecodeHelper.ReturnVarHandler, org.codehaus.groovy.classgen.asm.BytecodeHelper.LoadVarHandler, org.codehaus.groovy.classgen.asm.BytecodeHelper.StoreVarHandler

Field Summary

Modifier and TypeField and Description
private ClassNode

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public void
protected abstract void
protected abstract void
protected abstract void
handleIntType()

boolean, char, byte, int, short types are handle in the same way

protected abstract void
protected abstract void
protected abstract void
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

typeback to summary
private ClassNode type

Constructor Detail

PrimitiveTypeHandlerback to summary
public PrimitiveTypeHandler(ClassNode type)

Method Detail

handleback to summary
public void handle()
handleDoubleTypeback to summary
protected abstract void handleDoubleType()
handleFloatTypeback to summary
protected abstract void handleFloatType()
handleIntTypeback to summary
protected abstract void handleIntType()

boolean, char, byte, int, short types are handle in the same way

handleLongTypeback to summary
protected abstract void handleLongType()
handleRefTypeback to summary
protected abstract void handleRefType()
handleVoidTypeback to summary
protected abstract void handleVoidType()
org.codehaus.groovy.classgen.asm back to summary

private Class BytecodeHelper.ReturnVarHandler

extends PrimitiveTypeHandler
Class Inheritance

Field Summary

Modifier and TypeField and Description
private MethodVisitor

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
protected void
protected void
protected void
handleIntType()

Implements abstract org.codehaus.groovy.classgen.asm.BytecodeHelper.PrimitiveTypeHandler.handleIntType.

boolean, char, byte, int, short types are handle in the same way
protected void
protected void
protected void
Inherited from org.codehaus.groovy.classgen.asm.BytecodeHelper.PrimitiveTypeHandler:
handle

Field Detail

mvback to summary
private MethodVisitor mv

Constructor Detail

ReturnVarHandlerback to summary
public ReturnVarHandler(MethodVisitor mv, ClassNode type)

Method Detail

handleDoubleTypeback to summary
protected void handleDoubleType()

Implements abstract org.codehaus.groovy.classgen.asm.BytecodeHelper.PrimitiveTypeHandler.handleDoubleType.

Annotations
@Override
handleFloatTypeback to summary
protected void handleFloatType()

Implements abstract org.codehaus.groovy.classgen.asm.BytecodeHelper.PrimitiveTypeHandler.handleFloatType.

Annotations
@Override
handleIntTypeback to summary
protected void handleIntType()

Implements abstract org.codehaus.groovy.classgen.asm.BytecodeHelper.PrimitiveTypeHandler.handleIntType.

Doc from org.codehaus.groovy.classgen.asm.BytecodeHelper.PrimitiveTypeHandler.handleIntType.

boolean, char, byte, int, short types are handle in the same way

Annotations
@Override
handleLongTypeback to summary
protected void handleLongType()

Implements abstract org.codehaus.groovy.classgen.asm.BytecodeHelper.PrimitiveTypeHandler.handleLongType.

Annotations
@Override
handleRefTypeback to summary
protected void handleRefType()

Implements abstract org.codehaus.groovy.classgen.asm.BytecodeHelper.PrimitiveTypeHandler.handleRefType.

Annotations
@Override
handleVoidTypeback to summary
protected void handleVoidType()

Implements abstract org.codehaus.groovy.classgen.asm.BytecodeHelper.PrimitiveTypeHandler.handleVoidType.

Annotations
@Override
org.codehaus.groovy.classgen.asm back to summary

private Class BytecodeHelper.StoreVarHandler

extends PrimitiveTypeHandler
Class Inheritance

Field Summary

Modifier and TypeField and Description
private int
private MethodVisitor

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
protected void
protected void
protected void
handleIntType()

Implements abstract org.codehaus.groovy.classgen.asm.BytecodeHelper.PrimitiveTypeHandler.handleIntType.

boolean, char, byte, int, short types are handle in the same way
protected void
protected void
protected void
Inherited from org.codehaus.groovy.classgen.asm.BytecodeHelper.PrimitiveTypeHandler:
handle

Field Detail

idxback to summary
private int idx
mvback to summary
private MethodVisitor mv

Constructor Detail

StoreVarHandlerback to summary
public StoreVarHandler(MethodVisitor mv, ClassNode type, int idx)

Method Detail

handleDoubleTypeback to summary
protected void handleDoubleType()

Implements abstract org.codehaus.groovy.classgen.asm.BytecodeHelper.PrimitiveTypeHandler.handleDoubleType.

Annotations
@Override
handleFloatTypeback to summary
protected void handleFloatType()

Implements abstract org.codehaus.groovy.classgen.asm.BytecodeHelper.PrimitiveTypeHandler.handleFloatType.

Annotations
@Override
handleIntTypeback to summary
protected void handleIntType()

Implements abstract org.codehaus.groovy.classgen.asm.BytecodeHelper.PrimitiveTypeHandler.handleIntType.

Doc from org.codehaus.groovy.classgen.asm.BytecodeHelper.PrimitiveTypeHandler.handleIntType.

boolean, char, byte, int, short types are handle in the same way

Annotations
@Override
handleLongTypeback to summary
protected void handleLongType()

Implements abstract org.codehaus.groovy.classgen.asm.BytecodeHelper.PrimitiveTypeHandler.handleLongType.

Annotations
@Override
handleRefTypeback to summary
protected void handleRefType()

Implements abstract org.codehaus.groovy.classgen.asm.BytecodeHelper.PrimitiveTypeHandler.handleRefType.

Annotations
@Override
handleVoidTypeback to summary
protected void handleVoidType()

Implements abstract org.codehaus.groovy.classgen.asm.BytecodeHelper.PrimitiveTypeHandler.handleVoidType.

Annotations
@Override