Class CompactCodeAttributeComposer
- All Implemented Interfaces:
AttributeVisitor
AttributeVisitor accumulates instructions, exceptions and line numbers, in a compact
and fluent style, and then adds them to a method or copies them into code attributes that it
visits.
The class supports composing instructions (appendInstruction(Instruction)), labels
(createLabel() and label(Label)), exception handlers (catch_(Label, Label, String, Clazz)), and line numbers (line(int)).
The labels are numeric labels that you can choose freely, for example instruction offsets from existing code that you are copying. You can then refer to them in branches and exception handlers. You can compose the code as a hierarchy of code fragments with their own local labels.
You should provide an estimated maximum size (expressed in number of bytes in the bytecode), so the implementation can efficiently allocate the necessary internal buffers without reallocating them as the code grows.
For example:
ProgramClass programClass = ...
ProgramMethod programMethod = ...
// Compose the code.
CompactCodeAttributeComposer composer =
new CompactCodeAttributeComposer(programClass);
final Label TRY_START = composer.createLabel();
final Label TRY_END = composer.createLabel();
final Label ELSE = composer.createLabel();
composer
.beginCodeFragment(50)
.label(TRY_START)
.iconst_1()
.iconst_2()
.ificmplt(ELSE)
.iconst_1()
.ireturn()
.label(ELSE)
.iconst_2()
.ireturn()
.label(TRY_END)
.catch_(TRY_START, TRY_END, "java/lang/Exception", null)
.iconst_m1()
.ireturn()
.endCodeFragment();
// Add the code as a code attribute to the given method.
composer.addCodeAttribute(programClass, programMethod);
This class is mostly convenient to compose code programmatically from scratch. To compose code
based on existing code, where the instructions are already available, see CodeAttributeComposer.
If you're building many method bodies, it is more efficient to reuse a single instance of this composer for all methods that you add.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionclassThis class represents a label to which branch instructions and switch instructions can jump. -
Constructor Summary
ConstructorsConstructorDescriptionCompactCodeAttributeComposer(ConstantPoolEditor constantPoolEditor, boolean allowExternalBranchTargets, boolean allowExternalExceptionOffsets, boolean shrinkInstructions) Creates a new CompactCodeAttributeComposer.CompactCodeAttributeComposer(ConstantPoolEditor constantPoolEditor, CodeAttributeComposer codeAttributeComposer) Creates a new CompactCodeAttributeComposer.CompactCodeAttributeComposer(ProgramClass targetClass) Creates a new CompactCodeAttributeComposer that doesn't allow external branch targets or exception offsets and that automatically shrinks instructions.CompactCodeAttributeComposer(ProgramClass targetClass, boolean allowExternalBranchTargets, boolean allowExternalExceptionOffsets, boolean shrinkInstructions) Creates a new CompactCodeAttributeComposer.CompactCodeAttributeComposer(ProgramClass targetClass, boolean allowExternalBranchTargets, boolean allowExternalExceptionOffsets, boolean shrinkInstructions, ClassPool programClassPool, ClassPool libraryClassPool) Creates a new CompactCodeAttributeComposer.CompactCodeAttributeComposer(ProgramClass targetClass, ClassPool programClassPool, ClassPool libraryClassPool) Creates a new CompactCodeAttributeComposer that doesn't allow external branch targets or exception handlers and that automatically shrinks instructions. -
Method Summary
Modifier and TypeMethodDescriptionaaload()aastore()voidaddCodeAttribute(ProgramClass programClass, ProgramMethod programMethod) Adds the code that has been built as a code attribute to the given method.aload(int variableIndex) aload_0()aload_1()aload_2()aload_3()anewarray(int constantIndex) appendInstruction(Instruction instruction) Appends the given instruction.appendInstructions(Instruction[] instructions) Appends the given instruction without defined offsets.appendPrintInstructions(String message) Appends instructions to print out the given message.Appends instructions to print out the top integer on the stack as a hexadecimal value.appendPrintIntegerHexInstructions(String message) Appends instructions to print out the given message and the top int on the stack as a hexadecimal value.Appends instructions to print out the top int on the stack.appendPrintIntegerInstructions(String message) Appends instructions to print out the given message and the top int on the stack.Appends instructions to print out the top long on the stack.appendPrintLongInstructions(String message) Appends instructions to print out the given message and the top long on the stack.Appends instructions to print out the top Object on the stack.appendPrintObjectInstructions(String message) Appends instructions to print out the given message and the top Object on the stack.Appends instructions to print out the stack trace of the top Throwable on the stack.appendPrintStackTraceInstructions(String message) Appends instructions to print out the given message and the stack trace of the top Throwable on the stack.Appends instructions to print out the top String on the stack.appendPrintStringInstructions(String message) Appends instructions to print out the given message and the top String on the stack.areturn()astore(int variableIndex) astore_0()astore_1()astore_2()astore_3()athrow()baload()bastore()beginCodeFragment(int maximumCodeFragmentLength) Starts a new code fragment.bipush(int constant) boxPrimitiveType(char sourceType) Box the primitive value present on the stack.caload()castore()catch_(CompactCodeAttributeComposer.Label startLabel, CompactCodeAttributeComposer.Label endLabel, String catchType, Clazz referencedClass) Starts a catch handler at the current offset.catch_(CompactCodeAttributeComposer.Label startLabel, CompactCodeAttributeComposer.Label endLabel, CompactCodeAttributeComposer.Label handlerLabel, String catchType, Clazz referencedClass) Adds a catch handler.catchAll(CompactCodeAttributeComposer.Label startLabel, CompactCodeAttributeComposer.Label endLabel) Starts a catch-all handler at the current offset.catchAll(CompactCodeAttributeComposer.Label startLabel, CompactCodeAttributeComposer.Label endLabel, CompactCodeAttributeComposer.Label handlerLabel) Adds a catch-all handler.checkcast(int constantIndex) convertPrimitiveType(char source, char target) Add instructions to convert the primitive on the stack to a different primitive type.convertToTargetType(String sourceType, String targetType) Adds the required instructions to the provided CodeAttributeComposer to convert the current value on the stack to the given targetType.Creates a new label that can be specified and used in the code.d2f()d2i()d2l()dadd()daload()dastore()dcmpg()dcmpl()dconst(int constant) dconst_0()dconst_1()ddiv()dload(int variableIndex) dload_0()dload_1()dload_2()dload_3()dmul()dneg()drem()dreturn()dstore(int variableIndex) dstore_0()dstore_1()dstore_2()dstore_3()dsub()dup()dup_x1()dup_x2()dup2()dup2_x1()dup2_x2()Wraps up the current code fragment, continuing with the previous one on the stack.f2d()f2i()f2l()fadd()faload()fastore()fcmpg()fcmpl()fconst(int constant) fconst_0()fconst_1()fconst_2()fdiv()fload(int variableIndex) fload_0()fload_1()fload_2()fload_3()fmul()fneg()frem()freturn()fstore(int variableIndex) fstore_0()fstore_1()fstore_2()fstore_3()fsub()intReturns the current length (in bytes) of the code attribute being composed.Returns a ConstantPoolEditor instance for the created or edited class instance.getfield(int constantIndex) getfield(String className, String name, String descriptor, Clazz referencedClass, Field referencedField) getstatic(int constantIndex) getstatic(String className, String name, String descriptor, Clazz referencedClass, Field referencedField) Returns the target class for which code is generated.goto_(CompactCodeAttributeComposer.Label branchLabel) goto_w(CompactCodeAttributeComposer.Label branchLabel) i2b()i2c()i2d()i2f()i2l()i2s()iadd()iaload()iand()iastore()iconst(int constant) iconst_0()iconst_1()iconst_2()iconst_3()iconst_4()iconst_5()idiv()ifacmpeq(CompactCodeAttributeComposer.Label branchLabel) ifacmpne(CompactCodeAttributeComposer.Label branchLabel) ifeq(CompactCodeAttributeComposer.Label branchLabel) ifge(CompactCodeAttributeComposer.Label branchLabel) ifgt(CompactCodeAttributeComposer.Label branchLabel) ificmpeq(CompactCodeAttributeComposer.Label branchLabel) ificmpge(CompactCodeAttributeComposer.Label branchLabel) ificmpgt(CompactCodeAttributeComposer.Label branchLabel) ificmple(CompactCodeAttributeComposer.Label branchLabel) ificmplt(CompactCodeAttributeComposer.Label branchLabel) ificmpne(CompactCodeAttributeComposer.Label branchLabel) ifle(CompactCodeAttributeComposer.Label branchLabel) iflt(CompactCodeAttributeComposer.Label branchLabel) ifne(CompactCodeAttributeComposer.Label branchLabel) ifnonnull(CompactCodeAttributeComposer.Label branchLabel) ifnull(CompactCodeAttributeComposer.Label branchLabel) iinc(int variableIndex, int constant) iload(int variableIndex) iload_0()iload_1()iload_2()iload_3()imul()ineg()instanceof_(int constantIndex) instanceof_(String className, Clazz referencedClass) invoke(byte opcode, String className, String name, String descriptor, Clazz referencedClass, Method referencedMethod) invokedynamic(int constantIndex) invokedynamic(int bootStrapMethodIndex, String name, String descriptor, Clazz[] referencedClasses) invokeinterface(int constantIndex, int constant) invokeinterface(String className, String name, String descriptor) invokeinterface(String className, String name, String descriptor, Clazz referencedClass, Method referencedMethod) invokeinterface(Clazz clazz, Method method) invokespecial(int constantIndex) invokespecial(String className, String name, String descriptor) invokespecial(String className, String name, String descriptor, Clazz referencedClass, Method referencedMethod) invokespecial(Clazz clazz, Method method) invokestatic(int constantIndex) invokestatic(String className, String name, String descriptor) invokestatic(String className, String name, String descriptor, Clazz referencedClass, Method referencedMethod) invokestatic(Clazz clazz, Method method) invokestatic_interface(String className, String name, String descriptor) invokestatic_interface(String className, String name, String descriptor, Clazz referencedClass, Method referencedMethod) invokestatic_interface(Clazz clazz, Method method) invokevirtual(int constantIndex) invokevirtual(String className, String name, String descriptor) invokevirtual(String className, String name, String descriptor, Clazz referencedClass, Method referencedMethod) invokevirtual(Clazz clazz, Method method) ior()irem()ireturn()ishl()ishr()istore(int variableIndex) istore_0()istore_1()istore_2()istore_3()isub()iushr()ixor()jsr(CompactCodeAttributeComposer.Label branchLabel) jsr_w(CompactCodeAttributeComposer.Label branchLabel) l2d()l2f()l2i()Appends the given label at the current offset, so branch instructions and switch instructions can jump to it.ladd()laload()land()lastore()lcmp()lconst(int constant) lconst_0()lconst_1()ldc(float value) Appends an ldc instruction that loads a float constant with the given value.ldc(int value) Appends an ldc instruction that loads an integer constant with the given value.Appends an ldc instruction that loads an (internal) primitive array constant with the given value.Appends an ldc instruction that loads a string constant with the given value.Appends an ldc instruction that loads a class constant for the given type name, that references the given class.Appends an ldc instruction that loads a string constant with the given value, that references the given class member.ldc(String string, ResourceFile referencedResourceFile) Appends an ldc instruction that loads a string constant with the given value, that references the given resource file.Appends an ldc instruction that loads a class constant for the given class.Appends an ldc instruction that loads a string constant with the given class member name.ldc(ResourceFile resourceFile) Appends an ldc instruction that loads a string constant with the given resource file name.ldc_(int constantIndex) Appends an ldc instruction that loads the constant at the given index.ldc_w(float value) Appends an ldc_w instruction that loads a float constant with the given value.ldc_w(int value) Appends an ldc_w instruction that loads an integer constant with the given value.Appends an ldc_w instruction that loads an (internal) primitive array constant with the given value.Appends an ldc_w instruction that loads a string constant with the given value.Appends an ldc_w instruction that loads a class constant for the given type name, that references the given class.Appends an ldc_w instruction that loads a string constant with the given value, that references the given class member.ldc_w(String string, ResourceFile referencedResourceFile) Appends an ldc_w instruction that loads a string constant with the given value, that references the given resource file.Appends an ldc_w instruction that loads a class constant for the given class.Appends an ldc_w instruction that loads a string constant with the given class member name.ldc_w(ResourceFile resourceFile) Appends an ldc_w instruction that loads a string constant with the given resource file name.ldc_w_(int constantIndex) Appends an ldc_w instruction that loads the constant at the given index.ldc2_w(double value) Appends an ldc2_w instruction that loads a double constant with the given value.ldc2_w(int constantIndex) Appends an ldc2_w instruction that loads the Category 2 constant at the given index.ldc2_w(long value) Appends an ldc2_w instruction that loads a long constant with the given value.ldiv()line(int lineNumber) Adds a source line number for the current position.lload(int variableIndex) lload_0()lload_1()lload_2()lload_3()lmul()lneg()load(int variableIndex, char internalType) Loads the given variable of primitive type onto the stack.Loads the given variable onto the stack.lookupswitch(CompactCodeAttributeComposer.Label defaultLabel, int[] cases, CompactCodeAttributeComposer.Label[] jumpLabels) lor()lrem()lreturn()lshl()lshr()lstore(int variableIndex) lstore_0()lstore_1()lstore_2()lstore_3()lsub()lushr()lxor()static voidSmall sample application that illustrates the use of this class.multianewarray(int constantIndex, int dimensions) multianewarray(String className, Clazz referencedClass, int dimensions) new_(int constantIndex) newarray(int constant) nop()pop()pop2()pushDouble(double value) Pushes the given primitive double on the stack in the most efficient way (as a dconst or ldc instruction).pushFloat(float value) Pushes the given primitive float on the stack in the most efficient way (as an fconst or ldc instruction).pushInt(int value) Pushes the given primitive int on the stack in the most efficient way (as an iconst, bipush, sipush, or ldc instruction).pushLong(long value) Pushes the given primitive long on the stack in the most efficient way (as an lconst or ldc instruction).pushNewArray(String elementTypeOrClassName, int size) Pushes a new array on the stack.pushPrimitive(Object primitive, char internalType) Pushes the given primitive value on the stack.putfield(int constantIndex) putfield(String className, String name, String descriptor, Clazz referencedClass, Field referencedField) putstatic(int constantIndex) putstatic(String className, String name, String descriptor, Clazz referencedClass, Field referencedField) reset()Starts a new code definition.ret(int variableIndex) return_()Appends the proper return statement for the given internal type.saload()sastore()sipush(int constant) store(int variableIndex, char internalType) Stores the primitve value on top of the stack in the variable with given index.Stores the value on top of the stack in the variable with given index.storeToArray(String elementType) Stores an element to an array.swap()tableswitch(CompactCodeAttributeComposer.Label defaultLabel, int lowCase, int highCase, CompactCodeAttributeComposer.Label[] jumpLabels) unboxPrimitiveType(String sourceType, String targetType) Unbox the object on the stack to a primitive value.voidvisitAnyAttribute(Clazz clazz, Attribute attribute) Visits any Attribute instance.voidvisitCodeAttribute(Clazz clazz, Method method, CodeAttribute codeAttribute) Sets the code that has been built in the given code attribute.wide()Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface proguard.classfile.attribute.visitor.AttributeVisitor
visitAnnotationDefaultAttribute, visitAnyAnnotationsAttribute, visitAnyParameterAnnotationsAttribute, visitAnyTypeAnnotationsAttribute, visitBootstrapMethodsAttribute, visitConstantValueAttribute, visitDeprecatedAttribute, visitDeprecatedAttribute, visitDeprecatedAttribute, visitDeprecatedAttribute, visitEnclosingMethodAttribute, visitExceptionsAttribute, visitInnerClassesAttribute, visitLineNumberTableAttribute, visitLocalVariableTableAttribute, visitLocalVariableTypeTableAttribute, visitMethodParametersAttribute, visitModuleAttribute, visitModuleMainClassAttribute, visitModulePackagesAttribute, visitNestHostAttribute, visitNestMembersAttribute, visitPermittedSubclassesAttribute, visitRecordAttribute, visitRuntimeInvisibleAnnotationsAttribute, visitRuntimeInvisibleAnnotationsAttribute, visitRuntimeInvisibleAnnotationsAttribute, visitRuntimeInvisibleAnnotationsAttribute, visitRuntimeInvisibleAnnotationsAttribute, visitRuntimeInvisibleParameterAnnotationsAttribute, visitRuntimeInvisibleTypeAnnotationsAttribute, visitRuntimeInvisibleTypeAnnotationsAttribute, visitRuntimeInvisibleTypeAnnotationsAttribute, visitRuntimeInvisibleTypeAnnotationsAttribute, visitRuntimeInvisibleTypeAnnotationsAttribute, visitRuntimeInvisibleTypeAnnotationsAttribute, visitRuntimeVisibleAnnotationsAttribute, visitRuntimeVisibleAnnotationsAttribute, visitRuntimeVisibleAnnotationsAttribute, visitRuntimeVisibleAnnotationsAttribute, visitRuntimeVisibleAnnotationsAttribute, visitRuntimeVisibleParameterAnnotationsAttribute, visitRuntimeVisibleTypeAnnotationsAttribute, visitRuntimeVisibleTypeAnnotationsAttribute, visitRuntimeVisibleTypeAnnotationsAttribute, visitRuntimeVisibleTypeAnnotationsAttribute, visitRuntimeVisibleTypeAnnotationsAttribute, visitRuntimeVisibleTypeAnnotationsAttribute, visitSignatureAttribute, visitSignatureAttribute, visitSignatureAttribute, visitSignatureAttribute, visitSignatureAttribute, visitSourceDebugExtensionAttribute, visitSourceDirAttribute, visitSourceFileAttribute, visitStackMapAttribute, visitStackMapTableAttribute, visitSyntheticAttribute, visitSyntheticAttribute, visitSyntheticAttribute, visitSyntheticAttribute, visitUnknownAttribute
-
Constructor Details
-
CompactCodeAttributeComposer
Creates a new CompactCodeAttributeComposer that doesn't allow external branch targets or exception offsets and that automatically shrinks instructions.- Parameters:
targetClass- the class to be edited.
-
CompactCodeAttributeComposer
public CompactCodeAttributeComposer(ProgramClass targetClass, ClassPool programClassPool, ClassPool libraryClassPool) Creates a new CompactCodeAttributeComposer that doesn't allow external branch targets or exception handlers and that automatically shrinks instructions.- Parameters:
targetClass- the class to be edited.programClassPool- the program class pool from which new constants can be initialized.libraryClassPool- the library class pool from which new constants can be initialized.
-
CompactCodeAttributeComposer
public CompactCodeAttributeComposer(ProgramClass targetClass, boolean allowExternalBranchTargets, boolean allowExternalExceptionOffsets, boolean shrinkInstructions) Creates a new CompactCodeAttributeComposer.- Parameters:
targetClass- the class to be edited.allowExternalBranchTargets- specifies whether branch targets can lie outside the code fragment of the branch instructions.allowExternalExceptionOffsets- specifies whether exception offsets can lie outside the code fragment in which exceptions are defined.shrinkInstructions- specifies whether instructions should automatically be shrunk before being written.
-
CompactCodeAttributeComposer
public CompactCodeAttributeComposer(ProgramClass targetClass, boolean allowExternalBranchTargets, boolean allowExternalExceptionOffsets, boolean shrinkInstructions, ClassPool programClassPool, ClassPool libraryClassPool) Creates a new CompactCodeAttributeComposer.- Parameters:
targetClass- the class to be edited.allowExternalBranchTargets- specifies whether branch targets can lie outside the code fragment of the branch instructions.allowExternalExceptionOffsets- specifies whether exception offsets can lie outside the code fragment in which exceptions are defined.shrinkInstructions- specifies whether instructions should automatically be shrunk before being written.programClassPool- the program class pool from which new constants can be initialized.libraryClassPool- the library class pool from which new constants can be initialized.
-
CompactCodeAttributeComposer
public CompactCodeAttributeComposer(ConstantPoolEditor constantPoolEditor, boolean allowExternalBranchTargets, boolean allowExternalExceptionOffsets, boolean shrinkInstructions) Creates a new CompactCodeAttributeComposer.- Parameters:
constantPoolEditor- an editor for the constants in the class.allowExternalBranchTargets- specifies whether branch targets can lie outside the code fragment of the branch instructions.allowExternalExceptionOffsets- specifies whether exception offsets can lie outside the code fragment in which exceptions are defined.shrinkInstructions- specifies whether instructions should automatically be shrunk before being written.
-
CompactCodeAttributeComposer
public CompactCodeAttributeComposer(ConstantPoolEditor constantPoolEditor, CodeAttributeComposer codeAttributeComposer) Creates a new CompactCodeAttributeComposer.- Parameters:
constantPoolEditor- an editor for the constants in the class.codeAttributeComposer- an composer for the instructions in the method.
-
-
Method Details
-
getTargetClass
Returns the target class for which code is generated. -
getConstantPoolEditor
Returns a ConstantPoolEditor instance for the created or edited class instance. Reusing this instance is more efficient for classes that are created from scratch. -
getCodeLength
public int getCodeLength()Returns the current length (in bytes) of the code attribute being composed. -
convertToTargetType
Adds the required instructions to the provided CodeAttributeComposer to convert the current value on the stack to the given targetType. -
boxPrimitiveType
Box the primitive value present on the stack.Operand stack: ..., primitive -> ..., boxed_primitive
- Parameters:
sourceType- type of the primitive on the stack.
-
convertPrimitiveType
Add instructions to convert the primitive on the stack to a different primitive type.- Parameters:
source- The source type.target- The target type.
-
unboxPrimitiveType
Unbox the object on the stack to a primitive value.Operand stack: ..., boxed_primitive -> ..., primitive
- Parameters:
sourceType- type of the primitive that should be unboxed.targetType- resulting type.
-
reset
Starts a new code definition. -
beginCodeFragment
Starts a new code fragment. Branch instructions that are added are assumed to be relative within such code fragments.- Parameters:
maximumCodeFragmentLength- the maximum length of the code that will be added as part of this fragment (more precisely, the maximum old instruction offset or label that is specified, plus one).- Returns:
- this instance of CompactCodeAttributeComposer.
-
createLabel
Creates a new label that can be specified and used in the code. -
label
Appends the given label at the current offset, so branch instructions and switch instructions can jump to it.- Parameters:
label- the branch label.- Returns:
- this instance of CompactCodeAttributeComposer.
-
appendInstructions
Appends the given instruction without defined offsets.- Parameters:
instructions- the instructions to be appended.- Returns:
- this instance of CompactCodeAttributeComposer.
-
appendInstruction
Appends the given instruction.- Parameters:
instruction- the instruction to be appended.- Returns:
- this instance of CompactCodeAttributeComposer.
-
catchAll
public CompactCodeAttributeComposer catchAll(CompactCodeAttributeComposer.Label startLabel, CompactCodeAttributeComposer.Label endLabel) Starts a catch-all handler at the current offset.- Parameters:
startLabel- the start label of the try block.endLabel- the end label of the try block.- Returns:
- this instance of CompactCodeAttributeComposer.
-
catchAll
public CompactCodeAttributeComposer catchAll(CompactCodeAttributeComposer.Label startLabel, CompactCodeAttributeComposer.Label endLabel, CompactCodeAttributeComposer.Label handlerLabel) Adds a catch-all handler.- Parameters:
startLabel- the start label of the try block.endLabel- the end label of the try block.handlerLabel- the label of the exception handler.- Returns:
- this instance of CompactCodeAttributeComposer.
-
catch_
public CompactCodeAttributeComposer catch_(CompactCodeAttributeComposer.Label startLabel, CompactCodeAttributeComposer.Label endLabel, String catchType, Clazz referencedClass) Starts a catch handler at the current offset.- Parameters:
startLabel- the start label of the try block.endLabel- the end label of the try block.catchType- the exception type.referencedClass- the exception class, if known.- Returns:
- this instance of CompactCodeAttributeComposer.
-
catch_
public CompactCodeAttributeComposer catch_(CompactCodeAttributeComposer.Label startLabel, CompactCodeAttributeComposer.Label endLabel, CompactCodeAttributeComposer.Label handlerLabel, String catchType, Clazz referencedClass) Adds a catch handler.- Parameters:
startLabel- the start label of the try block.endLabel- the end label of the try block.handlerLabel- the label of the exception handler.catchType- the exception type.referencedClass- the exception class, if known.- Returns:
- this instance of CompactCodeAttributeComposer.
-
line
Adds a source line number for the current position.- Parameters:
lineNumber- the line number from the source code.- Returns:
- this instance of CompactCodeAttributeComposer.
-
endCodeFragment
Wraps up the current code fragment, continuing with the previous one on the stack.- Returns:
- this instance of CompactCodeAttributeComposer.
-
nop
-
aconst_null
-
iconst
-
iconst_m1
-
iconst_0
-
iconst_1
-
iconst_2
-
iconst_3
-
iconst_4
-
iconst_5
-
lconst
-
lconst_0
-
lconst_1
-
fconst
-
fconst_0
-
fconst_1
-
fconst_2
-
dconst
-
dconst_0
-
dconst_1
-
bipush
-
sipush
-
ldc
Appends an ldc instruction that loads an integer constant with the given value. -
ldc
Appends an ldc instruction that loads a float constant with the given value. -
ldc
Appends an ldc instruction that loads a string constant with the given value. -
ldc
Appends an ldc instruction that loads an (internal) primitive array constant with the given value. -
ldc
Appends an ldc instruction that loads a string constant with the given class member name. -
ldc
public CompactCodeAttributeComposer ldc(String string, Clazz referencedClass, Member referencedMember) Appends an ldc instruction that loads a string constant with the given value, that references the given class member. -
ldc
Appends an ldc instruction that loads a string constant with the given resource file name. -
ldc
Appends an ldc instruction that loads a string constant with the given value, that references the given resource file. -
ldc
Appends an ldc instruction that loads a class constant for the given class. -
ldc
Appends an ldc instruction that loads a class constant for the given type name, that references the given class. -
ldc_
Appends an ldc instruction that loads the constant at the given index. -
ldc_w
Appends an ldc_w instruction that loads an integer constant with the given value. -
ldc_w
Appends an ldc_w instruction that loads a float constant with the given value. -
ldc_w
Appends an ldc_w instruction that loads a string constant with the given value. -
ldc_w
Appends an ldc_w instruction that loads an (internal) primitive array constant with the given value. -
ldc_w
Appends an ldc_w instruction that loads a string constant with the given class member name. -
ldc_w
public CompactCodeAttributeComposer ldc_w(String string, Clazz referencedClass, Member referencedMember) Appends an ldc_w instruction that loads a string constant with the given value, that references the given class member. -
ldc_w
Appends an ldc_w instruction that loads a string constant with the given resource file name. -
ldc_w
Appends an ldc_w instruction that loads a string constant with the given value, that references the given resource file. -
ldc_w
Appends an ldc_w instruction that loads a class constant for the given class. -
ldc_w
Appends an ldc_w instruction that loads a class constant for the given type name, that references the given class. -
ldc_w_
Appends an ldc_w instruction that loads the constant at the given index. -
ldc2_w
Appends an ldc2_w instruction that loads a long constant with the given value. -
ldc2_w
Appends an ldc2_w instruction that loads a double constant with the given value. -
ldc2_w
Appends an ldc2_w instruction that loads the Category 2 constant at the given index. -
iload
-
lload
-
fload
-
dload
-
aload
-
iload_0
-
iload_1
-
iload_2
-
iload_3
-
lload_0
-
lload_1
-
lload_2
-
lload_3
-
fload_0
-
fload_1
-
fload_2
-
fload_3
-
dload_0
-
dload_1
-
dload_2
-
dload_3
-
aload_0
-
aload_1
-
aload_2
-
aload_3
-
iaload
-
laload
-
faload
-
daload
-
aaload
-
baload
-
caload
-
saload
-
istore
-
lstore
-
fstore
-
dstore
-
astore
-
istore_0
-
istore_1
-
istore_2
-
istore_3
-
lstore_0
-
lstore_1
-
lstore_2
-
lstore_3
-
fstore_0
-
fstore_1
-
fstore_2
-
fstore_3
-
dstore_0
-
dstore_1
-
dstore_2
-
dstore_3
-
astore_0
-
astore_1
-
astore_2
-
astore_3
-
iastore
-
lastore
-
fastore
-
dastore
-
aastore
-
bastore
-
castore
-
sastore
-
pop
-
pop2
-
dup
-
dup_x1
-
dup_x2
-
dup2
-
dup2_x1
-
dup2_x2
-
swap
-
iadd
-
ladd
-
fadd
-
dadd
-
isub
-
lsub
-
fsub
-
dsub
-
imul
-
lmul
-
fmul
-
dmul
-
idiv
-
ldiv
-
fdiv
-
ddiv
-
irem
-
lrem
-
frem
-
drem
-
ineg
-
lneg
-
fneg
-
dneg
-
ishl
-
lshl
-
ishr
-
lshr
-
iushr
-
lushr
-
iand
-
land
-
ior
-
lor
-
ixor
-
lxor
-
iinc
-
i2l
-
i2f
-
i2d
-
l2i
-
l2f
-
l2d
-
f2i
-
f2l
-
f2d
-
d2i
-
d2l
-
d2f
-
i2b
-
i2c
-
i2s
-
lcmp
-
fcmpl
-
fcmpg
-
dcmpl
-
dcmpg
-
ifeq
-
ifne
-
iflt
-
ifge
-
ifgt
-
ifle
-
ificmpeq
-
ificmpne
-
ificmplt
-
ificmpge
-
ificmpgt
-
ificmple
-
ifacmpeq
-
ifacmpne
-
goto_
-
jsr
-
ret
-
tableswitch
public CompactCodeAttributeComposer tableswitch(CompactCodeAttributeComposer.Label defaultLabel, int lowCase, int highCase, CompactCodeAttributeComposer.Label[] jumpLabels) -
lookupswitch
public CompactCodeAttributeComposer lookupswitch(CompactCodeAttributeComposer.Label defaultLabel, int[] cases, CompactCodeAttributeComposer.Label[] jumpLabels) -
ireturn
-
lreturn
-
freturn
-
dreturn
-
areturn
-
return_
-
getstatic
-
getstatic
-
getstatic
-
getstatic
-
putstatic
-
putstatic
-
putstatic
-
putstatic
-
getfield
-
getfield
-
getfield
-
getfield
-
putfield
-
putfield
-
putfield
-
putfield
-
invoke
-
invoke
public CompactCodeAttributeComposer invoke(byte opcode, Clazz referencedClass, Method referencedMethod) -
invoke
-
invokevirtual
-
invokevirtual
-
invokevirtual
-
invokevirtual
-
invokespecial
-
invokespecial
-
invokespecial
-
invokespecial
-
invokestatic
-
invokestatic
-
invokestatic
-
invokestatic_interface
-
invokestatic_interface
public CompactCodeAttributeComposer invokestatic_interface(String className, String name, String descriptor) -
invokestatic_interface
-
invokestatic
-
invokeinterface
-
invokeinterface
public CompactCodeAttributeComposer invokeinterface(String className, String name, String descriptor) -
invokeinterface
-
invokeinterface
-
invokedynamic
public CompactCodeAttributeComposer invokedynamic(int bootStrapMethodIndex, String name, String descriptor, Clazz[] referencedClasses) -
invokedynamic
-
new_
-
new_
-
new_
-
new_
-
newarray
-
anewarray
-
anewarray
-
arraylength
-
athrow
-
checkcast
-
checkcast
-
checkcast
-
instanceof_
-
instanceof_
-
monitorenter
-
monitorexit
-
wide
-
multianewarray
public CompactCodeAttributeComposer multianewarray(String className, Clazz referencedClass, int dimensions) -
multianewarray
-
ifnull
-
ifnonnull
-
goto_w
-
jsr_w
-
pushPrimitive
Pushes the given primitive value on the stack.Operand stack: ... -> ..., value
- Parameters:
primitive- the primitive value to be pushed - should never be null.internalType- the internal type of the primitive ('Z','B','I',...)
-
pushInt
Pushes the given primitive int on the stack in the most efficient way (as an iconst, bipush, sipush, or ldc instruction).- Parameters:
value- the int value to be pushed.
-
pushFloat
Pushes the given primitive float on the stack in the most efficient way (as an fconst or ldc instruction).- Parameters:
value- the float value to be pushed.
-
pushLong
Pushes the given primitive long on the stack in the most efficient way (as an lconst or ldc instruction).- Parameters:
value- the long value to be pushed.
-
pushDouble
Pushes the given primitive double on the stack in the most efficient way (as a dconst or ldc instruction).- Parameters:
value- the double value to be pushed.
-
pushNewArray
Pushes a new array on the stack.Operand stack: ... -> ..., array
- Parameters:
elementTypeOrClassName- the array element type (or class name in case of objects).size- the size of the array to be created.
-
load
Loads the given variable onto the stack.Operand stack: ... -> ..., value
- Parameters:
variableIndex- the index of the variable to be loaded.internalType- the type of the variable to be loaded.
-
load
Loads the given variable of primitive type onto the stack.Operand stack: ... -> ..., value
- Parameters:
variableIndex- the index of the variable to be loaded.internalType- the primitive type of the variable to be loaded.
-
store
Stores the value on top of the stack in the variable with given index.Operand stsack: ..., value -> ...
- Parameters:
variableIndex- the index of the variable where to store the value.internalType- the type of the value to be stored.
-
store
Stores the primitve value on top of the stack in the variable with given index.Operand stack: ..., value -> ...
- Parameters:
variableIndex- the index of the variable where to store the value.internalType- the primitive type of the value to be stored.
-
storeToArray
Stores an element to an array.Operand stack: ..., array, index, value -> ...
- Parameters:
elementType- the type of the value to be stored.
-
return_
Appends the proper return statement for the given internal type.- Parameters:
internalType- the return type.
-
appendPrintIntegerInstructions
Appends instructions to print out the given message and the top int on the stack. -
appendPrintIntegerHexInstructions
Appends instructions to print out the given message and the top int on the stack as a hexadecimal value. -
appendPrintLongInstructions
Appends instructions to print out the given message and the top long on the stack. -
appendPrintStringInstructions
Appends instructions to print out the given message and the top String on the stack. -
appendPrintObjectInstructions
Appends instructions to print out the given message and the top Object on the stack. -
appendPrintStackTraceInstructions
Appends instructions to print out the given message and the stack trace of the top Throwable on the stack. -
appendPrintInstructions
Appends instructions to print out the given message. -
appendPrintIntegerInstructions
Appends instructions to print out the top int on the stack. -
appendPrintIntegerHexInstructions
Appends instructions to print out the top integer on the stack as a hexadecimal value. -
appendPrintLongInstructions
Appends instructions to print out the top long on the stack. -
appendPrintStringInstructions
Appends instructions to print out the top String on the stack. -
appendPrintObjectInstructions
Appends instructions to print out the top Object on the stack. -
appendPrintStackTraceInstructions
Appends instructions to print out the stack trace of the top Throwable on the stack. -
addCodeAttribute
Adds the code that has been built as a code attribute to the given method. -
visitAnyAttribute
Description copied from interface:AttributeVisitorVisits any Attribute instance. The more specific default implementations of this interface delegate to this method.- Specified by:
visitAnyAttributein interfaceAttributeVisitor
-
visitCodeAttribute
Sets the code that has been built in the given code attribute.- Specified by:
visitCodeAttributein interfaceAttributeVisitor
-
main
Small sample application that illustrates the use of this class.
-