Class JMethod

  • All Implemented Interfaces:
    JAnnotatedElement, JMember

    public final class JMethod
    extends java.lang.Object
    implements JMember, JAnnotatedElement
    A class which holds information about the methods of a JClass. Modelled closely after the Java Reflection API. This class is part of package which is used to create source code.
    Version:
    $Revision: 7331 $ $Date: 2004-12-03 11:57:33 -0700 (Fri, 03 Dec 2004) $
    Author:
    Keith Visco
    • Constructor Detail

      • JMethod

        public JMethod​(java.lang.String name)
        Creates a new JMethod with the given name and "void" return type.
        Parameters:
        name - The method name. Must not be null.
      • JMethod

        public JMethod​(java.lang.String name,
                       JType returnType,
                       java.lang.String returnDoc)
        Creates a new JMethod with the given name and returnType. The return type must not be empty or null. For "void" return types, use JMethod(String) instead of this constructor.
        Parameters:
        name - The method name. Must not be null.
        returnType - The return type of the method. Must not be null.
        returnDoc - Javadoc comment for the @return annotation. If null, a default (and mostly useless) javadoc comment will be generated.
    • Method Detail

      • addException

        public void addException​(JClass exp,
                                 java.lang.String description)
        Adds the given Exception to this JMethod's throws clause.
        Parameters:
        exp - The JClass representing the Exception.
        description - JavaDoc comment explaining when this exception is thrown.
      • addParameter

        public void addParameter​(JParameter parameter)
        Adds the given parameter to this JMethod's list of parameters.
        Parameters:
        parameter - The parameter to add to the this JMethod's list of parameters.
      • getJDocComment

        public JDocComment getJDocComment()
        Returns the JavaDoc comment describing this JMethod.
        Returns:
        The JavaDoc comment describing this JMethod.
      • getExceptions

        public JClass[] getExceptions()
        Returns the exceptions that this JMethod throws.
        Returns:
        The exceptions that this JMethod throws.
      • getExceptionCount

        public int getExceptionCount()
        Returns the amount of exceptions thrown.
        Returns:
        The amount of exceptions thrown.
      • getModifiers

        public JModifiers getModifiers()
        Returns the modifiers for this JMethod.
        Specified by:
        getModifiers in interface JMember
        Returns:
        The modifiers for this JMethod.
      • getName

        public java.lang.String getName()
        Returns the name of this JMethod.
        Specified by:
        getName in interface JMember
        Returns:
        The name of this JMethod.
      • getParameter

        public JParameter getParameter​(int index)
        Returns the JParameter at the given index.
        Parameters:
        index - The index of the JParameter to return.
        Returns:
        The JParameter at the given index.
      • getParameters

        public JParameter[] getParameters()
        Returns the set of JParameters for this JMethod.
        Note: the array is a copy, the parameters in the array are the actual references.
        Returns:
        The set of JParameters for this JMethod.
      • getParameterCount

        public int getParameterCount()
        Returns the amount of parameters.
        Returns:
        The amount of parameters.
      • getReturnType

        public JType getReturnType()
        Returns the JType that represents the return type of the JMethod.
        Returns:
        The JType that represents the return type of the JMethod.
      • getSignature

        public JMethodSignature getSignature()
        Returns the JMethodSignature for this JMethod.
        Returns:
        The JMethodSignature for this JMethod.
      • getSourceCode

        public JSourceCode getSourceCode()
        Returns the JSourceCode for the method body.
        Returns:
        The JSourceCode for the method body.
      • setName

        public void setName​(java.lang.String name)
        Sets the name of this JMethod.
        Parameters:
        name - The name of this method.
      • setComment

        public void setComment​(java.lang.String comment)
        Sets the comment describing this JMethod. The comment will be printed when this JMethod is printed.
        Parameters:
        comment - The comment for this member.
      • setModifiers

        public void setModifiers​(JModifiers modifiers)
        Sets the JModifiers for this JMethod. This JMethod will use only a copy of the JModifiers.
        Note: The JModifiers will be set in the containing JMethodSignature. If the JMethodSignature is used by other methods, keep in mind that it will be changed.
        Parameters:
        modifiers - The JModifiers to set.
      • setSourceCode

        public void setSourceCode​(java.lang.String source)
        Sets the given string as the source code (method body) for this JMethod.
        Parameters:
        source - The String that represents the method body.
      • setSourceCode

        public void setSourceCode​(JSourceCode source)
        Sets the given JSourceCode as the source code (method body) for this JMethod.
        Parameters:
        source - The JSourceCode that represents the method body.
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • getAnnotation

        public JAnnotation getAnnotation​(JAnnotationType annotationType)
        Retrieves a JAnnotation for the given JAnnotationType, returns null if no annotation has been set.
        Specified by:
        getAnnotation in interface JAnnotatedElement
        Parameters:
        annotationType - Annotation type to retrieve.
        Returns:
        A JAnnotation for the given JAnnotationType.
      • getAnnotations

        public JAnnotation[] getAnnotations()
        Returns a list of JAnnotation's already set on this source element.
        Specified by:
        getAnnotations in interface JAnnotatedElement
        Returns:
        A list of all JAnnotations associated with this source element.
      • isAnnotationPresent

        public boolean isAnnotationPresent​(JAnnotationType annotationType)
        Returns true if a JAnnotation exists for the given JAnnotationType.
        Specified by:
        isAnnotationPresent in interface JAnnotatedElement
        Parameters:
        annotationType - Annotation type to check for presence or absense.
        Returns:
        True if a JAnnotation has been added for the given JAnnotationType.
      • addAnnotation

        public void addAnnotation​(JAnnotation annotation)
        Adds a JAnnotation to this source element. An IllegalArgumentException is thrown if one already exists for the associated JAnnotationType.
        Specified by:
        addAnnotation in interface JAnnotatedElement
        Parameters:
        annotation - A JAnnotation to add to this source element.
      • removeAnnotation

        public JAnnotation removeAnnotation​(JAnnotationType annotationType)
        Removes the JAnnotation from this source element for the given JAnnotationType. An IllegalArgumentException is thrown if the provided JAnnotation isn't present.
        Specified by:
        removeAnnotation in interface JAnnotatedElement
        Parameters:
        annotationType - Annotation type to remove.
        Returns:
        The JAnnotation that was associated with this source element.
      • hasAnnotations

        public boolean hasAnnotations()
        Returns true if this source element has any annotations.
        Specified by:
        hasAnnotations in interface JAnnotatedElement
        Returns:
        Returns true if this source element has any annotations.
      • print

        public void print​(JSourceWriter jsw)
        Prints this JMethod to the given JSourceWriter.
        Parameters:
        jsw - The JSourceWriter to print to.