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);
 
See Also:
  • Field Details

    • doPreverify

      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.
    • nameInit

      public static final String nameInit
      The name of constructors: <init>.
      See Also:
    • nameClinit

      public static final String nameClinit
      The name of class initializer (static initializer): <clinit>.
      See Also:
  • Constructor Details

    • MethodInfo

      public MethodInfo(ConstPool cp, String methodname, String desc)
      Constructs a method_info structure. The initial value of access_flags is zero.
      Parameters:
      cp - a constant pool table
      methodname - method name
      desc - method descriptor
      See Also:
    • MethodInfo

      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 - a constant pool table
      methodname - a method name
      src - a source method_info
      classnameMap - specifies pairs of replaced and substituted name.
      Throws:
      BadBytecode
      See Also:
  • Method Details

    • toString

      public String toString()
      Returns a string representation of the object.
      Overrides:
      toString in class 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:
    • setAccessFlags

      public void setAccessFlags(int acc)
      Sets access flags.
      See Also:
    • getDescriptor

      public String getDescriptor()
      Returns a method descriptor.
      See Also:
    • setDescriptor

      public void setDescriptor(String desc)
      Sets a method descriptor.
      See Also:
    • getAttributes

      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:
      a list of AttributeInfo objects.
      See Also:
    • 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 or AnnotationsAttribute.invisibleTag.

      Parameters:
      name - attribute name
      Returns:
      an AttributeInfo object or null.
      See Also:
    • 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:
    • 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. If doPreverify 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

      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 - used for making type hierarchy.
      Throws:
      BadBytecode
      Since:
      3.6
      See Also:
    • 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 this MethodInfo 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

      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