home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgLangD.iso / VCAFE.3.0A / Main.bin / DTMember.java < prev    next >
Text File  |  1998-10-25  |  8KB  |  263 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.util.Vector;
  9. import java.io.*;
  10.  
  11. public abstract class DTMember implements java.io.Serializable
  12. {
  13.     static final long serialVersionUID = -4216301143967605388L;
  14.  
  15.     protected DTClass    clazz;            // declaring class
  16.     protected String    name;            // method name
  17.     protected int        modifiers;        // flags
  18.     protected String    parameters;        // expressed as "[typename1[,typename2,...]]"
  19.     protected String    parameterNames;    // expressed as "[name1[,name2,...]]"
  20.     protected String    returns;        // expressed as "typename"
  21.     protected String    exceptions;        // expressed as "[typename1[,typename2,...]]"
  22.  
  23.     /**
  24.      * Constructor.  Only the dtreflect package may construct a DTMember.
  25.      */
  26.     DTMember(DTClass clazz, String name, int modifiers, String parameters, String parameterNames, String returns, String exceptions) {
  27.         this.clazz = clazz;
  28.         this.name = name;
  29.         this.modifiers = modifiers;
  30.         this.parameters = parameters;
  31.         this.parameterNames = parameterNames;
  32.         this.returns = returns;
  33.         this.exceptions = exceptions;
  34.     }
  35.  
  36.     /**
  37.      * Constructor for serialization.
  38.      */
  39.     DTMember() {
  40.     }
  41.  
  42.     /**
  43.      * Identifies the set of all public members of a class or interface,
  44.      * including inherited members.
  45.      */
  46.     public static final int PUBLIC = 0;
  47.  
  48.     /**
  49.      * Identifies the set of declared members of a class or interface.
  50.      * Inherited members are not included.
  51.      */
  52.     public static final int DECLARED = 1;
  53.  
  54.     /**
  55.      * Returns the Class object representing the class or interface
  56.      * that declares the member or constructor represented by this Member.
  57.      */
  58.     public DTClass getDeclaringClass() {
  59.         return clazz;
  60.     }
  61.  
  62.     /**
  63.      * Returns the simple name of the underlying member or constructor
  64.      * represented by this Member.
  65.      */
  66.     public String getName() {
  67.         return name;
  68.     }
  69.  
  70.     /**
  71.      * Returns the Java language modifiers for the member or
  72.      * constructor represented by this Member, as an integer.  The
  73.      * Modifier class should be used to decode the modifiers in
  74.      * the integer.
  75.      * @see Modifier
  76.      */
  77.     public int getModifiers() {
  78.         return modifiers;
  79.     }
  80.  
  81.     /**
  82.      * Returns the parameters of the method represented by this DTMember
  83.      * object, as a String, e.g., "java.lang.String,int".
  84.      * For fields, getParameters() returns an empty string.
  85.      */
  86.     public String getParameters() {
  87.         return parameters;
  88.     }
  89.  
  90.     /**
  91.      * Returns the number of parameters of the method represented by this DTMember.
  92.      */
  93.     public int getNumParameters() {
  94.         if (parameters.equals(""))
  95.             return 0;
  96.         int num = 1, pos = 0;
  97.         while ((pos = parameters.indexOf(',', pos)) != -1)
  98.         {
  99.             num++;
  100.             pos++;
  101.         }
  102.         return num;
  103.     }
  104.  
  105.     /**
  106.      * Returns an array of DTClass objects that represent the formal
  107.      * parameter types, in declaration order, of the method/constructor
  108.      * represented by this DTMember object.  Returns an array of length
  109.      * 0 if the underlying method takes no parameters or for a field.
  110.      *
  111.      * <p>Returns null if a class could not be created.
  112.      */
  113.     public DTClass[] getParameterTypes() {
  114.         return parametersToClassArray(parameters);
  115.     }
  116.  
  117.     /**
  118.      * Returns an array of String objects that represent the formal
  119.      * parameter type names, in declaration order, of the method/constructor
  120.      * represented by this DTMember object.  Returns an array of length
  121.      * 0 if the underlying method takes no parameters,or for a field.
  122.      *
  123.      * <p>Returns null if a class could not be created.
  124.      */
  125.     public String[] getParameterTypeNames() {
  126.         return parametersToStringArray(parameters);
  127.     }
  128.  
  129.     /**
  130.      * Returns the parameter names of the method represented by this DTMember
  131.      * object, as a String, e.g., "c,i".
  132.      * For fields, getParameters() returns an empty string.
  133.      */
  134.     public String getParameterNames() {
  135.         return parameterNames;
  136.     }
  137.  
  138.     /**
  139.      * Returns the return type as a String, e.g., "java.lang.String".
  140.      * For constructors, the type is "void". For fields, this is
  141.      * the field type.
  142.      */
  143.     public String getReturns() {
  144.         return returns;
  145.     }
  146.  
  147.     /**
  148.      * Returns a Class object that identifies the declared type for
  149.      * the field or the return type of the method. Constructor type
  150.      * is always DTClass.VoidType.
  151.      */
  152.     public DTClass getType() {
  153.         return returns == "" ? DTClass.VoidType : DTClass.findClass(clazz.getProject(), returns);
  154.     }
  155.  
  156.     /**
  157.      * Returns the exceptions of the method represented by this DTMethod
  158.      * object, as a String, e.g., "java.lang.NullPointerException,java.lang.NumberFormatException".
  159.      * The exceptions appear in alphabetical order.
  160.      */
  161.     public String getExceptions() {
  162.         return exceptions;
  163.     }
  164.  
  165.     /**
  166.      * Returns an array of DTClass objects that represent the types of
  167.      * the checked exceptions thrown by the underlying method
  168.      * represented by this DTMethod object.  Returns an array of length
  169.      * 0 if the method throws no checked exceptions. Returns null
  170.      * in the corresponding array element if a class cannot be created.
  171.      * @see DTClass#nonNull
  172.      */
  173.     public DTClass[] getExceptionTypes() {
  174.         return parametersToClassArray(exceptions);
  175.     }
  176.  
  177.     /**
  178.      * Returns a hashcode for this DTMember.  The hashcode is computed
  179.      * as the exclusive-or of the hashcodes for the declaring class name
  180.      * and the member's name.
  181.      */
  182.     public int hashCode() {
  183.         return getDeclaringClass().getName().hashCode() ^ getName().hashCode();
  184.     }
  185.  
  186.     /**
  187.      * gets the text range from the beginning of the member declaration to the closing brace
  188.      * (or semicolon for native or abstract members)
  189.      */
  190.     public abstract Range getSourceRange();
  191.  
  192.     /**
  193.      * gets the text range from the beginning of the first line of the member's Javadoc comment
  194.      * to the closing asterisk-slash characters
  195.      */
  196.     public abstract Range getJavadocRange();
  197.  
  198.  
  199.     /**
  200.      * Ensure that this member is valid in its project
  201.      */
  202.     public abstract boolean validate();
  203.  
  204.     /**
  205.      * Return true if one expression is equivalent to another.
  206.      * <p>Argument values are not taken into account in the comparision,
  207.      * just the "value" of the expression.
  208.      */
  209.     public boolean equals(Object obj) {
  210.         if (obj != null && getClass() == obj.getClass()) {
  211.             DTMember other = (DTMember) obj;
  212.             return clazz.equals(other.clazz) && name.equals(other.name) && modifiers == other.modifiers
  213.                 && (parameters == null ? other.parameters == null : parameters.equals(other.parameters));
  214.         }
  215.         return false;
  216.     }
  217.  
  218.     /**
  219.      * Utility method converts a comma-delimited string of
  220.      * type names into an array of DTClass. Like most
  221.      * other methods, does not throw if DTClass can't be
  222.      * created, just returns null in the corresponding
  223.      * array entry.
  224.      *
  225.      * @param    sig        string of types, e.g., "int,java.lang.String"
  226.      */
  227.     public DTClass[] parametersToClassArray(String sig) {
  228.         int start = 0, len = sig.length();
  229.         Vector v = new Vector();
  230.         while (start < len) {
  231.             int i = sig.indexOf(',', start);
  232.             if (i < 0)
  233.                 i = len;
  234.             v.addElement(DTClass.findClass(clazz.getProject(),sig.substring(start,i)));
  235.             start = i + 1;
  236.         }
  237.         DTClass[] ret = new DTClass[v.size()];
  238.         v.copyInto(ret);
  239.         return ret;
  240.     }
  241.  
  242.     /**
  243.      * Utility method converts a comma-delimited string of
  244.      * type names into an array of String.
  245.      *
  246.      * @param    sig        string of types, e.g., "int,java.lang.String"
  247.      */
  248.     public String[] parametersToStringArray(String sig) {
  249.         int start = 0, len = sig.length();
  250.         Vector v = new Vector();
  251.         while (start < len) {
  252.             int i = sig.indexOf(',', start);
  253.             if (i < 0)
  254.                 i = len;
  255.             v.addElement(sig.substring(start,i));
  256.             start = i + 1;
  257.         }
  258.         String[] ret = new String[v.size()];
  259.         v.copyInto(ret);
  260.         return ret;
  261.     }
  262. }
  263.