org.exolab.javasource

Class JMethod

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: 6324 $ $Date: 2004-12-03 11:57:33 -0700 (Fri, 03 Dec 2004) $
Author:
Keith Visco

Constructor Summary

JMethod(String name)
Creates a new JMethod with the given name and "void" return type.
JMethod(String name, JType returnType, String returnDoc)
Creates a new JMethod with the given name and returnType.

Method Summary

void
addAnnotation(JAnnotation annotation)
protected void
addDeclaringClass(JClass jClass)
Adds the given JClass to the set of classes that contain this method.
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.
JAnnotation
getAnnotation(JAnnotationType annotationType)
JAnnotation[]
getAnnotations()
JClass[]
getExceptions()
Returns the exceptions that this JMethod throws.
JDocComment
getJDocComment()
Returns the JavaDoc comment describing this JMethod.
JModifiers
getModifiers()
Returns the modifiers for this JMethod.
String
getName()
Returns the name of this JMethod.
JParameter
getParameter(int index)
Returns the JParameter at the given index.
protected String[]
getParameterClassNames()
Return the list of class names representing the parameters.
JParameter[]
getParameters()
Returns the set of JParameters for this JMethod.
JType
getReturnType()
Returns the JType that represents the return type of the JMethod.
JMethodSignature
getSignature()
Returns the JMethodSignature for this JMethod.
JSourceCode
getSourceCode()
Returns the JSourceCode for the method body.
boolean
hasAnnotations()
boolean
isAnnotationPresent(JAnnotationType annotationType)
void
print(JSourceWriter jsw)
Prints this JMethod to the given JSourceWriter.
JAnnotation
removeAnnotation(JAnnotationType annotationType)
protected void
removeDeclaringClass(JClass jClass)
Removes the given JClass from the set of classes that contain this method.
void
setComment(String comment)
Sets the comment describing this JMethod.
void
setModifiers(JModifiers modifiers)
Sets the JModifiers for this JMethod.
void
setName(String name)
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.
String
toString()
Returns the String representation of this JMethod, which is the method prototype.

Constructor Details

JMethod

public JMethod(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(String name,
               JType returnType,
               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 Details

addAnnotation

public void addAnnotation(JAnnotation annotation)
Specified by:
addAnnotation in interface JAnnotatedElement

addDeclaringClass

protected void addDeclaringClass(JClass jClass)
Adds the given JClass to the set of classes that contain this method.
Parameters:
jClass - the JClass to add as one of the JClasses that contain this method

addException

public void addException(JClass exp,
                         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.

getAnnotation

public JAnnotation getAnnotation(JAnnotationType annotationType)
Specified by:
getAnnotation in interface JAnnotatedElement

getAnnotations

public JAnnotation[] getAnnotations()
Specified by:
getAnnotations in interface JAnnotatedElement
See Also:
{@inheritDoc}

getExceptions

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

getJDocComment

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

getModifiers

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

getName

public 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.

getParameterClassNames

protected String[] getParameterClassNames()
Return the list of class names representing the parameters.
Returns:
the list of class names representing the parameters

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.

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.

hasAnnotations

public boolean hasAnnotations()
Specified by:
hasAnnotations in interface JAnnotatedElement
See Also:
{@inheritDoc}

isAnnotationPresent

public boolean isAnnotationPresent(JAnnotationType annotationType)
Specified by:
isAnnotationPresent in interface JAnnotatedElement

print

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

removeAnnotation

public JAnnotation removeAnnotation(JAnnotationType annotationType)
Specified by:
removeAnnotation in interface JAnnotatedElement

removeDeclaringClass

protected void removeDeclaringClass(JClass jClass)
Removes the given JClass from the set of classes that contain this method.
Parameters:
jClass - the JClass to remove as one of the JClasses that contain this method

setComment

public void setComment(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.

setName

public void setName(String name)
Sets the name of this JMethod.
Parameters:
name - the name of this method

setSourceCode

public void setSourceCode(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 String toString()
Returns the String representation of this JMethod, which is the method prototype.
Returns:
the String representation of this JMethod, which is simply the method prototype

Intalio Inc. (C) 1999-2006. All rights reserved http://www.intalio.com