/[classpath]/classpath/gnu/java/lang/ClassHelper.java
ViewVC logotype

Diff of /classpath/gnu/java/lang/ClassHelper.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.7 by mark, Tue Jan 22 22:26:57 2002 UTC revision 1.8 by ericb, Fri Feb 22 20:07:40 2002 UTC
# Line 1  Line 1 
1  /* gnu.java.lang.ClassHelper  /* ClassHelper.java -- Utility methods to augment java.lang.Class
2     Copyright (C) 1998 Free Software Foundation, Inc.     Copyright (C) 1998, 2002 Free Software Foundation, Inc.
3    
4  This file is part of GNU Classpath.  This file is part of GNU Classpath.
5    
# Line 7  GNU Classpath is free software; you can Line 7  GNU Classpath is free software; you can
7  it under the terms of the GNU General Public License as published by  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2, or (at your option)  the Free Software Foundation; either version 2, or (at your option)
9  any later version.  any later version.
10    
11  GNU Classpath is distributed in the hope that it will be useful, but  GNU Classpath is distributed in the hope that it will be useful, but
12  WITHOUT ANY WARRANTY; without even the implied warranty of  WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Line 42  import java.util.*; Line 42  import java.util.*;
42  import java.lang.reflect.*;  import java.lang.reflect.*;
43    
44  /**  /**
45   ** ClassHelper has various methods that ought to have been   * ClassHelper has various methods that ought to have been in Class.
46   ** in class.   *
47   **   * @author John Keiser
48   ** @author John Keiser   * @author Eric Blake <ebb9@email.byu.edu>
49   ** @version 1.1.0, 29 Jul 1998   */
50   **/  public class ClassHelper
51    {
52  public class ClassHelper {    /**
53          /** Strip the package part from the class name.     * Strip the package part from the class name.
54           ** @param clazz the class to get the truncated name from     *
55           ** @return the truncated class name.     * @param clazz the class to get the truncated name from
56           **/     * @return the truncated class name
57          public static String getTruncatedClassName(Class clazz) {     */
58                  return getTruncatedName(clazz.getName());    public static String getTruncatedClassName(Class clazz)
59          }    {
60          /** Strip the package part from the class name, or the      return getTruncatedName(clazz.getName());
61           ** class part from the method or field name.    }
62           ** @param name the name to truncate.  
63           ** @return the truncated name.    /**
64           **/     * Strip the package part from the class name, or the class part from
65          public static String getTruncatedName(String name) {     * the method or field name.
66                  int lastInd = name.lastIndexOf('.');     *
67                  if(lastInd == -1) {     * @param name the name to truncate
68                          return name;     * @return the truncated name
69                  } else {     */
70                          return name.substring(lastInd+1);    public static String getTruncatedName(String name)
71                  }    {
72          }      int lastInd = name.lastIndexOf('.');
73        if (lastInd == -1)
74          /** Strip the last portion of the name (after the last        return name;
75           ** dot).      return name.substring(lastInd + 1);
76           ** @param name the name to get package of.    }
77           ** @return the package name.  "" if no package.  
78           **/    /**
79          public static String getPackagePortion(String name) {     * Strip the last portion of the name (after the last dot).
80                  int lastInd = name.lastIndexOf('.');     *
81                  if(lastInd == -1) {     * @param name the name to get package of
82                          return "";     * @return the package name, or "" if no package
83                  } else {     */
84                          return name.substring(0,lastInd);    public static String getPackagePortion(String name)
85                  }    {
86          }      int lastInd = name.lastIndexOf('.');
87        if (lastInd == -1)
88          static Hashtable allMethods = new Hashtable();        return "";
89          static Hashtable allMethodsAtDeclaration = new Hashtable();      return name.substring(0, lastInd);
90      }
91          /** Get all the methods, public, private and  
92           ** otherwise, from the class, getting them    /** Cache of methods found in getAllMethods(). */
93           ** from the most recent class to find them.    static Hashtable allMethods = new Hashtable();
94           **/  
95          public static Method[] getAllMethods(Class clazz) {    /** Cache of methods found in getAllMethodsAtDeclaration(). */
96                  Method[] retval = (Method[])allMethods.get(clazz);    static Hashtable allMethodsAtDeclaration = new Hashtable();
97                  if(retval == null) {  
98                          Method[] superMethods;    /**
99                          if(clazz.getSuperclass() != null) {     * Get all the methods, public, private and otherwise, from the class,
100                                  superMethods = getAllMethods(clazz.getSuperclass());     * getting them from the most recent class to find them.
101                          } else {     *
102                                  superMethods = new Method[0];     * @param clazz the class to start at
103                          }     * @return all methods declared or inherited in clazz
104                          Vector v = new Vector();     */
105                          Method[] currentMethods = clazz.getDeclaredMethods();    public static Method[] getAllMethods(Class clazz)
106                          for(int i=0;i<currentMethods.length;i++) {    {
107                                  v.addElement(currentMethods[i]);      Method[] retval = (Method[]) allMethods.get(clazz);
108                          }      if (retval == null)
109                          for(int i=0;i<superMethods.length;i++) {        {
110                                  boolean addOK = true;          Method[] superMethods;
111                                  for(int j=0;j<currentMethods.length;j++) {          if (clazz.getSuperclass() != null)
112                                          if(getTruncatedName(superMethods[i].getName()).equals(getTruncatedName(currentMethods[j].getName()))              superMethods = getAllMethods(clazz.getSuperclass());
113                                             && ArrayHelper.equalsArray(superMethods[i].getParameterTypes(),currentMethods[j].getParameterTypes())) {          else
114                                                  addOK = false;              superMethods = new Method[0];
115                                          }          Vector v = new Vector();
116                                  }          Method[] currentMethods = clazz.getDeclaredMethods();
117                                  if(addOK) {          for (int i = 0; i < currentMethods.length; i++)
118                                          v.addElement(superMethods[i]);            v.addElement(currentMethods[i]);
119                                  }          for (int i = 0; i < superMethods.length; i++)
120                          }            {
121                boolean addOK = true;
122                          retval = new Method[v.size()];              for (int j = 0; j < currentMethods.length; j++)
123                          v.copyInto(retval);                {
124                          allMethods.put(clazz,retval);                  if (getTruncatedName(superMethods[i].getName())
125                  }                      .equals(getTruncatedName(currentMethods[j].getName()))
126                  return retval;                      && ArrayHelper.equalsArray(superMethods[i].getParameterTypes(),
127          }                                                 currentMethods[j].getParameterTypes()))
128                      {
129          /** Get all the methods, public, private and                      addOK = false;
130           ** otherwise, from the class, and get them from                    }
131           ** their point of declaration.                }
132           **/              if(addOK)
133          public static Method[] getAllMethodsAtDeclaration(Class clazz) {                v.addElement(superMethods[i]);
134                  Method[] retval = (Method[])allMethodsAtDeclaration.get(clazz);            }
135                  if(retval == null) {          retval = new Method[v.size()];
136                          Method[] superMethods;          v.copyInto(retval);
137                          if(clazz.getSuperclass() != null) {          allMethods.put(clazz,retval);
138                                  superMethods = getAllMethodsAtDeclaration(clazz.getSuperclass());        }
139                          } else {      return retval;
140                                  superMethods = new Method[0];    }
141                          }  
142                          Vector v = new Vector();    /**
143                          Method[] currentMethods = clazz.getDeclaredMethods();     * Get all the methods, public, private and otherwise, from the class,
144                          for(int i=0;i<superMethods.length;i++) {     * and get them from their point of declaration.
145                                  v.addElement(superMethods[i]);     *
146                          }     * @param clazz the class to start at
147                          for(int i=0;i<superMethods.length;i++) {     * @return all methods declared or inherited in clazz
148                                  boolean addOK = true;     */
149                                  for(int j=0;j<currentMethods.length;j++) {    public static Method[] getAllMethodsAtDeclaration(Class clazz)
150                                          if(getTruncatedName(superMethods[i].getName()).equals(getTruncatedName(currentMethods[j].getName()))    {
151                                             && ArrayHelper.equalsArray(superMethods[i].getParameterTypes(),currentMethods[j].getParameterTypes())) {      Method[] retval = (Method[]) allMethodsAtDeclaration.get(clazz);
152                                                  addOK = false;      if (retval == null)
153                                          }        {
154                                  }          Method[] superMethods;
155                                  if(addOK) {          if (clazz.getSuperclass() != null)
156                                          v.addElement(superMethods[i]);            superMethods = getAllMethodsAtDeclaration(clazz.getSuperclass());
157                                  }          else
158                          }            superMethods = new Method[0];
159            Vector v = new Vector();
160                          retval = new Method[v.size()];          Method[] currentMethods = clazz.getDeclaredMethods();
161                          v.copyInto(retval);          for (int i = 0; i < superMethods.length; i++)
162                          allMethodsAtDeclaration.put(clazz,retval);            v.addElement(superMethods[i]);
163                  }          for (int i = 0; i < superMethods.length; i++)
164                  return retval;            {
165          }              boolean addOK = true;
166                for (int j = 0; j < currentMethods.length; j++)
167          static Hashtable allFields = new Hashtable();                {
168          static Hashtable allFieldsAtDeclaration = new Hashtable();                  if (getTruncatedName(superMethods[i].getName())
169                        .equals(getTruncatedName(currentMethods[j].getName()))
170          /** Get all the fields, public, private and                      && ArrayHelper.equalsArray(superMethods[i].getParameterTypes(),
171           ** otherwise, from the class, getting them                                                 currentMethods[j].getParameterTypes()))
172           ** from the most recent class to find them.                    {
173           **/                      addOK = false;
174          public static Field[] getAllFields(Class clazz) {                    }
175                  Field[] retval = (Field[])allFields.get(clazz);                }
176                  if(retval == null) {              if(addOK)
177                          Field[] superFields;                v.addElement(superMethods[i]);
178                          if(clazz.getSuperclass() != null) {            }
179                                  superFields = getAllFields(clazz.getSuperclass());          retval = new Method[v.size()];
180                          } else {          v.copyInto(retval);
181                                  superFields = new Field[0];          allMethodsAtDeclaration.put(clazz,retval);
182                          }        }
183                          Vector v = new Vector();      return retval;
184                          Field[] currentFields = clazz.getDeclaredFields();    }
185                          for(int i=0;i<currentFields.length;i++) {  
186                                  v.addElement(currentFields[i]);    /** Cache of fields found in getAllFields(). */
187                          }    static Hashtable allFields = new Hashtable();
188                          for(int i=0;i<superFields.length;i++) {  
189                                  boolean addOK = true;    /** Cache of fields found in getAllFieldsAtDeclaration(). */
190                                  for(int j=0;j<currentFields.length;j++) {    static Hashtable allFieldsAtDeclaration = new Hashtable();
191                                          if(getTruncatedName(superFields[i].getName()).equals(getTruncatedName(currentFields[j].getName()))) {  
192                                                  addOK = false;    /**
193                                          }     * Get all the fields, public, private and otherwise, from the class,
194                                  }     * getting them from the most recent class to find them.
195                                  if(addOK) {     *
196                                          v.addElement(superFields[i]);     * @param clazz the class to start at
197                                  }     * @return all fields declared or inherited in clazz
198                          }     */
199      public static Field[] getAllFields(Class clazz)
200                          retval = new Field[v.size()];    {
201                          v.copyInto(retval);      Field[] retval = (Field[]) allFields.get(clazz);
202                          allFields.put(clazz,retval);      if (retval == null)
203                  }        {
204                  return retval;          Field[] superFields;
205          }          if (clazz.getSuperclass() != null)
206              superFields = getAllFields(clazz.getSuperclass());
207          /** Get all the fields, public, private and          else
208           ** otherwise, from the class, and get them from            superFields = new Field[0];
209           ** their point of declaration.          Vector v = new Vector();
210           **/          Field[] currentFields = clazz.getDeclaredFields();
211          public static Field[] getAllFieldsAtDeclaration(Class clazz) {          for (int i = 0; i < currentFields.length; i++)
212                  Field[] retval = (Field[])allFieldsAtDeclaration.get(clazz);            v.addElement(currentFields[i]);
213                  if(retval == null) {          for (int i = 0; i < superFields.length; i++)
214                          Field[] superFields;            {
215                          if(clazz.getSuperclass() != null) {              boolean addOK = true;
216                                  superFields = getAllFieldsAtDeclaration(clazz.getSuperclass());              for (int j = 0; j < currentFields.length; j++)
217                          } else {                {
218                                  superFields = new Field[0];                  if (getTruncatedName(superFields[i].getName())
219                          }                      .equals(getTruncatedName(currentFields[j].getName())))
220                          Vector v = new Vector();                    {
221                          Field[] currentFields = clazz.getDeclaredFields();                      addOK = false;
222                          for(int i=0;i<superFields.length;i++) {                    }
223                                  v.addElement(superFields[i]);                }
224                          }              if (addOK)
225                          for(int i=0;i<superFields.length;i++) {                v.addElement(superFields[i]);
226                                  boolean addOK = true;            }
227                                  for(int j=0;j<currentFields.length;j++) {          retval = new Field[v.size()];
228                                          if(getTruncatedName(superFields[i].getName()).equals(getTruncatedName(currentFields[j].getName()))) {          v.copyInto(retval);
229                                                  addOK = false;          allFields.put(clazz,retval);
230                                          }        }
231                                  }      return retval;
232                                  if(addOK) {    }
233                                          v.addElement(superFields[i]);  
234                                  }    /**
235                          }     * Get all the fields, public, private and otherwise, from the class,
236       * and get them from their point of declaration.
237                          retval = new Field[v.size()];     *
238                          v.copyInto(retval);     * @param clazz the class to start at
239                          allFieldsAtDeclaration.put(clazz,retval);     * @return all fields declared or inherited in clazz
240                  }     */
241                  return retval;    public static Field[] getAllFieldsAtDeclaration(Class clazz)
242          }    {
243        Field[] retval = (Field[]) allFieldsAtDeclaration.get(clazz);
244        if (retval == null)
245          {
246            Field[] superFields;
247            if (clazz.getSuperclass() != null)
248              superFields = getAllFieldsAtDeclaration(clazz.getSuperclass());
249            else
250              superFields = new Field[0];
251            Vector v = new Vector();
252            Field[] currentFields = clazz.getDeclaredFields();
253            for (int i = 0; i < superFields.length; i++)
254              v.addElement(superFields[i]);
255            for (int i = 0; i < superFields.length; i++)
256              {
257                boolean addOK = true;
258                for (int j = 0; j < currentFields.length; j++)
259                  {
260                    if (getTruncatedName(superFields[i].getName())
261                        .equals(getTruncatedName(currentFields[j].getName())))
262                      {
263                        addOK = false;
264                      }
265                  }
266                if(addOK)
267                  v.addElement(superFields[i]);
268              }
269            retval = new Field[v.size()];
270            v.copyInto(retval);
271            allFieldsAtDeclaration.put(clazz,retval);
272          }
273        return retval;
274      }
275  }  }

Legend:
Removed from v.1.7  
changed lines
  Added in v.1.8

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26