Class MethodInfo
- java.lang.Object
-
- javassist.bytecode.MethodInfo
-
public class MethodInfo extends Object
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
Fields Modifier and Type Field Description static boolean
doPreverify
If this value is true, Javassist maintains aStackMap
attribute generated by thepreverify
tool of J2ME (CLDC).static String
nameClinit
The name of class initializer (static initializer):<clinit>
.static String
nameInit
The name of constructors:<init>
.
-
Constructor Summary
Constructors Constructor Description MethodInfo(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
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addAttribute(AttributeInfo info)
Appends an attribute.int
getAccessFlags()
Returns access flags.AttributeInfo
getAttribute(String name)
Returns the attribute with the specified name.List<AttributeInfo>
getAttributes()
Returns all the attributes.CodeAttribute
getCodeAttribute()
Returns a Code attribute.ConstPool
getConstPool()
Returns a constant pool table used by this method.String
getDescriptor()
Returns a method descriptor.ExceptionsAttribute
getExceptionsAttribute()
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.String
getName()
Returns a method name.boolean
isConstructor()
Returns true if this is a constructor.boolean
isMethod()
Returns true if this is not a constructor or a class initializer (static initializer).boolean
isStaticInitializer()
Returns true if this is a class initializer (static initializer).void
rebuildStackMap(ClassPool pool)
Rebuilds a stack map table.void
rebuildStackMapForME(ClassPool pool)
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.AttributeInfo
removeAttribute(String name)
Removes an attribute with the specified name.void
removeCodeAttribute()
Removes a Code attribute.void
removeExceptionsAttribute()
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
setExceptionsAttribute(ExceptionsAttribute cattr)
Adds an Exception attribute.void
setName(String newName)
Sets a method name.void
setSuperclass(String superclass)
Changes a super constructor called by this constructor.String
toString()
Returns a string representation of the object.
-
-
-
Field Detail
-
doPreverify
public static boolean doPreverify
If this value is true, Javassist maintains aStackMap
attribute generated by thepreverify
tool of J2ME (CLDC). The initial value of this field isfalse
.
-
nameInit
public static final String nameInit
The name of constructors:<init>
.- See Also:
- Constant Field Values
-
nameClinit
public static final String nameClinit
The name of class initializer (static initializer):<clinit>
.- See Also:
- Constant Field Values
-
-
Constructor Detail
-
MethodInfo
public MethodInfo(ConstPool cp, String methodname, String desc)
Constructs amethod_info
structure. The initial value ofaccess_flags
is zero.- Parameters:
cp
- a constant pool tablemethodname
- method namedesc
- method descriptor- See Also:
Descriptor
-
MethodInfo
public MethodInfo(ConstPool cp, String methodname, MethodInfo src, Map<String,String> classnameMap) throws BadBytecode
Constructs 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:
Descriptor
-
-
Method Detail
-
toString
public String toString()
Returns a string representation of the object.
-
getName
public String getName()
Returns a method name.
-
setName
public void setName(String newName)
Sets a method name.
-
isMethod
public boolean isMethod()
Returns true if this is not a constructor or a class initializer (static initializer).
-
getConstPool
public ConstPool 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:
AccessFlag
-
setAccessFlags
public void setAccessFlags(int acc)
Sets access flags.- See Also:
AccessFlag
-
getDescriptor
public String getDescriptor()
Returns a method descriptor.- See Also:
Descriptor
-
setDescriptor
public void setDescriptor(String desc)
Sets a method descriptor.- See Also:
Descriptor
-
getAttributes
public List<AttributeInfo> 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:
AttributeInfo
-
getAttribute
public AttributeInfo getAttribute(String name)
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:
getAttributes()
-
removeAttribute
public AttributeInfo removeAttribute(String name)
Removes an attribute with the specified name.- Parameters:
name
- attribute name.- Returns:
- the removed attribute or null.
- Since:
- 3.21
-
addAttribute
public void addAttribute(AttributeInfo info)
Appends an attribute. If there is already an attribute with the same name, the new one substitutes for it.- See Also:
getAttributes()
-
getExceptionsAttribute
public ExceptionsAttribute getExceptionsAttribute()
Returns an Exceptions attribute.- Returns:
- an Exceptions attribute or null if it is not specified.
-
getCodeAttribute
public CodeAttribute 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
public void setExceptionsAttribute(ExceptionsAttribute cattr)
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
public void setCodeAttribute(CodeAttribute cattr)
Adds a Code attribute.The added attribute must share the same constant pool table as this
method_info
structure.
-
rebuildStackMapIf6
public void rebuildStackMapIf6(ClassPool pool, ClassFile cf) throws BadBytecode
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(ClassPool)
,rebuildStackMapForME(ClassPool)
,doPreverify
-
rebuildStackMap
public void rebuildStackMap(ClassPool pool) throws BadBytecode
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:
StackMapTable
-
rebuildStackMapForME
public void rebuildStackMapForME(ClassPool pool) throws BadBytecode
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:
StackMap
-
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
public void setSuperclass(String superclass) throws BadBytecode
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
-
-