Class MethodInfo

  • All Implemented Interfaces:
    AccessFlags

    public class MethodInfo
    extends MemberInfo
    implements AccessFlags
    Implementation of the "method_info" structure as defined in the JVM specification.
    Version:
    1.0
    • Field Detail

      • nameIndex

        private int nameIndex
        An index into the constant pool of a ConstantUtf8Info structure representing either one of the special method names ( "<init>" or "<clinit>") or a valid method name in the Java programming language, stored as a simple name.
      • descriptorIndex

        private int descriptorIndex
        An index into the constant pool of a ConstantUtf8Info structure representing a valid method descriptor.
      • signatureAttr

        private Signature signatureAttr
        The Signature attribute, or null if there isn't one for this method.
      • codeAttr

        private Code codeAttr
        The Code attribute, or null if this method is abstract or native.
      • attributes

        private java.util.List<AttributeInfo> attributes
        All attributes of this method that aren't explicitly covered by the private members signatureAttr and codeAttr.
      • paramTypes

        private java.lang.String[] paramTypes
        The type of all parameters to this method. Note that this cache will be short-lived, as classes that take type parameters will pass their type arguments down to individual MethodInfos when doing completions, to ensure types are as correct as possible.
      • returnType

        private java.lang.String returnType
        Cached return type.
      • nameAndParameters

        private java.lang.String nameAndParameters
        Cached string representing the name and parameters for this method.
      • SPECIAL_NAME_CONSTRUCTOR

        private static final java.lang.String SPECIAL_NAME_CONSTRUCTOR
        Used in class files to denote constructors.
        See Also:
        Constant Field Values
    • Constructor Detail

      • MethodInfo

        public MethodInfo​(ClassFile cf,
                          int accessFlags,
                          int nameIndex,
                          int descriptorIndex)
        Constructor.
        Parameters:
        cf - The class file defining this method.
        accessFlags - The access flags.
        nameIndex - The name index.
        descriptorIndex - The descriptor index.
    • Method Detail

      • addAttribute

        private void addAttribute​(AttributeInfo info)
        Adds the specified attribute to this field.
        Parameters:
        info - Information about the attribute.
      • appendParamDescriptors

        private void appendParamDescriptors​(java.lang.StringBuilder sb)
      • clearParamTypeInfo

        void clearParamTypeInfo()
        Called internally by ClassFile whenever its ClassFile.setTypeParamsToTypeArgs(Map) method is called. This clears this method's local cache of parameter/return types. They'll be lazily recomputed the next time they are needed. This allows this MethodInfo to be used for code completion for instances of the same class initialized with different type arguments.

        Note that if this method does not have parameterized arguments or return type, calling this method won't affect its behavior.

      • createParamTypesFromDescriptor

        private java.lang.String[] createParamTypesFromDescriptor​(boolean qualified)
        Creates an array of types of each parameter by looking at the method's descriptor field. This technique should work with Java 1.0+, but won't pick up on generic types added in Java 5.
        Returns:
        The parameter types.
        See Also:
        createParamTypesFromTypeSignature()
      • createParamTypesFromTypeSignature

        private java.lang.String[] createParamTypesFromTypeSignature()
        Creates an array of types of each parameter by looking at the method's Signature attribute, and querying the parent ClassFile instance for any type argument values. This attribute was introduced in Java 5, and is the only way to detect generic parameters.
        Returns:
        The parameter types.
        See Also:
        createParamTypesFromDescriptor(boolean)
      • getAttribute

        public AttributeInfo getAttribute​(int index)
        Returns the specified attribute.
        Parameters:
        index - The index of the attribute.
        Returns:
        The attribute.
      • getAttributeCount

        public int getAttributeCount()
        Returns the number of attributes of this field.
        Returns:
        The number of attributes.
      • getDescriptor

        public java.lang.String getDescriptor()
        Description copied from class: MemberInfo
        Returns the descriptor of this member.
        Specified by:
        getDescriptor in class MemberInfo
        Returns:
        The descriptor of this member.
      • getName

        public java.lang.String getName()
        Description copied from class: MemberInfo
        Returns the name of this member.
        Specified by:
        getName in class MemberInfo
        Returns:
        The name of this member.
      • getNameAndParameters

        public java.lang.String getNameAndParameters()
        Returns the name and parameters of this method, in the form performAction(String, int, Runnable).
        Returns:
        The name and parameters of this method.
      • getParameterName

        public java.lang.String getParameterName​(int index)
        If debugging was enabled during compilation, this method returns the name of the given parameter to this method. Otherwise, null is returned.
        Parameters:
        index - The index of the parameter.
        Returns:
        The name of the parameter, or null.
      • getParameterType

        public java.lang.String getParameterType​(int index,
                                                 boolean fullyQualified)
        Returns a string representing the type of parameter to this method.
        Parameters:
        index - The index of the parameter.
        fullyQualified - Whether the returned type should be fully qualified. Note that if fully qualified information is not available for the parameters to this method, this parameter will be ignored (but I'm not sure that ever happens).
        Returns:
        The type of the parameter.
        See Also:
        getParameterCount(), getParameterTypes()
      • getParameterTypes

        public java.lang.String[] getParameterTypes()
        Returns an array if strings representing the types of all parameters to this method. If this method takes no parameters, a zero-length array is returned.
        Returns:
        The array. These types will likely be fully qualified.
        See Also:
        getParameterCount(), getParameterType(int, boolean)
      • getReturnTypeString

        public java.lang.String getReturnTypeString​(boolean fullyQualified)
        Returns the return type of this method.
        Parameters:
        fullyQualified - Whether the returned value should be fully-qualified.
        Returns:
        The return type of this method.
      • getReturnTypeStringFromDescriptor

        private java.lang.String getReturnTypeStringFromDescriptor​(boolean qualified)
        Returns the return type of this method, as determined by a snippet of the method descriptor. This should work with all class files created in Java 1.0+, but won't discover the generic types added in Java 5.
        Returns:
        The return type of this method.
        See Also:
        getReturnTypeStringFromTypeSignature(boolean)
      • getReturnTypeStringFromTypeSignature

        private java.lang.String getReturnTypeStringFromTypeSignature​(boolean qualified)
        Returns the return type of this method, as determined by the Signature attribute that was added in Java 5. This allows us to check for generic types.
        Returns:
        The return type of this method.
        See Also:
        getReturnTypeStringFromDescriptor(boolean)
      • getSignature

        public java.lang.String getSignature()
        Returns the signature of this method, as determined from its method descriptor.
        Returns:
        The signature of this method.
      • isAbstract

        public boolean isAbstract()
        Returns whether this method is abstract.
        Returns:
        Whether this method is abstract.
      • isConstructor

        public boolean isConstructor()
        Returns whether this method is a constructor.
        Returns:
        Whether this method is a constructor.
      • isNative

        public boolean isNative()
        Returns whether this method is native.
        Returns:
        Whether this method is native.
      • isStatic

        public boolean isStatic()
        Returns whether this method is static.
        Overrides:
        isStatic in class MemberInfo
        Returns:
        Whether this method is static.
      • read

        public static MethodInfo read​(ClassFile cf,
                                      java.io.DataInputStream in)
                               throws java.io.IOException
        Reads a MethodInfo from an input stream.
        Parameters:
        cf - The class file defining the method.
        in - The input stream to read from.
        Returns:
        The method information read.
        Throws:
        java.io.IOException - If an IO error occurs.
      • readAttribute

        private AttributeInfo readAttribute​(java.io.DataInputStream in)
                                     throws java.io.IOException
        Reads an attribute for this method from the specified input stream.
        Parameters:
        in - The input stream to read from.
        Returns:
        The attribute read, possibly null if it was known to be unimportant for our purposes.
        Throws:
        java.io.IOException - If an IO error occurs.