Package org.exolab.javasource
Class JMethod
java.lang.Object
org.exolab.javasource.JMethod
- All Implemented Interfaces:
JAnnotatedElement
,JMember
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$ $Date: 2004-12-03 11:57:33 -0700 (Fri, 03 Dec 2004) $
- Author:
- Keith Visco
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
addAnnotation
(JAnnotation annotation) Adds a JAnnotation to this source element.void
addException
(JClass exp, String description) Adds the given Exception to this JMethod's throws clause.void
addParameter
(JParameter parameter) Adds the given parameter to this JMethod's list of parameters.getAnnotation
(JAnnotationType annotationType) Retrieves a JAnnotation for the given JAnnotationType, returns null if no annotation has been set.Returns a list of JAnnotation's already set on this source element.int
Returns the amount of exceptions thrown.JClass[]
Returns the exceptions that this JMethod throws.Returns the JavaDoc comment describing this JMethod.Returns the modifiers for this JMethod.getName()
Returns the name of this JMethod.getParameter
(int index) Returns the JParameter at the given index.int
Returns the amount of parameters.Returns the set of JParameters for this JMethod.Returns the JType that represents the return type of the JMethod.Returns the JMethodSignature for this JMethod.Returns the JSourceCode for the method body.boolean
Returns true if this source element has any annotations.boolean
isAnnotationPresent
(JAnnotationType annotationType) Returns true if a JAnnotation exists for the given JAnnotationType.void
print
(JSourceWriter jsw) Prints this JMethod to the given JSourceWriter.removeAnnotation
(JAnnotationType annotationType) Removes the JAnnotation from this source element for the given JAnnotationType.void
setComment
(String comment) Sets the comment describing this JMethod.void
setModifiers
(JModifiers modifiers) Sets the JModifiers for this JMethod.void
Sets the name of this JMethod.void
setSourceCode
(String source) Sets the given string as the source code (method body) for this JMethod.void
setSourceCode
(JSourceCode source) Sets the given JSourceCode as the source code (method body) for this JMethod.toString()
-
Constructor Details
-
JMethod
Creates a new JMethod with the given name and "void" return type.- Parameters:
name
- The method name. Must not be null.
-
JMethod
Creates a new JMethod with the given name and returnType. The return type must not be empty or null. For "void" return types, useJMethod(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 Details
-
addException
-
addParameter
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
Returns the JavaDoc comment describing this JMethod.- Returns:
- The JavaDoc comment describing this JMethod.
-
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
Returns the modifiers for this JMethod.- Specified by:
getModifiers
in interfaceJMember
- Returns:
- The modifiers for this JMethod.
-
getName
-
getParameter
Returns the JParameter at the given index.- Parameters:
index
- The index of the JParameter to return.- Returns:
- The JParameter at the given index.
-
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
Returns the JType that represents the return type of the JMethod.- Returns:
- The JType that represents the return type of the JMethod.
-
getSignature
Returns the JMethodSignature for this JMethod.- Returns:
- The JMethodSignature for this JMethod.
-
getSourceCode
Returns the JSourceCode for the method body.- Returns:
- The JSourceCode for the method body.
-
setName
Sets the name of this JMethod.- Parameters:
name
- The name of this method.
-
setComment
Sets the comment describing this JMethod. The comment will be printed when this JMethod is printed.- Parameters:
comment
- The comment for this member.
-
setModifiers
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
Sets the given string as the source code (method body) for this JMethod.- Parameters:
source
- The String that represents the method body.
-
setSourceCode
Sets the given JSourceCode as the source code (method body) for this JMethod.- Parameters:
source
- The JSourceCode that represents the method body.
-
toString
-
getAnnotation
Retrieves a JAnnotation for the given JAnnotationType, returns null if no annotation has been set.- Specified by:
getAnnotation
in interfaceJAnnotatedElement
- Parameters:
annotationType
- Annotation type to retrieve.- Returns:
- A JAnnotation for the given JAnnotationType.
-
getAnnotations
Returns a list of JAnnotation's already set on this source element.- Specified by:
getAnnotations
in interfaceJAnnotatedElement
- Returns:
- A list of all JAnnotations associated with this source element.
-
isAnnotationPresent
Returns true if a JAnnotation exists for the given JAnnotationType.- Specified by:
isAnnotationPresent
in interfaceJAnnotatedElement
- Parameters:
annotationType
- Annotation type to check for presence or absense.- Returns:
- True if a JAnnotation has been added for the given JAnnotationType.
-
addAnnotation
Adds a JAnnotation to this source element. An IllegalArgumentException is thrown if one already exists for the associated JAnnotationType.- Specified by:
addAnnotation
in interfaceJAnnotatedElement
- Parameters:
annotation
- A JAnnotation to add to this source element.
-
removeAnnotation
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 interfaceJAnnotatedElement
- 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 interfaceJAnnotatedElement
- Returns:
- Returns true if this source element has any annotations.
-
print
Prints this JMethod to the given JSourceWriter.- Parameters:
jsw
- The JSourceWriter to print to.
-