Class MethodInfo
method_info
structure.
The bytecode sequence of the method is represented
by a CodeAttribute
object.
The following code adds the default constructor to a class:
of int
type:
ClassFile cf = ... Bytecode code = new Bytecode(cf.getConstPool()); code.addAload(0); code.addInvokespecial("java/lang/Object", MethodInfo.nameInit, "()V"); code.addReturn(null); code.setMaxLocals(1); MethodInfo minfo = new MethodInfo(cf.getConstPool(), MethodInfo.nameInit, "()V"); minfo.setCodeAttribute(code.toCodeAttribute()); cf.addMethod(minfo);
-
Field Summary
-
Constructor Summary
ConstructorDescriptionMethodInfo
(ConstPool cp, String methodname, String desc) Constructs amethod_info
structure.MethodInfo
(ConstPool cp, String methodname, MethodInfo src, Map<String, String> classnameMap) Constructs a copy ofmethod_info
structure. -
Method Summary
Modifier and TypeMethodDescriptionvoid
addAttribute
(AttributeInfo info) Appends an attribute.int
Returns access flags.getAttribute
(String name) Returns the attribute with the specified name.Returns all the attributes.Returns a Code attribute.Returns a constant pool table used by this method.Returns a method descriptor.Returns an Exceptions attribute.int
getLineNumber
(int pos) Returns the line number of the source line corresponding to the specified bytecode contained in this method.getName()
Returns a method name.boolean
Returns true if this is a constructor.boolean
isMethod()
Returns true if this is not a constructor or a class initializer (static initializer).boolean
Returns true if this is a class initializer (static initializer).void
rebuildStackMap
(ClassPool pool) Rebuilds a stack map table.void
Rebuilds a stack map table for J2ME (CLDC).void
rebuildStackMapIf6
(ClassPool pool, ClassFile cf) Rebuilds a stack map table if the class file is for Java 6 or later.removeAttribute
(String name) Removes an attribute with the specified name.void
Removes a Code attribute.void
Removes an Exception attribute.void
setAccessFlags
(int acc) Sets access flags.void
setCodeAttribute
(CodeAttribute cattr) Adds a Code attribute.void
setDescriptor
(String desc) Sets a method descriptor.void
Adds an Exception attribute.void
Sets a method name.void
setSuperclass
(String superclass) Changes a super constructor called by this constructor.toString()
Returns a string representation of the object.
-
Field Details
-
doPreverify
public static boolean doPreverifyIf this value is true, Javassist maintains aStackMap
attribute generated by thepreverify
tool of J2ME (CLDC). The initial value of this field isfalse
. -
nameInit
The name of constructors:<init>
.- See Also:
-
nameClinit
The name of class initializer (static initializer):<clinit>
.- See Also:
-
-
Constructor Details
-
MethodInfo
Constructs amethod_info
structure. The initial value ofaccess_flags
is zero.- Parameters:
cp
- a constant pool tablemethodname
- method namedesc
- method descriptor- See Also:
-
MethodInfo
public MethodInfo(ConstPool cp, String methodname, MethodInfo src, Map<String, String> classnameMap) throws BadBytecodeConstructs a copy ofmethod_info
structure. Class names appearing in the sourcemethod_info
are renamed according toclassnameMap
.Note: only
Code
andExceptions
attributes are copied from the source. The other attributes are ignored.- Parameters:
cp
- a constant pool tablemethodname
- a method namesrc
- a sourcemethod_info
classnameMap
- specifies pairs of replaced and substituted name.- Throws:
BadBytecode
- See Also:
-
-
Method Details
-
toString
Returns a string representation of the object. -
getName
Returns a method name. -
setName
Sets a method name. -
isMethod
public boolean isMethod()Returns true if this is not a constructor or a class initializer (static initializer). -
getConstPool
Returns a constant pool table used by this method. -
isConstructor
public boolean isConstructor()Returns true if this is a constructor. -
isStaticInitializer
public boolean isStaticInitializer()Returns true if this is a class initializer (static initializer). -
getAccessFlags
public int getAccessFlags()Returns access flags.- See Also:
-
setAccessFlags
public void setAccessFlags(int acc) Sets access flags.- See Also:
-
getDescriptor
Returns a method descriptor.- See Also:
-
setDescriptor
Sets a method descriptor.- See Also:
-
getAttributes
Returns all the attributes. The returnedList
object is shared with this object. If you add a new attribute to the list, the attribute is also added to the method represented by this object. If you remove an attribute from the list, it is also removed from the method.- Returns:
- a list of
AttributeInfo
objects. - See Also:
-
getAttribute
Returns the attribute with the specified name. If it is not found, this method returns null.An attribute name can be obtained by, for example,
AnnotationsAttribute.visibleTag
orAnnotationsAttribute.invisibleTag
.- Parameters:
name
- attribute name- Returns:
- an
AttributeInfo
object or null. - See Also:
-
removeAttribute
Removes an attribute with the specified name.- Parameters:
name
- attribute name.- Returns:
- the removed attribute or null.
- Since:
- 3.21
-
addAttribute
Appends an attribute. If there is already an attribute with the same name, the new one substitutes for it.- See Also:
-
getExceptionsAttribute
Returns an Exceptions attribute.- Returns:
- an Exceptions attribute or null if it is not specified.
-
getCodeAttribute
Returns a Code attribute.- Returns:
- a Code attribute or null if it is not specified.
-
removeExceptionsAttribute
public void removeExceptionsAttribute()Removes an Exception attribute. -
setExceptionsAttribute
Adds an Exception attribute.The added attribute must share the same constant pool table as this
method_info
structure. -
removeCodeAttribute
public void removeCodeAttribute()Removes a Code attribute. -
setCodeAttribute
Adds a Code attribute.The added attribute must share the same constant pool table as this
method_info
structure. -
rebuildStackMapIf6
Rebuilds a stack map table if the class file is for Java 6 or later. Java 5 or older Java VMs do not recognize a stack map table. IfdoPreverify
is true, this method also rebuilds a stack map for J2ME (CLDC).- Parameters:
pool
- used for making type hierarchy.cf
- rebuild if this class file is for Java 6 or later.- Throws:
BadBytecode
- Since:
- 3.6
- See Also:
-
rebuildStackMap
Rebuilds a stack map table. If no stack map table is included, a new one is created. If thisMethodInfo
does not include a code attribute, nothing happens.- Parameters:
pool
- used for making type hierarchy.- Throws:
BadBytecode
- Since:
- 3.6
- See Also:
-
rebuildStackMapForME
Rebuilds a stack map table for J2ME (CLDC). If no stack map table is included, a new one is created. If thisMethodInfo
does not include a code attribute, nothing happens.- Parameters:
pool
- used for making type hierarchy.- Throws:
BadBytecode
- Since:
- 3.12
- See Also:
-
getLineNumber
public int getLineNumber(int pos) Returns the line number of the source line corresponding to the specified bytecode contained in this method.- Parameters:
pos
- the position of the bytecode (>= 0). an index into the code array.- Returns:
- -1 if this information is not available.
-
setSuperclass
Changes a super constructor called by this constructor.This method modifies a call to
super()
, which should be at the head of a constructor body, so that a constructor in a different super class is called. This method does not change actual parameters. Hence the new super class must have a constructor with the same signature as the original one.This method should be called when the super class of the class declaring this method is changed.
This method does not perform anything unless this
MethodInfo
represents a constructor.- Parameters:
superclass
- the new super class- Throws:
BadBytecode
-