home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Programming Languages Suite
/
ProgLangD.iso
/
VCAFE.3.0A
/
Main.bin
/
DTMethod.java
< prev
next >
Wrap
Text File
|
1998-10-25
|
7KB
|
220 lines
/*
* Copyright 1998 Symantec Corporation, All Rights Reserved.
*/
package com.symantec.itools.vcafe.openapi.dtreflect;
import com.symantec.itools.vcafe.openapi.Range;
import java.lang.reflect.*;
/**
* A Method provides information about, and access to, a single method
* on a class or interface. The reflected method may be a class method
* or an instance method (including an abstract method).
*
* <p>A Method permits widening conversions to occur when matching the
* actual parameters to invokewith the underlying method's formal
* parameters, but it throws an IllegalArgumentException if a
* narrowing conversion would occur.
*
* @see com.symantec.itools.vcafe.openapi.dtreflect.DTMember
* @see com.symantec.itools.vcafe.openapi.dtreflect.DTClass#getMethods()
* @see com.symantec.itools.vcafe.openapi.dtreflect.DTClass#getMethod()
* @see com.symantec.itools.vcafe.openapi.dtreflect.DTClass#getDeclaredMethods()
* @see com.symantec.itools.vcafe.openapi.dtreflect.DTClass#getDeclaredMethod()
*
* @author RKF
*/
public class DTMethod extends DTMember implements java.io.Serializable
{
static final long serialVersionUID = -2372011401088924874L;
transient boolean valid; // set by validate
// The returns, parameters and exception strings keep from having to
// preallocate arrays of class objects when they may not
// be needed. The downside is that they introduce the possibility
// of ClassNotFoundException where the corresponding Method call
// cannot throw. Such methods instead throw a NullPointerException
// to keep their protocols the same.
/**
* Constructor. Usually, a DTMethod is constructed by native methods,
* but for some uses needs to be reconstructed from data.
*
* @param clazz class or interface the method is declared in
* @param name method name
* @param modifiers flags (see java.lang.reflect.Modifier)
* @param parameters arg types expressed as "typename1,typename2,...,typenamen"
* @param returns return type expressed as "typename"
* @param exceptions exceptions expressed as "exceptionType1,...,exceptionTypen"
* with the exception type names sorted in canonical order
* ("alphabetical" according to their Unicode char values)
*/
DTMethod(DTClass clazz, String name, int modifiers, String returns, String parameters, String parameterNames, String exceptions) {
super(clazz,name,modifiers,parameters,parameterNames,returns,exceptions);
}
/**
* Constructor for serialization.
*/
DTMethod() {
}
/**
* Get the method's return type.
*/
public DTClass getReturnType() {
return getType();
}
/**
* gets the text range from the beginning of the method declaration to the closing brace
* (or semicolon for native or abstract members)
*/
public Range getSourceRange() {
return clazz.getSourceRange(this);
}
/**
* gets the text range from the beginning of the first line of the method's Javadoc comment
* to the closing asterisk-slash characters
*/
public Range getJavadocRange() {
return clazz.getJavadocRange(this);
}
/**
* Renames the method to new name
* @param newName: The new name of the field
* @return Success (true) or failure (false)...
*/
public boolean rename(String newName) {
return clazz.rename(this, newName);
}
/**
* Compares this DTMethod against the specified object. Returns
* true if the objects are the same. Two Methods are the same if
* they were declared by the same class and have the same name,
* formal parameter types, return type, exceptions thrown
* and modifiers.
*/
public boolean equals(Object obj) {
if (obj instanceof DTMethod) {
DTMethod other = (DTMethod)obj;
return this.equals(other);
}
return false;
}
/**
* Compares this DTMethod against the specified DTMethod. Returns
* true if the methods are the same. Two Methods are the same if
* they were declared by the same class and have the same name,
* formal parameter types, return type, exceptions thrown
* and modifiers.
*/
public boolean equals(DTMethod other) {
if ((other != null)
&& (clazz.equals(other.clazz))
&& (name.equals(other.name))
&& (parameters.equals(other.parameters))
&& (returns.equals(other.returns))
&& (exceptions.equals(other.exceptions))
&& (modifiers == other.modifiers))
return true;
return false;
}
/**
* Compares the formal signature of this DTMethod with another.
* Returns true if the unqualified method name and number and
* types of arguments are the same.
public boolean equalSignatures(DTMember other) {
if (other != null
&& getName().equals(other.getName())
&& getParameters().equals(other.getParameters()))
return true;
return false;
}
/**
* Returns a string describing this DTMethod. The string is
* formatted as the method access modifiers, if any, followed by
* the method return type, followed by a space, followed by the
* class declaring the method, followed by a period, followed by
* the method name, followed by a parenthesized, comma-separated
* list of the method's formal parameter types. If the method
* throws checked exceptions, the parameter list is followed by a
* space, followed by the word throws followed by a
* comma-separated list of the thrown exception types.
* For example:
* <pre>
* public boolean java.lang.Object.equals(java.lang.Object)
* </pre>
*
* <p>The access modifiers are placed in canonical order as
* specified by "The Java Language Specification". This is
* <tt>public</tt>, <tt>protected</tt> or <tt>private</tt> first,
* and then other modifiers in the following order:
* <tt>abstract</tt>, <tt>static</tt>, <tt>final</tt>,
* <tt>synchronized</tt> <tt>native</tt>.
*/
public String toString() {
try {
StringBuffer sb = new StringBuffer();
int mod = getModifiers();
if (mod != 0) {
sb.append(Modifier.toString(mod));
sb.append(" ");
}
sb.append(returns);
sb.append(" ");
sb.append(getDeclaringClass().getName());
sb.append(".");
sb.append(getName());
sb.append("(");
sb.append(parameters);
sb.append(")");
if (exceptions.length() > 0) {
sb.append(" throws ");
sb.append(exceptions);
}
return sb.toString();
} catch (Exception e) {
return "<" + e + ">";
}
}
/**
* Returns a short string describing this DTMethod.
* The string consists of the method name followed by the
* parameter types in parenthesis, e.g., "substring(int,int)".
*/
public String toShortString() {
try {
StringBuffer sb = new StringBuffer();
sb.append(name + "(");
sb.append(parameters);
sb.append(")");
return sb.toString();
} catch (Exception e) {
return "<" + e + ">";
}
}
/**
* Finish serializing in.
*/
private void readObject(java.io.ObjectInputStream in)
throws java.io.IOException, ClassNotFoundException {
in.defaultReadObject();
validate();
}
public boolean validate() {
DTMethod method = clazz.getDeclaredMethod(name, getParameterTypes());
return valid = equals(method);
}
}