Top Description Fields Constructors Methods
javassist.bytecode

public Class MethodInfo

extends Object
Class Inheritance
Imports
java.io.DataInputStream, .DataOutputStream, .IOException, java.util.ArrayList, .List, .Map, javassist.ClassPool, javassist.bytecode.stackmap.MapMaker

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);
See Also
getCodeAttribute(), CodeAttribute, Bytecode, javassist.CtMethod#getMethodInfo(), javassist.CtConstructor#getMethodInfo()

Field Summary

Modifier and TypeField and Description
pack-priv int
pack-priv List<AttributeInfo>
pack-priv String
pack-priv ConstPool
pack-priv int
public static boolean
doPreverify

If this value is true, Javassist maintains a StackMap attribute generated by the preverify tool of J2ME (CLDC).

pack-priv int
public static final String
nameClinit

The name of class initializer (static initializer): <clinit>.

public static final String
nameInit

The name of constructors: <init>.

Constructor Summary

AccessConstructor and Description
private
public
MethodInfo(ConstPool
a constant pool table
cp
,
String
method name
methodname
,
String
method descriptor
desc
)

Constructs a method_info structure.

pack-priv
public
MethodInfo(ConstPool
a constant pool table
cp
,
String
a method name
methodname
,
MethodInfo
a source method_info
src
,
Map<String, String>
specifies pairs of replaced and substituted name.
classnameMap
)

Constructs a copy of method_info structure.

Method Summary

Modifier and TypeMethod and Description
public void
addAttribute(AttributeInfo info)

Appends an attribute.

pack-priv void
compact(ConstPool
the destination
cp
)

Copies all constant pool items to a given new constant pool and replaces the original items with the new ones.

public int
getAccessFlags()

Returns access flags.

public AttributeInfo

Returns:

an AttributeInfo object or null.
getAttribute
(String
attribute name
name
)

Returns the attribute with the specified name.

public List<AttributeInfo>

Returns:

a list of AttributeInfo objects.
getAttributes
()

Returns all the attributes.

public CodeAttribute

Returns:

a Code attribute or null if it is not specified.
getCodeAttribute
()

Returns a Code attribute.

public ConstPool
getConstPool()

Returns a constant pool table used by this method.

public String
getDescriptor()

Returns a method descriptor.

public ExceptionsAttribute

Returns:

an Exceptions attribute or null if it is not specified.
getExceptionsAttribute
()

Returns an Exceptions attribute.

public int

Returns:

-1 if this information is not available.
getLineNumber
(int
the position of the bytecode (>= 0). an index into the code array.
pos
)

Returns the line number of the source line corresponding to the specified bytecode contained in this method.

public String
getName()

Returns a method name.

public boolean
isConstructor()

Returns true if this is a constructor.

public boolean
isMethod()

Returns true if this is not a constructor or a class initializer (static initializer).

public boolean
isStaticInitializer()

Returns true if this is a class initializer (static initializer).

pack-priv void
private void
read(MethodInfo src, String methodname, Map<String, String> classnames)

private void
public void
rebuildStackMap(ClassPool
used for making type hierarchy.
pool
)

Rebuilds a stack map table.

public void
rebuildStackMapForME(ClassPool
used for making type hierarchy.
pool
)

Rebuilds a stack map table for J2ME (CLDC).

public void
rebuildStackMapIf6(ClassPool
used for making type hierarchy.
pool
,
ClassFile
rebuild if this class file is for Java 6 or later.
cf
)

Rebuilds a stack map table if the class file is for Java 6 or later.

public AttributeInfo

Returns:

the removed attribute or null.
removeAttribute
(String
attribute name.
name
)

Removes an attribute with the specified name.

public void
removeCodeAttribute()

Removes a Code attribute.

public void
removeExceptionsAttribute()

Removes an Exception attribute.

public void
setAccessFlags(int acc)

Sets access flags.

public void
setCodeAttribute(CodeAttribute cattr)

Adds a Code attribute.

public void
setDescriptor(String desc)

Sets a method descriptor.

public void
setExceptionsAttribute(ExceptionsAttribute cattr)

Adds an Exception attribute.

public void
setName(String newName)

Sets a method name.

public void
setSuperclass(String
the new super class
superclass
)

Changes a super constructor called by this constructor.

public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.

pack-priv void
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

accessFlagsback to summary
pack-priv int accessFlags
attributeback to summary
pack-priv List<AttributeInfo> attribute
cachedNameback to summary
pack-priv String cachedName
constPoolback to summary
pack-priv ConstPool constPool
descriptorback to summary
pack-priv int descriptor
doPreverifyback to summary
public static boolean doPreverify

If this value is true, Javassist maintains a StackMap attribute generated by the preverify tool of J2ME (CLDC). The initial value of this field is false.

nameback to summary
pack-priv int name
nameClinitback to summary
public static final String nameClinit

The name of class initializer (static initializer): <clinit>.

nameInitback to summary
public static final String nameInit

The name of constructors: <init>.

Constructor Detail

MethodInfoback to summary
private MethodInfo(ConstPool cp)
MethodInfoback to summary
public MethodInfo(ConstPool cp, String methodname, String desc)

