home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgLangD.iso / VCAFE.3.0A / Main.bin / DTConstructor.java < prev    next >
Text File  |  1998-10-25  |  5KB  |  167 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. public class DTConstructor extends DTMember implements java.io.Serializable
  11. {
  12.     static final long serialVersionUID = -5975450004844899437L;
  13.  
  14.     transient boolean    valid;            // set by validate
  15.  
  16.     /**
  17.      * Constructor.  Only the dtreflect package may construct a DTConstructor.
  18.      */
  19.     DTConstructor(DTClass clazz, String name, int modifiers, String parameters, String parameterNames, String exceptions) {
  20.         super(clazz,name,modifiers,parameters,parameterNames,"",exceptions);
  21.     }
  22.  
  23.     /**
  24.      * Constructor for serialization.
  25.      */
  26.     DTConstructor() {}
  27.  
  28.     /**
  29.      * gets the text range from the beginning of the constructor declaration to the closing brace
  30.      */
  31.     public Range getSourceRange() {
  32.         return clazz.getSourceRange(this);
  33.     }
  34.  
  35.     /**
  36.      * gets the text range from the beginning of the first line of the constructor's Javadoc comment
  37.      * to the closing asterisk-slash characters
  38.      */
  39.     public Range getJavadocRange() {
  40.         return clazz.getJavadocRange(this);
  41.     }
  42.  
  43.     /**
  44.      * Compares this DTConstructor against the specified object.  Returns
  45.      * true if the objects are the same.  Two constructors are the same if
  46.      * they were declared by the same class and have the same name
  47.      * and formal parameter types.
  48.      */
  49.     public boolean equals(Object obj) {
  50.         if (obj instanceof DTConstructor) {
  51.             DTConstructor other = (DTConstructor)obj;
  52.             return this.equals(other);
  53.         }
  54.         return false;
  55.     }
  56.  
  57.     /**
  58.      * Compares this DTConstructor against the specified DTConstructor.  Returns
  59.      * true if the constructors are the same.  Two Constructors are the same if
  60.      * they were declared by the same class and have the same name
  61.      * and formal parameter types.
  62.      */
  63.     public boolean equals(DTConstructor other) {
  64.         if ((other != null)
  65.             && (clazz.equals(other.clazz))
  66.             && (name.equals(other.name))
  67.             && (parameters.equals(other.parameters))
  68.             && (exceptions.equals(other.exceptions))
  69.             && (modifiers == other.modifiers))
  70.                 return true;
  71.  
  72.         return false;
  73.     }
  74.  
  75.     /**
  76.      * Compares the formal signature of this DTConstructor with another.
  77.      * Returns true if the unqualified method name and number and
  78.      * types of arguments are the same.
  79.     public boolean equalSignatures(DTConstructor other) {
  80.         if (other != null
  81.         && getName().equals(other.getName())
  82.         && getParameters().equals(other.getParameters()))
  83.             return true;
  84.         return false;
  85.     }
  86.  
  87.     /**
  88.      * Returns a string describing this DTConstructor.  The string is
  89.      * formatted as the constructor access modifiers, if any,
  90.      * followed by a space, followed by the
  91.      * class declaring the constructor, followed by a period, followed by
  92.      * the constructor name, followed by a parenthesized, comma-separated
  93.      * list of the constructor's formal parameter types. If the constructor
  94.      * throws checked exceptions, the parameter list is followed by a
  95.      * space, followed by the word throws followed by a
  96.      * comma-separated list of the thrown exception types.
  97.      * For example:
  98.      * <pre>
  99.      *    public boolean java.lang.Object.equals(java.lang.Object)
  100.      * </pre>
  101.      *
  102.      * <p>The access modifiers are placed in canonical order as
  103.      * specified by "The Java Language Specification".  This is
  104.      * <tt>public</tt>, <tt>protected</tt> or <tt>private</tt> first,
  105.      * and then other modifiers in the following order:
  106.      * <tt>abstract</tt>, <tt>static</tt>, <tt>final</tt>,
  107.      * <tt>synchronized</tt> <tt>native</tt>.
  108.      */
  109.     public String toString() {
  110.         try {
  111.             StringBuffer sb = new StringBuffer();
  112.             int mod = getModifiers();
  113.             if (mod != 0) {
  114.                 sb.append(Modifier.toString(mod));
  115.                 sb.append(" ");
  116.             }
  117.             sb.append(returns);
  118.             sb.append(" ");
  119.             sb.append(getDeclaringClass().getName());
  120.             sb.append(".");
  121.             sb.append(getName());
  122.             sb.append("(");
  123.             sb.append(parameters);
  124.             sb.append(")");
  125.             if (exceptions.length() > 0) {
  126.                 sb.append(" throws ");
  127.                 sb.append(exceptions);
  128.             }
  129.             return sb.toString();
  130.         } catch (Exception e) {
  131.             return "<" + e + ">";
  132.         }
  133.     }
  134.  
  135.     /**
  136.      * Returns a short string describing this DTConstructor.
  137.      * The string consists of the constructor name followed by the
  138.      * parameter types in parenthesis, e.g., "substring(int,int)".
  139.      */
  140.     public String toShortString() {
  141.         try {
  142.             StringBuffer sb = new StringBuffer();
  143.             sb.append(name + "(");
  144.             sb.append(parameters);
  145.             sb.append(")");
  146.             return sb.toString();
  147.         } catch (Exception e) {
  148.             return "<" + e + ">";
  149.         }
  150.     }
  151.  
  152.     /**
  153.      * Finish serializing in.
  154.      */
  155.     private void readObject(java.io.ObjectInputStream in)
  156.     throws java.io.IOException, ClassNotFoundException {
  157.         in.defaultReadObject();
  158.         validate();
  159.     }
  160.  
  161.     public boolean validate() {
  162.         DTConstructor cons = clazz.getDeclaredConstructor(getParameterTypes());
  163.         return valid = equals(cons);
  164.     }
  165.  
  166. }
  167.