Class JMethod

All Implemented Interfaces:
JAnnotatable, JDeclaration, JDocCommentable, JGenerifiable

public class JMethod extends JGenerifiableImpl implements JDeclaration, JAnnotatable, JDocCommentable
Java method.
  • Field Details

    • mods

      private JMods mods
      Modifiers for this method
    • type

      private JType type
      Return type for this method
    • name

      private String name
      Name of this method
    • params

      private final List<JVar> params
      List of parameters for this method's declaration
    • _throws

      private Set<JClass> _throws
      Set of exceptions that this method may throw. A set instance lazily created.
    • body

      private JBlock body
      JBlock of statements that makes up the body this method
    • outer

      private JDefinedClass outer
    • jdoc

      private JDocComment jdoc
      javadoc comments for this JMethod
    • varParam

      private JVar varParam
      Variable parameter for this method's varargs declaration introduced in J2SE 1.5
    • annotations

      private List<JAnnotationUse> annotations
      Annotations on this variable. Lazily created.
    • defaultValue

      private JExpression defaultValue
      To set the default value for the annotation member
  • Constructor Details

    • JMethod

      JMethod(JDefinedClass outer, int mods, JType type, String name)
      JMethod constructor
      Parameters:
      mods - Modifiers for this method's declaration
      type - Return type for the method
      name - Name of this method
    • JMethod

      JMethod(int mods, JDefinedClass _class)
      Constructor constructor
      Parameters:
      mods - Modifiers for this constructor's declaration
      _class - JClass containing this constructor
  • Method Details

    • isConstructor

      private boolean isConstructor()
    • getThrows

      private Set<JClass> getThrows()
    • _throws

      public JMethod _throws(JClass exception)
      Add an exception to the list of exceptions that this method may throw.
      Parameters:
      exception - Name of an exception that this method may throw
    • _throws

      public JMethod _throws(Class<? extends Throwable> exception)
    • params

      public List<JVar> params()
      Returns the list of variable of this method.
      Returns:
      List of parameters of this method. This list is not modifiable.
    • param

      public JVar param(int mods, JType type, String name)
      Add the specified variable to the list of parameters for this method signature.
      Parameters:
      type - JType of the parameter being added
      name - Name of the parameter being added
      Returns:
      New parameter variable
    • param

      public JVar param(JType type, String name)
    • param

      public JVar param(int mods, Class<?> type, String name)
    • param

      public JVar param(Class<?> type, String name)
    • varParam

      public JVar varParam(Class<?> type, String name)
      See Also:
    • varParam

      public JVar varParam(JType type, String name)
      Add the specified variable argument to the list of parameters for this method signature.
      Parameters:
      type - Type of the parameter being added.
      name - Name of the parameter being added
      Returns:
      the variable parameter
      Throws:
      IllegalStateException - If this method is called twice. varargs in J2SE 1.5 can appear only once in the method signature.
    • annotate

      public JAnnotationUse annotate(JClass clazz)
      Adds an annotation to this variable.
      Specified by:
      annotate in interface JAnnotatable
      Parameters:
      clazz - The annotation class to annotate the field with
    • annotate

      public JAnnotationUse annotate(Class<? extends Annotation> clazz)
      Adds an annotation to this variable.
      Specified by:
      annotate in interface JAnnotatable
      Parameters:
      clazz - The annotation class to annotate the field with
    • annotate2

      public <W extends JAnnotationWriter> W annotate2(Class<W> clazz)
      Description copied from interface: JAnnotatable
      Adds an annotation to this program element and returns a type-safe writer to fill in the values of such annotations.
      Specified by:
      annotate2 in interface JAnnotatable
    • annotations

      public Collection<JAnnotationUse> annotations()
      Description copied from interface: JAnnotatable
      Read-only live view of all annotations on this JAnnotatable
      Specified by:
      annotations in interface JAnnotatable
      Returns:
      Can be empty but never null.
    • hasVarArgs

      public boolean hasVarArgs()
      Check if there are any varargs declared for this method signature.
    • name

      public String name()
    • name

      public void name(String n)
      Changes the name of the method.
    • type

      public JType type()
      Returns the return type.
    • type

      public void type(JType t)
      Overrides the return type.
    • listParamTypes

      public JType[] listParamTypes()
      Returns all the parameter types in an array.
      Returns:
      If there's no parameter, an empty array will be returned.
    • listVarParamType

      public JType listVarParamType()
      Returns the varags parameter type.
      Returns:
      If there's no vararg parameter type, null will be returned.
    • listParams

      public JVar[] listParams()
      Returns all the parameters in an array.
      Returns:
      If there's no parameter, an empty array will be returned.
    • listVarParam

      public JVar listVarParam()
      Returns the variable parameter
      Returns:
      If there's no parameter, null will be returned.
    • hasSignature

      public boolean hasSignature(JType[] argTypes)
      Returns true if the method has the specified signature.
    • body

      public JBlock body()
      Get the block that makes up body of this method
      Returns:
      Body of method
    • declareDefaultValue

      public void declareDefaultValue(JExpression value)
      Specify the default value for this annotation member
      Parameters:
      value - Default value for the annotation member
    • javadoc

      public JDocComment javadoc()
      Creates, if necessary, and returns the class javadoc for this JDefinedClass
      Specified by:
      javadoc in interface JDocCommentable
      Returns:
      JDocComment containing javadocs for this class
    • declare

      public void declare(JFormatter f)
      Specified by:
      declare in interface JDeclaration
      Overrides:
      declare in class JGenerifiableImpl
    • mods

      public JMods mods()
      Returns:
      the current modifiers of this method. Always return non-null valid object.
    • getMods

      public JMods getMods()
      Deprecated.
      use mods()
    • owner

      protected JCodeModel owner()
      Specified by:
      owner in class JGenerifiableImpl