Constructs a method_info structure. The initial value of access_flags is zero.

Parameters
cp:ConstPool

a constant pool table

methodname:String

method name

desc:String

method descriptor

See Also
Descriptor
MethodInfoback to summary
pack-priv MethodInfo(ConstPool cp, DataInputStream in) throws IOException
MethodInfoback to summary
public MethodInfo(ConstPool cp, String methodname, MethodInfo src, Map<String, String> classnameMap) throws BadBytecode

Constructs a copy of method_info structure. Class names appearing in the source method_info are renamed according to classnameMap.

Note

only Code and Exceptions attributes are copied from the source. The other attributes are ignored.

Parameters
cp:ConstPool

a constant pool table

methodname:String

a method name

src:MethodInfo

a source method_info

classnameMap:Map<String, String>

specifies pairs of replaced and substituted name.

See Also
Descriptor

Method Detail

addAttributeback to summary
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()
compactback to summary
pack-priv void compact(ConstPool cp)

Copies all constant pool items to a given new constant pool and replaces the original items with the new ones. This is used for garbage collecting the items of removed fields and methods.

Parameters
cp:ConstPool

the destination

getAccessFlagsback to summary
public int getAccessFlags()

Returns access flags.

See Also
AccessFlag
getAttributeback to summary
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 or AnnotationsAttribute#invisibleTag.

Parameters
name:String

attribute name

Returns:AttributeInfo

an AttributeInfo object or null.

See Also
getAttributes()
getAttributesback to summary
public List<AttributeInfo> getAttributes()

Returns all the attributes. The returned List 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:List<AttributeInfo>

a list of AttributeInfo objects.

See Also
AttributeInfo
getCodeAttributeback to summary
public CodeAttribute getCodeAttribute()

Returns a Code attribute.

Returns:CodeAttribute

a Code attribute or null if it is not specified.

getConstPoolback to summary
public ConstPool getConstPool()

Returns a constant pool table used by this method.

getDescriptorback to summary
public String getDescriptor()

Returns a method descriptor.

See Also
Descriptor
getExceptionsAttributeback to summary
public ExceptionsAttribute getExceptionsAttribute()

Returns an Exceptions attribute.

Returns:ExceptionsAttribute

an Exceptions attribute or null if it is not specified.

getLineNumberback to summary
public int getLineNumber(int pos)

Returns the line number of the source line corresponding to the specified bytecode contained in this method.

Parameters
pos:int

the position of the bytecode (>= 0). an index into the code array.

Returns:int

-1 if this information is not available.

getNameback to summary
public String getName()

Returns a method name.

isConstructorback to summary
public boolean isConstructor()

Returns true if this is a constructor.

isMethodback to summary
public boolean isMethod()

Returns true if this is not a constructor or a class initializer (static initializer).

isStaticInitializerback to summary
public boolean isStaticInitializer()

Returns true if this is a class initializer (static initializer).

pruneback to summary
pack-priv void prune(ConstPool cp)
readback to summary
private void read(MethodInfo src, String methodname, Map<String, String> classnames)
readback to summary
private void read(DataInputStream in) throws IOException
rebuildStackMapback to summary
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 this MethodInfo does not include a code attribute, nothing happens.

Parameters
pool:ClassPool

used for making type hierarchy.

Since
3.6
See Also
StackMapTable
rebuildStackMapForMEback to summary
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 this MethodInfo does not include a code attribute, nothing happens.

Parameters
pool:ClassPool

used for making type hierarchy.

Since
3.12
See Also
StackMap
rebuildStackMapIf6back to summary
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. If doPreverify is true, this method also rebuilds a stack map for J2ME (CLDC).

Parameters
pool:ClassPool

used for making type hierarchy.

cf:ClassFile

rebuild if this class file is for Java 6 or later.

Since
3.6
See Also
rebuildStackMap(ClassPool), rebuildStackMapForME(ClassPool), doPreverify
removeAttributeback to summary
public AttributeInfo removeAttribute(String name)

Removes an attribute with the specified name.

Parameters
name:String

attribute name.

Returns:AttributeInfo

the removed attribute or null.

Since
3.21
removeCodeAttributeback to summary
public void removeCodeAttribute()

Removes a Code attribute.

removeExceptionsAttributeback to summary
public void removeExceptionsAttribute()

Removes an Exception attribute.

setAccessFlagsback to summary
public void setAccessFlags(int acc)

Sets access flags.

See Also
AccessFlag
setCodeAttributeback to summary
public void setCodeAttribute(CodeAttribute cattr)

Adds a Code attribute.

The added attribute must share the same constant pool table as this method_info structure.

setDescriptorback to summary
public void setDescriptor(String desc)

Sets a method descriptor.

See Also
Descriptor
setExceptionsAttributeback to summary
public void setExceptionsAttribute(ExceptionsAttribute cattr)

Adds an Exception attribute.

The added attribute must share the same constant pool table as this method_info structure.

setNameback to summary
public void setName(String newName)

Sets a method name.

setSuperclassback to summary
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:String

the new super class

toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.

Returns:String

Doc from java.lang.Object.toString.

a string representation of the object

Annotations
@Override
writeback to summary
pack-priv void write(DataOutputStream out) throws IOException