Class MethodInfo


  • 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);
     
    See Also:
    getCodeAttribute(), CodeAttribute, Bytecode, CtBehavior.getMethodInfo(), CtBehavior.getMethodInfo()
    • Field Detail

      • 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 java.lang.String nameInit
        The name of constructors: <init>.
        See Also:
        Constant Field Values
      • nameClinit

        public static final java.lang.String nameClinit
        The name of class initializer (static initializer): <clinit>.
        See Also:
        Constant Field Values
    • Constructor Detail

      • MethodInfo

        public MethodInfo​(ConstPool cp,
                          java.lang.String methodname,
                          java.lang.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:
        Descriptor
      • MethodInfo

        public MethodInfo​(ConstPool cp,
                          java.lang.String methodname,
                          MethodInfo src,
                          java.util.Map<java.lang.String,​java.lang.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:
        Descriptor
    • Method Detail

      • toString

        public java.lang.String toString()
        Returns a string representation of the object.
        Overrides:
        toString in class java.lang.Object
      • getName

        public java.lang.String getName()
        Returns a method name.
      • setName

        public void setName​(java.lang.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 java.lang.String getDescriptor()
        Returns a method descriptor.
        See Also:
        Descriptor
      • setDescriptor

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

        public java.util.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:
        AttributeInfo
      • removeAttribute

        public AttributeInfo removeAttribute​(java.lang.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.

      • 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:
        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 this MethodInfo 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​(java.lang.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