home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgLangD.iso / VCAFE.3.0A / Main.bin / DTField.java < prev    next >
Text File  |  1998-10-25  |  4KB  |  146 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 DTField extends DTMember implements java.io.Serializable
  11. {
  12.     static final long serialVersionUID = -1442822286115441750L;
  13.  
  14.     transient boolean    valid;            // set by validate
  15.  
  16.     /**
  17.      * Constructor. Used only by DTClass native methods.
  18.      */
  19.     DTField(DTClass clazz, String name, int modifiers, String returns) {
  20.         super(clazz,name,modifiers,"","",returns,"");
  21.     }
  22.  
  23.     /**
  24.      * Constructor for serialization.
  25.      */
  26.     private DTField() {
  27.     }
  28.  
  29.     /**
  30.      * gets the text range from the beginning of the field declaration to the closing brace
  31.      */
  32.     public Range getSourceRange() {
  33.         return clazz.getSourceRange(this);
  34.     }
  35.  
  36.     /**
  37.      * gets the text range from the beginning of the first line of the field's Javadoc comment
  38.      * to the closing asterisk-slash characters
  39.      */
  40.     public Range getJavadocRange() {
  41.         return clazz.getJavadocRange(this);
  42.     }
  43.  
  44.  
  45.     /**
  46.      * Renames the field to new name
  47.      * @param newName: The new name of the field
  48.      * @return Success (true) or failure (false)...
  49.      */
  50.     public boolean rename(String newName) {
  51.         return clazz.rename(this, newName);
  52.     }
  53.  
  54.  
  55.     /**
  56.      * Compares this DTField against the specified object.  Returns
  57.      * true if the objects are the same.  Two Fields are the same if
  58.      * they were declared by the same class and have the same name,
  59.      * type and modifiers.
  60.      */
  61.     public boolean equals(Object obj) {
  62.         if (obj instanceof DTField) {
  63.             DTField other = (DTField)obj;
  64.             return this.equals(other);
  65.         }
  66.         return false;
  67.     }
  68.  
  69.     /**
  70.      * Compares this DTField against the specified DTField.  Returns
  71.      * true if the fields are the same.  Two Fields are the same if
  72.      * they were declared by the same class and have the same name,
  73.      * type and modifiers.
  74.      */
  75.     public boolean equals(DTField other) {
  76.         if ((other != null)
  77.             && (clazz.equals(other.clazz))
  78.             && (name.equals(other.name))
  79.             && (returns.equals(other.returns))
  80.             && (modifiers == other.modifiers))
  81.                 return true;
  82.         return false;
  83.     }
  84.  
  85.     /**
  86.      * Returns a string describing this DTField.  The string is
  87.      * formatted as the field access modifiers, if any, followed by
  88.      * the field type, followed by the
  89.      * class declaring the field, followed by a period, followed by
  90.      * the field name.
  91.      * For example:
  92.      * <pre>
  93.      *    private float java.lang.Float.value
  94.      * </pre>
  95.      *
  96.      * <p>The access modifiers are placed in canonical order as
  97.      * specified by "The Java Language Specification".  This is
  98.      * <tt>public</tt>, <tt>protected</tt> or <tt>private</tt> first,
  99.      * and then other modifiers in the following order:
  100.      * <tt>abstract</tt>, <tt>static</tt>, <tt>final</tt>,
  101.      * <tt>synchronized</tt> <tt>native</tt>.
  102.      */
  103.     public String toString() {
  104.         try {
  105.             StringBuffer sb = new StringBuffer();
  106.             int mod = getModifiers();
  107.             if (mod != 0) {
  108.                 sb.append(Modifier.toString(mod));
  109.                 sb.append(" ");
  110.             }
  111.             sb.append(returns);
  112.             sb.append(" ");
  113.             sb.append(getDeclaringClass().getName());
  114.             sb.append(".");
  115.             sb.append(getName());
  116.             return sb.toString();
  117.         } catch (Exception e) {
  118.             return "<" + e + ">";
  119.         }
  120.     }
  121.  
  122.     /**
  123.      * Returns a short string describing this DTField.
  124.      * The string consists of the field name,
  125.      * e.g., "substring(int,int)".
  126.      */
  127.     public String toShortString() {
  128.         return getName();
  129.     }
  130.  
  131.     /**
  132.      * Finish serializing in.
  133.      */
  134.     private void readObject(java.io.ObjectInputStream in)
  135.     throws java.io.IOException, ClassNotFoundException {
  136.         in.defaultReadObject();
  137.         validate();
  138.     }
  139.  
  140.     public boolean validate() {
  141.         DTField field = clazz.getDeclaredField(getName());
  142.         if (field == null || !field.getReturns().equals(getReturns()))
  143.             return valid = false;
  144.         return valid = true;
  145.     }
  146. }