home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgLangD.iso / VCAFE.3.0A / Main.bin / DTMethod.java < prev    next >
Text File  |  1998-10-25  |  7KB  |  220 lines

  1. /*
  2.  * Copyright 1998 Symantec Corporation, All Rights Reserved.
  3.  */
  4.  
  5. package com.symantec.itools.vcafe.openapi.dtreflect;
  6.  
  7. import com.symantec.itools.vcafe.openapi.Range;
  8. import java.lang.reflect.*;
  9.  
  10. /**
  11.  * A Method provides information about, and access to, a single method
  12.  * on a class or interface.  The reflected method may be a class method
  13.  * or an instance method (including an abstract method).
  14.  *
  15.  * <p>A Method permits widening conversions to occur when matching the
  16.  * actual parameters to invokewith the underlying method's formal
  17.  * parameters, but it throws an IllegalArgumentException if a
  18.  * narrowing conversion would occur.
  19.  *
  20.  * @see com.symantec.itools.vcafe.openapi.dtreflect.DTMember
  21.  * @see com.symantec.itools.vcafe.openapi.dtreflect.DTClass#getMethods()
  22.  * @see com.symantec.itools.vcafe.openapi.dtreflect.DTClass#getMethod()
  23.  * @see com.symantec.itools.vcafe.openapi.dtreflect.DTClass#getDeclaredMethods()
  24.  * @see com.symantec.itools.vcafe.openapi.dtreflect.DTClass#getDeclaredMethod()
  25.  *
  26.  * @author RKF
  27.  */
  28. public class DTMethod extends DTMember implements java.io.Serializable
  29. {
  30.     static final long serialVersionUID = -2372011401088924874L;
  31.  
  32.     transient boolean    valid;            // set by validate
  33.  
  34.     // The returns, parameters and exception strings keep from having to
  35.     // preallocate arrays of class objects when they may not
  36.     // be needed. The downside is that they introduce the possibility
  37.     // of ClassNotFoundException where the corresponding Method call
  38.     // cannot throw. Such methods instead throw a NullPointerException
  39.     // to keep their protocols the same.
  40.  
  41.     /**
  42.      * Constructor. Usually, a DTMethod is constructed by native methods,
  43.      * but for some uses needs to be reconstructed from data.
  44.      *
  45.      * @param    clazz        class or interface the method is declared in
  46.      * @param    name        method name
  47.      * @param    modifiers    flags (see java.lang.reflect.Modifier)
  48.      * @param    parameters    arg types expressed as "typename1,typename2,...,typenamen"
  49.      * @param    returns        return type expressed as "typename"
  50.      * @param    exceptions    exceptions expressed as "exceptionType1,...,exceptionTypen"
  51.      *                        with the exception type names sorted in canonical order
  52.      *                        ("alphabetical" according to their Unicode char values)
  53.      */
  54.     DTMethod(DTClass clazz, String name, int modifiers, String returns, String parameters, String parameterNames, String exceptions) {
  55.         super(clazz,name,modifiers,parameters,parameterNames,returns,exceptions);
  56.     }
  57.  
  58.     /**
  59.      * Constructor for serialization.
  60.      */
  61.     DTMethod() {
  62.     }
  63.  
  64.     /**
  65.      * Get the method's return type.
  66.      */
  67.     public DTClass getReturnType() {
  68.         return getType();
  69.     }
  70.  
  71.     /**
  72.      * gets the text range from the beginning of the method declaration to the closing brace
  73.      * (or semicolon for native or abstract members)
  74.      */
  75.     public Range getSourceRange() {
  76.         return clazz.getSourceRange(this);
  77.     }
  78.  
  79.     /**
  80.      * gets the text range from the beginning of the first line of the method's Javadoc comment
  81.      * to the closing asterisk-slash characters
  82.      */
  83.     public Range getJavadocRange() {
  84.         return clazz.getJavadocRange(this);
  85.     }
  86.  
  87.     /**
  88.      * Renames the method to new name
  89.      * @param newName: The new name of the field
  90.      * @return Success (true) or failure (false)...
  91.      */
  92.     public boolean rename(String newName) {
  93.         return clazz.rename(this, newName);
  94.     }
  95.  
  96.     /**
  97.      * Compares this DTMethod against the specified object.  Returns
  98.      * true if the objects are the same.  Two Methods are the same if
  99.      * they were declared by the same class and have the same name,
  100.      * formal parameter types, return type, exceptions thrown
  101.      * and modifiers.
  102.      */
  103.     public boolean equals(Object obj) {
  104.         if (obj instanceof DTMethod) {
  105.             DTMethod other = (DTMethod)obj;
  106.             return this.equals(other);
  107.         }
  108.         return false;
  109.     }
  110.  
  111.     /**
  112.      * Compares this DTMethod against the specified DTMethod.  Returns
  113.      * true if the methods are the same.  Two Methods are the same if
  114.      * they were declared by the same class and have the same name,
  115.      * formal parameter types, return type, exceptions thrown
  116.      * and modifiers.
  117.      */
  118.     public boolean equals(DTMethod other) {
  119.         if ((other != null)
  120.             && (clazz.equals(other.clazz))
  121.             && (name.equals(other.name))
  122.             && (parameters.equals(other.parameters))
  123.             && (returns.equals(other.returns))
  124.             && (exceptions.equals(other.exceptions))
  125.             && (modifiers == other.modifiers))
  126.                 return true;
  127.         return false;
  128.     }
  129.  
  130.     /**
  131.      * Compares the formal signature of this DTMethod with another.
  132.      * Returns true if the unqualified method name and number and
  133.      * types of arguments are the same.
  134.     public boolean equalSignatures(DTMember other) {
  135.         if (other != null
  136.         && getName().equals(other.getName())
  137.         && getParameters().equals(other.getParameters()))
  138.             return true;
  139.         return false;
  140.     }
  141.  
  142.     /**
  143.      * Returns a string describing this DTMethod.  The string is
  144.      * formatted as the method access modifiers, if any, followed by
  145.      * the method return type, followed by a space, followed by the
  146.      * class declaring the method, followed by a period, followed by
  147.      * the method name, followed by a parenthesized, comma-separated
  148.      * list of the method's formal parameter types. If the method
  149.      * throws checked exceptions, the parameter list is followed by a
  150.      * space, followed by the word throws followed by a
  151.      * comma-separated list of the thrown exception types.
  152.      * For example:
  153.      * <pre>
  154.      *    public boolean java.lang.Object.equals(java.lang.Object)
  155.      * </pre>
  156.      *
  157.      * <p>The access modifiers are placed in canonical order as
  158.      * specified by "The Java Language Specification".  This is
  159.      * <tt>public</tt>, <tt>protected</tt> or <tt>private</tt> first,
  160.      * and then other modifiers in the following order:
  161.      * <tt>abstract</tt>, <tt>static</tt>, <tt>final</tt>,
  162.      * <tt>synchronized</tt> <tt>native</tt>.
  163.      */
  164.     public String toString() {
  165.         try {
  166.             StringBuffer sb = new StringBuffer();
  167.             int mod = getModifiers();
  168.             if (mod != 0) {
  169.                 sb.append(Modifier.toString(mod));
  170.                 sb.append(" ");
  171.             }
  172.             sb.append(returns);
  173.             sb.append(" ");
  174.             sb.append(getDeclaringClass().getName());
  175.             sb.append(".");
  176.             sb.append(getName());
  177.             sb.append("(");
  178.             sb.append(parameters);
  179.             sb.append(")");
  180.             if (exceptions.length() > 0) {
  181.                 sb.append(" throws ");
  182.                 sb.append(exceptions);
  183.             }
  184.             return sb.toString();
  185.         } catch (Exception e) {
  186.             return "<" + e + ">";
  187.         }
  188.     }
  189.  
  190.     /**
  191.      * Returns a short string describing this DTMethod.
  192.      * The string consists of the method name followed by the
  193.      * parameter types in parenthesis, e.g., "substring(int,int)".
  194.      */
  195.     public String toShortString() {
  196.         try {
  197.             StringBuffer sb = new StringBuffer();
  198.             sb.append(name + "(");
  199.             sb.append(parameters);
  200.             sb.append(")");
  201.             return sb.toString();
  202.         } catch (Exception e) {
  203.             return "<" + e + ">";
  204.         }
  205.     }
  206.  
  207.     /**
  208.      * Finish serializing in.
  209.      */
  210.     private void readObject(java.io.ObjectInputStream in)
  211.     throws java.io.IOException, ClassNotFoundException {
  212.         in.defaultReadObject();
  213.         validate();
  214.     }
  215.  
  216.     public boolean validate() {
  217.         DTMethod method = clazz.getDeclaredMethod(name, getParameterTypes());
  218.         return valid = equals(method);
  219.     }
  220. }