Package org.exolab.javasource
Class JMethod
- java.lang.Object
-
- org.exolab.javasource.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
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddAnnotation(JAnnotation annotation)Adds a JAnnotation to this source element.voidaddException(JClass exp, java.lang.String description)Adds the given Exception to this JMethod's throws clause.voidaddParameter(JParameter parameter)Adds the given parameter to this JMethod's list of parameters.JAnnotationgetAnnotation(JAnnotationType annotationType)Retrieves a JAnnotation for the given JAnnotationType, returns null if no annotation has been set.JAnnotation[]getAnnotations()Returns a list of JAnnotation's already set on this source element.intgetExceptionCount()Returns the amount of exceptions thrown.JClass[]getExceptions()Returns the exceptions that this JMethod throws.JDocCommentgetJDocComment()Returns the JavaDoc comment describing this JMethod.JModifiersgetModifiers()Returns the modifiers for this JMethod.java.lang.StringgetName()Returns the name of this JMethod.JParametergetParameter(int index)Returns the JParameter at the given index.intgetParameterCount()Returns the amount of parameters.JParameter[]getParameters()Returns the set of JParameters for this JMethod.JTypegetReturnType()Returns the JType that represents the return type of the JMethod.JMethodSignaturegetSignature()Returns the JMethodSignature for this JMethod.JSourceCodegetSourceCode()Returns the JSourceCode for the method body.booleanhasAnnotations()Returns true if this source element has any annotations.booleanisAnnotationPresent(JAnnotationType annotationType)Returns true if a JAnnotation exists for the given JAnnotationType.voidprint(JSourceWriter jsw)Prints this JMethod to the given JSourceWriter.JAnnotationremoveAnnotation(JAnnotationType annotationType)Removes the JAnnotation from this source element for the given JAnnotationType.voidsetComment(java.lang.String comment)Sets the comment describing this JMethod.voidsetModifiers(JModifiers modifiers)Sets the JModifiers for this JMethod.voidsetName(java.lang.String name)Sets the name of this JMethod.voidsetSourceCode(java.lang.String source)Sets the given string as the source code (method body) for this JMethod.voidsetSourceCode(JSourceCode source)Sets the given JSourceCode as the source code (method body) for this JMethod.java.lang.StringtoString()
-
-
-
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, 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 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:
getModifiersin interfaceJMember- Returns:
- The modifiers for this JMethod.
-
getName
public java.lang.String getName()
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:
toStringin classjava.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:
getAnnotationin interfaceJAnnotatedElement- 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:
getAnnotationsin interfaceJAnnotatedElement- 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:
isAnnotationPresentin interfaceJAnnotatedElement- 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:
addAnnotationin interfaceJAnnotatedElement- 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:
removeAnnotationin 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:
hasAnnotationsin interfaceJAnnotatedElement- 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.
-
-