public class MethodInfo
extends java.lang.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);
Modifier and Type | Field and Description |
---|---|
static boolean |
doPreverify
If this value is true, Javassist maintains a
StackMap attribute
generated by the preverify tool of J2ME (CLDC). |
static java.lang.String |
nameClinit
The name of class initializer (static initializer):
<clinit> . |
static java.lang.String |
nameInit
The name of constructors:
<init> . |
Constructor and Description |
---|
MethodInfo(ConstPool cp,
java.lang.String methodname,
MethodInfo src,
java.util.Map classnameMap)
Constructs a copy of
method_info structure. |
MethodInfo(ConstPool cp,
java.lang.String methodname,
java.lang.String desc)
Constructs a
method_info structure. |
Modifier and Type | Method and Description |
---|---|
void |
addAttribute(AttributeInfo info)
Appends an attribute.
|
int |
getAccessFlags()
Returns access flags.
|
AttributeInfo |
getAttribute(java.lang.String name)
Returns the attribute with the specified name.
|
java.util.List |
getAttributes()
Returns all the attributes.
|
CodeAttribute |
getCodeAttribute()
Returns a Code attribute.
|
ConstPool |
getConstPool()
Returns a constant pool table used by this method.
|
java.lang.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.
|
java.lang.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.
|
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(java.lang.String desc)
Sets a method descriptor.
|
void |
setExceptionsAttribute(ExceptionsAttribute cattr)
Adds an Exception attribute.
|
void |
setName(java.lang.String newName)
Sets a method name.
|
void |
setSuperclass(java.lang.String superclass)
Changes a super constructor called by this constructor.
|
java.lang.String |
toString()
Returns a string representation of the object.
|
public static boolean doPreverify
StackMap
attribute
generated by the preverify
tool of J2ME (CLDC). The initial
value of this field is false
.public static final java.lang.String nameInit
<init>
.public static final java.lang.String nameClinit
<clinit>
.public MethodInfo(ConstPool cp, java.lang.String methodname, java.lang.String desc)
method_info
structure. The initial value of
access_flags
is zero.cp
- a constant pool tablemethodname
- method namedesc
- method descriptorDescriptor
public MethodInfo(ConstPool cp, java.lang.String methodname, MethodInfo src, java.util.Map classnameMap) throws BadBytecode
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.
cp
- a constant pool tablemethodname
- a method namesrc
- a source method_info
classnameMap
- specifies pairs of replaced and substituted name.BadBytecode
Descriptor
public java.lang.String toString()
toString
in class java.lang.Object
public java.lang.String getName()
public void setName(java.lang.String newName)
public boolean isMethod()
public ConstPool getConstPool()
public boolean isConstructor()
public boolean isStaticInitializer()
public int getAccessFlags()
AccessFlag
public void setAccessFlags(int acc)
AccessFlag
public java.lang.String getDescriptor()
Descriptor
public void setDescriptor(java.lang.String desc)
Descriptor
public java.util.List getAttributes()
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.AttributeInfo
objects.AttributeInfo
public AttributeInfo getAttribute(java.lang.String name)
name
- attribute nameAttributeInfo
object or null.getAttributes()
public void addAttribute(AttributeInfo info)
getAttributes()
public ExceptionsAttribute getExceptionsAttribute()
public CodeAttribute getCodeAttribute()
public void removeExceptionsAttribute()
public void setExceptionsAttribute(ExceptionsAttribute cattr)
The added attribute must share the same constant pool table as this
method_info
structure.
public void removeCodeAttribute()
public void setCodeAttribute(CodeAttribute cattr)
The added attribute must share the same constant pool table as this
method_info
structure.
public void rebuildStackMapIf6(ClassPool pool, ClassFile cf) throws BadBytecode
doPreverify
is true, this method
also rebuilds a stack map for J2ME (CLDC).pool
- used for making type hierarchy.cf
- rebuild if this class file is for Java 6 or later.BadBytecode
rebuildStackMap(ClassPool)
,
rebuildStackMapForME(ClassPool)
,
doPreverify
public void rebuildStackMap(ClassPool pool) throws BadBytecode
MethodInfo
does not
include a code attribute, nothing happens.pool
- used for making type hierarchy.BadBytecode
StackMapTable
public void rebuildStackMapForME(ClassPool pool) throws BadBytecode
MethodInfo
does not
include a code attribute, nothing happens.pool
- used for making type hierarchy.BadBytecode
StackMap
public int getLineNumber(int pos)
pos
- the position of the bytecode (>= 0). an index into the code
array.public void setSuperclass(java.lang.String superclass) throws BadBytecode
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.
superclass
- the new super classBadBytecode
Javassist, a Java-bytecode translator toolkit.
Copyright (C) 1999- Shigeru Chiba. All Rights Reserved.