Class MethodGen
- All Implemented Interfaces:
Cloneable, NamedAndTyped
- See Also:
-
Field Summary
Fields inherited from class FieldGenOrMethodGen
cp, name, typeModifier and TypeFieldDescriptionprotected ConstantPoolGenDeprecated.(since 6.0) will be made private; do not access directly, use getter/setterprotected StringDeprecated.(since 6.0) will be made private; do not access directly, use getter/setterprotected TypeDeprecated.(since 6.0) will be made private; do not access directly, use getter/setterFields inherited from class AccessFlags
access_flagsModifier and TypeFieldDescriptionprotected intDeprecated.(since 6.0) will be made private; do not access directly, use getter/setter. -
Constructor Summary
ConstructorsConstructorDescriptionMethodGen(int accessFlags, Type returnType, Type[] argTypes, String[] argNames, String methodName, String className, InstructionList il, ConstantPoolGen cp) Declare method.MethodGen(Method method, String className, ConstantPoolGen cp) Instantiate from existing method. -
Method Summary
Modifier and TypeMethodDescriptionvoidAdds annotations as an attribute.voidAdd an attribute to the code.voidaddException(String className) Add an exception possibly thrown by this method.addExceptionHandler(InstructionHandle startPc, InstructionHandle endPc, InstructionHandle handlerPc, ObjectType catchType) Add an exception handler, that is, specify region where a handler is active and an instruction where the actual handling is done.addLineNumber(InstructionHandle ih, int srcLine) Give an instruction a line number corresponding to the source code line.addLocalVariable(String name, Type type, int slot, InstructionHandle start, InstructionHandle end) Adds a local variable to this method.addLocalVariable(String name, Type type, int slot, InstructionHandle start, InstructionHandle end, int origIndex) Adds a local variable to this method.addLocalVariable(String name, Type type, InstructionHandle start, InstructionHandle end) Adds a local variable to this method and assigns an index automatically.voidAdd observer for this object.voidaddParameterAnnotation(int parameterIndex, AnnotationEntryGen annotation) Adds a parameter annotation.voidAdds parameter annotations as an attribute.copy(String className, ConstantPoolGen cp) Creates a deep copy of this method.booleanReturn value as defined by given BCELComparator strategy.getAnnotationsOnParameter(int i) Gets a list of AnnotationGen objects representing parameter annotations.getArgumentName(int i) Gets the argument name at the specified index.String[]Gets all argument names.getArgumentType(int i) Gets the argument type at the specified index.Type[]Gets all argument types.Gets the class that contains this method.Gets all attributes of this method.static BCELComparator<FieldGenOrMethodGen> Gets the comparison strategy object.Gets an array of declared exception handlers.String[]Gets an array of thrown exceptions.Gets the instruction list.Gets an array of line numbers.Gets the 'LineNumberTable' attribute of all the local variables of this method.Gets an array of declared local variables sorted by index.Gets the 'LocalVariableTable' attribute of all the local variables of this method.Gets the 'LocalVariableTypeTable' attribute of this method.intGets the maximum number of local variables.intGets the maximum stack size.static intgetMaxStack(ConstantPoolGen cp, InstructionList il, CodeExceptionGen[] et) Gets the maximum stack depth of an instruction list by performing control flow analysis.Gets method object.Gets the return type.Gets the signature of method or field.inthashCode()Return value as defined by given BCELComparator strategy.booleanGets whether generated Code attributes use compact four-byte headers.voidRemove a code attribute.voidRemove all code attributes.voidRemove an exception.voidRemove an exception handler.voidRemove all line numbers.voidRemove all exceptions.voidRemove a line number.voidRemove all line numbers.voidRemove a local variable, its slot will not be reused, if you do not use addLocalVariable with an explicit index argument.voidRemove all local variables.voidRemove the LocalVariableTypeTablevoidRemove all NOPs from the instruction list (if possible) and update every object referring to them, that is, branch instructions, local variables and exception handlers.voidRemove observer for this object.voidremoveRuntimeAttributes(Attribute[] attributes) Would prefer to make this private, but need a way to test if client is using BCEL version 6.5.0 or later that contains fix for BCEL-329.voidsetArgumentName(int i, String name) Sets the argument name at the specified index.voidsetArgumentNames(String[] argNames) Sets all argument names.voidsetArgumentType(int i, Type type) Sets the argument type at the specified index.voidsetArgumentTypes(Type[] argTypes) Sets all argument types.voidsetClassName(String className) Sets the class name.voidsetCompactCodeHeader(boolean compactCodeHeader) Sets the header layout of generated Code attributes.static voidsetComparator(BCELComparator<FieldGenOrMethodGen> comparator) Sets the comparison strategy object.voidSets the instruction list.voidSets the maximum number of local variables by examining the method signature, the instructions and the local variable table entries added viaaddLocalVariable(String, Type, InstructionHandle, InstructionHandle).voidsetMaxLocals(int m) Sets maximum number of local variables.voidSets the maximum stack size by performing control flow analysis.voidsetMaxStack(int m) Sets maximum stack size for this method.voidsetReturnType(Type returnType) Sets the return type.voidstripAttributes(boolean flag) Do not/Do produce attributes code attributesLineNumberTable and LocalVariableTable, like javac -O.final StringtoString()Return string representation close to declaration format, 'public static void main(String[]) throws IOException', for example.voidupdate()Call notify() method on all observers.Methods inherited from class FieldGenOrMethodGen
addAll, addAnnotationEntry, addAttribute, clone, getAnnotationEntries, getAttributes, getConstantPool, getName, getType, removeAnnotationEntries, removeAnnotationEntry, removeAttribute, removeAttributes, setConstantPool, setName, setTypeModifier and TypeMethodDescriptionprotected voidAdds all attributes from an array.voidAdds an annotation entry.voidAdd an attribute to this method.clone()Gets all annotation entries.Gets all attributes of this method or field.Gets the constant pool.getName()Gets the name of the method or field.getType()Gets the type.voidRemoves all annotation entries.voidRemoves an annotation entry.voidRemoves an attribute.voidRemoves all attributes.voidSets the constant pool.voidSets the name.voidSets the type.Methods inherited from class AccessFlags
getAccessFlags, getModifiers, isAbstract, isAbstract, isAnnotation, isAnnotation, isEnum, isEnum, isFinal, isFinal, isInterface, isInterface, isNative, isNative, isPrivate, isPrivate, isProtected, isProtected, isPublic, isPublic, isStatic, isStatic, isStrictfp, isStrictfp, isSynchronized, isSynchronized, isSynthetic, isSynthetic, isTransient, isTransient, isVarArgs, isVarArgs, isVolatile, isVolatile, setAccessFlags, setModifiersModifier and TypeMethodDescriptionfinal intGets access flags.final intGets access flags.final booleanTests whether the abstract bit is on.final voidisAbstract(boolean flag) Sets the abstract bit.final booleanTests whether the annotation bit is on.final voidisAnnotation(boolean flag) Sets the annotation bit.final booleanisEnum()Tests whether the enum bit is on.final voidisEnum(boolean flag) Sets the enum bit.final booleanisFinal()Tests whether the final bit is on.final voidisFinal(boolean flag) Sets the final bit.final booleanTests whether the interface bit is on.final voidisInterface(boolean flag) Sets the interface bit.final booleanisNative()Tests whether the native bit is on.final voidisNative(boolean flag) Sets the native bit.final booleanTests whether the private bit is on.final voidisPrivate(boolean flag) Sets the private bit.final booleanTests whether the protected bit is on.final voidisProtected(boolean flag) Sets the protected bit.final booleanisPublic()Tests whether the public bit is on.final voidisPublic(boolean flag) Sets the public bit.final booleanisStatic()Tests whether the static bit is on.final voidisStatic(boolean flag) Sets the static bit.final booleanTests whether the strict bit is on.final voidisStrictfp(boolean flag) Sets the strict bit.final booleanTests whether the synchronized bit is on.final voidisSynchronized(boolean flag) Sets the synchronized bit.final booleanTests whether the synthetic bit is on.final voidisSynthetic(boolean flag) Sets the synthetic bit.final booleanTests whether the transient bit is on.final voidisTransient(boolean flag) Sets the transient bit.final booleanTests whether the varargs bit is on.final voidisVarArgs(boolean flag) Sets the varargs bit.final booleanTests whether the volatile bit is on.final voidisVolatile(boolean flag) Sets the volatile bit.final voidsetAccessFlags(int accessFlags) Sets access flags also known as modifiers.final voidsetModifiers(int accessFlags) Sets access flags aka "modifiers".
-
Constructor Details
-
MethodGen
public MethodGen(int accessFlags, Type returnType, Type[] argTypes, String[] argNames, String methodName, String className, InstructionList il, ConstantPoolGen cp) Declare method. If the method is non-static the constructor automatically declares a local variable '$this' in slot 0. The actual code is contained in the 'il' parameter, which may further manipulated by the user. But they must take care not to remove any instruction (handles) that are still referenced from this object. For example one may not add a local variable and later remove the instructions it refers to without causing havoc. It is safe however if you remove that local variable, too.- Parameters:
accessFlags- access qualifiers.returnType- method type.argTypes- argument types.argNames- argument names (if this is null, default names will be provided for them).methodName- name of method.className- class name containing this method (may be null, if you don't care).il- instruction list associated with this method, may be null only for abstract or native methods.cp- constant pool.
-
MethodGen
Instantiate from existing method.- Parameters:
method- method.className- class name containing this method.cp- constant pool.
-
-
Method Details
-
getComparator
Gets the comparison strategy object.- Returns:
- Comparison strategy object.
-
getMaxStack
Gets the maximum stack depth of an instruction list by performing control flow analysis.- Parameters:
cp- The constant pool generator.il- The instruction list.et- The exception handlers.- Returns:
- maximum stack depth used by method.
-
setComparator
Sets the comparison strategy object.- Parameters:
comparator- Comparison strategy object.
-
addAnnotationsAsAttribute
Adds annotations as an attribute.- Parameters:
cp- The constant pool generator.- Since:
- 6.0
-
addCodeAttribute
Add an attribute to the code. Currently, the JVM knows about the LineNumberTable, LocalVariableTable and StackMap attributes, where the former two will be generated automatically and the latter is used for the MIDP only. Other attributes will be ignored by the JVM but do no harm.- Parameters:
a- attribute to be added.
-
addException
Add an exception possibly thrown by this method.- Parameters:
className- (fully qualified) name of exception.
-
addExceptionHandler
public CodeExceptionGen addExceptionHandler(InstructionHandle startPc, InstructionHandle endPc, InstructionHandle handlerPc, ObjectType catchType) Add an exception handler, that is, specify region where a handler is active and an instruction where the actual handling is done.- Parameters:
startPc- Start of region (inclusive).endPc- End of region (inclusive).handlerPc- Where handling is done.catchType- class type of handled exception or null if any exception is handled.- Returns:
- new exception handler object.
-
addLineNumber
Give an instruction a line number corresponding to the source code line.- Parameters:
ih- instruction to tag.srcLine- The source line number.- Returns:
- new line number object.
- See Also:
-
addLocalVariable
public LocalVariableGen addLocalVariable(String name, Type type, InstructionHandle start, InstructionHandle end) Adds a local variable to this method and assigns an index automatically.- Parameters:
name- variable name.type- variable type.start- from where the variable is valid, if this is null, it is valid from the start.end- until where the variable is valid, if this is null, it is valid to the end.- Returns:
- new local variable object.
- See Also:
-
addLocalVariable
public LocalVariableGen addLocalVariable(String name, Type type, int slot, InstructionHandle start, InstructionHandle end) Adds a local variable to this method.- Parameters:
name- variable name.type- variable type.slot- The index of the local variable, if type is long or double, the next available index is slot+2.start- from where the variable is valid.end- until where the variable is valid.- Returns:
- new local variable object.
- See Also:
-
addLocalVariable
public LocalVariableGen addLocalVariable(String name, Type type, int slot, InstructionHandle start, InstructionHandle end, int origIndex) Adds a local variable to this method.- Parameters:
name- variable name.type- variable type.slot- The index of the local variable, if type is long or double, the next available index is slot+2.start- from where the variable is valid.end- until where the variable is valid.origIndex- The index of the local variable prior to any modifications.- Returns:
- new local variable object.
- See Also:
-
addObserver
Add observer for this object.- Parameters:
o- The observer to add.
-
addParameterAnnotation
Adds a parameter annotation.- Parameters:
parameterIndex- The parameter index.annotation- The annotation.
-
addParameterAnnotationsAsAttribute
Adds parameter annotations as an attribute.- Parameters:
cp- The constant pool generator.- Since:
- 6.0
-
copy
Creates a deep copy of this method.- Parameters:
className- The class name.cp- The constant pool generator.- Returns:
- deep copy of this method.
-
equals
-
getAnnotationsOnParameter
Gets a list of AnnotationGen objects representing parameter annotations.- Parameters:
i- The parameter index.- Returns:
- list of AnnotationGen objects.
- Since:
- 6.0
-
getArgumentName
Gets the argument name at the specified index.- Parameters:
i- The argument index.- Returns:
- The argument name.
-
getArgumentNames
-
getArgumentType
Gets the argument type at the specified index.- Parameters:
i- The argument index.- Returns:
- The argument type.
-
getArgumentTypes
-
getClassName
Gets the class that contains this method.- Returns:
- class that contains this method.
-
getCodeAttributes
Gets all attributes of this method.- Returns:
- all attributes of this method.
-
getExceptionHandlers
Gets an array of declared exception handlers.- Returns:
- array of declared exception handlers.
-
getExceptions
Gets an array of thrown exceptions.- Returns:
- array of thrown exceptions.
-
getInstructionList
Gets the instruction list.- Returns:
- The instruction list.
-
getLineNumbers
Gets an array of line numbers.- Returns:
- array of line numbers.
-
getLineNumberTable
Gets the 'LineNumberTable' attribute of all the local variables of this method.- Parameters:
cp- The constant pool generator.- Returns:
- 'LineNumberTable' attribute of all the local variables of this method.
-
getLocalVariables
Gets an array of declared local variables sorted by index. If the range of the variable has not been set yet, it will be set to be valid from the start to the end of the instruction list.- Returns:
- array of declared local variables sorted by index.
-
getLocalVariableTable
Gets the 'LocalVariableTable' attribute of all the local variables of this method.- Parameters:
cp- The constant pool generator.- Returns:
- 'LocalVariableTable' attribute of all the local variables of this method.
-
getLocalVariableTypeTable
Gets the 'LocalVariableTypeTable' attribute of this method.- Returns:
- 'LocalVariableTypeTable' attribute of this method.
-
getMaxLocals
Gets the maximum number of local variables.- Returns:
- The maximum number of local variables.
-
getMaxStack
-
getMethod
Gets method object. Never forget to call setMaxStack() or setMaxStack(max), respectively, before calling this method (the same applies for max locals).When initialized from an existing method, the Code header layout is preserved. Use
setCompactCodeHeader(boolean)to change it.- Returns:
- method object.
- Throws:
ClassFormatException- Thrown if Code values do not fit the selected header layout.
-
getReturnType
-
getSignature
Description copied from class:FieldGenOrMethodGenGets the signature of method or field.- Specified by:
getSignaturein classFieldGenOrMethodGen- Returns:
- signature of method/field.
-
hashCode
-
isCompactCodeHeader
Gets whether generated Code attributes use compact four-byte headers.- Returns:
- Whether generated Code attributes use compact headers.
- Since:
- 6.14.0
- See Also:
-
removeCodeAttribute
Remove a code attribute.- Parameters:
a- The attribute to remove.
-
removeCodeAttributes
Remove all code attributes. -
removeException
-
removeExceptionHandler
Remove an exception handler.- Parameters:
c- The exception handler to remove.
-
removeExceptionHandlers
Remove all line numbers. -
removeExceptions
Remove all exceptions. -
removeLineNumber
Remove a line number.- Parameters:
l- The line number to remove.
-
removeLineNumbers
Remove all line numbers. -
removeLocalVariable
Remove a local variable, its slot will not be reused, if you do not use addLocalVariable with an explicit index argument.- Parameters:
l- The local variable to remove.
-
removeLocalVariables
Remove all local variables. -
removeLocalVariableTypeTable
Remove the LocalVariableTypeTable -
removeNOPs
Remove all NOPs from the instruction list (if possible) and update every object referring to them, that is, branch instructions, local variables and exception handlers. -
removeObserver
Remove observer for this object.- Parameters:
o- The observer to remove.
-
removeRuntimeAttributes
Would prefer to make this private, but need a way to test if client is using BCEL version 6.5.0 or later that contains fix for BCEL-329.- Parameters:
attributes- The attributes to remove.- Since:
- 6.5.0
-
setArgumentName
Sets the argument name at the specified index.- Parameters:
i- The argument index.name- The argument name.
-
setArgumentNames
Sets all argument names.- Parameters:
argNames- The argument names.
-
setArgumentType
Sets the argument type at the specified index.- Parameters:
i- The argument index.type- The argument type.
-
setArgumentTypes
Sets all argument types.- Parameters:
argTypes- The argument types.
-
setClassName
Sets the class name.- Parameters:
className- The class name.
-
setCompactCodeHeader
Sets the header layout of generated Code attributes.Compact headers encode
max_stack,max_locals, andcode_lengthas unsigned one-, one-, and two-byte fields, respectively, totaling 4 bytes instead of the standard 8. This changes only these field widths, not the bytecode or the remaining attribute layout. Compact headers are intended for historical class file versions 45.0 through 45.2.Newly created methods use standard headers by default. Methods initialized from an existing method preserve its header layout.
getMethod()checks that compact headers havemax_stackandmax_localsfrom 0 through 255 and a code length of at most 65535 bytes.When dumping the enclosing class,
JavaClass.dump(java.io.DataOutputStream)may select a different layout for consistency with the class version and its other methods. This does not change the generated Code attribute's header setting.- Parameters:
compactCodeHeader- Whether to generate compact four-byte Code headers (true) or standard eight-byte headers (false).- Since:
- 6.14.0
- See Also:
-
setInstructionList
Sets the instruction list.- Parameters:
il- The instruction list.
-
setMaxLocals
Sets the maximum number of local variables by examining the method signature, the instructions and the local variable table entries added viaaddLocalVariable(String, Type, InstructionHandle, InstructionHandle). -
setMaxLocals
Sets maximum number of local variables.- Parameters:
m- The maximum number of local variables.
-
setMaxStack
Sets the maximum stack size by performing control flow analysis. -
setMaxStack
Sets maximum stack size for this method.- Parameters:
m- The maximum stack size.
-
setReturnType
Sets the return type.- Parameters:
returnType- The return type.
-
stripAttributes
Do not/Do produce attributes code attributesLineNumberTable and LocalVariableTable, like javac -O.- Parameters:
flag- whether to strip attributes.
-
toString
-
update
Call notify() method on all observers. This method is not called automatically whenever the state has changed, but has to be called by the user after they have finished editing the object.
-