source: trunk/gcc/libjava/gnu/java/lang/ClassHelper.java

Last change on this file was 2, checked in by bird, 22 years ago

Initial revision

  • Property cvs2svn:cvs-rev set to 1.1
  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 7.5 KB
Line 
1/* gnu.java.lang.ClassHelper
2 Copyright (C) 1998 Free Software Foundation, Inc.
3
4This file is part of GNU Classpath.
5
6GNU Classpath is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2, or (at your option)
9any later version.
10
11GNU Classpath is distributed in the hope that it will be useful, but
12WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU Classpath; see the file COPYING. If not, write to the
18Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
1902111-1307 USA.
20
21Linking this library statically or dynamically with other modules is
22making a combined work based on this library. Thus, the terms and
23conditions of the GNU General Public License cover the whole
24combination.
25
26As a special exception, the copyright holders of this library give you
27permission to link this library with independent modules to produce an
28executable, regardless of the license terms of these independent
29modules, and to copy and distribute the resulting executable under
30terms of your choice, provided that you also meet, for each linked
31independent module, the terms and conditions of the license of that
32module. An independent module is a module which is not derived from
33or based on this library. If you modify this library, you may extend
34this exception to your version of the library, but you are not
35obligated to do so. If you do not wish to do so, delete this
36exception statement from your version. */
37
38
39package gnu.java.lang;
40
41import java.util.*;
42import java.lang.reflect.*;
43
44/**
45 ** ClassHelper has various methods that ought to have been
46 ** in class.
47 **
48 ** @author John Keiser
49 ** @version 1.1.0, 29 Jul 1998
50 **/
51
52public class ClassHelper {
53 /** 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.
56 **/
57 public static String getTruncatedClassName(Class clazz) {
58 return getTruncatedName(clazz.getName());
59 }
60 /** Strip the package part from the class name, or the
61 ** class part from the method or field name.
62 ** @param name the name to truncate.
63 ** @return the truncated name.
64 **/
65 public static String getTruncatedName(String name) {
66 int lastInd = name.lastIndexOf('.');
67 if(lastInd == -1) {
68 return name;
69 } else {
70 return name.substring(lastInd+1);
71 }
72 }
73
74 /** Strip the last portion of the name (after the last
75 ** dot).
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) {
80 int lastInd = name.lastIndexOf('.');
81 if(lastInd == -1) {
82 return "";
83 } else {
84 return name.substring(0,lastInd);
85 }
86 }
87
88 static Hashtable allMethods = new Hashtable();
89 static Hashtable allMethodsAtDeclaration = new Hashtable();
90
91 /** Get all the methods, public, private and
92 ** otherwise, from the class, getting them
93 ** from the most recent class to find them.
94 **/
95 public static Method[] getAllMethods(Class clazz) {
96 Method[] retval = (Method[])allMethods.get(clazz);
97 if(retval == null) {
98 Method[] superMethods;
99 if(clazz.getSuperclass() != null) {
100 superMethods = getAllMethods(clazz.getSuperclass());
101 } else {
102 superMethods = new Method[0];
103 }
104 Vector v = new Vector();
105 Method[] currentMethods = clazz.getDeclaredMethods();
106 for(int i=0;i<currentMethods.length;i++) {
107 v.addElement(currentMethods[i]);
108 }
109 for(int i=0;i<superMethods.length;i++) {
110 boolean addOK = true;
111 for(int j=0;j<currentMethods.length;j++) {
112 if(getTruncatedName(superMethods[i].getName()).equals(getTruncatedName(currentMethods[j].getName()))
113 && ArrayHelper.equalsArray(superMethods[i].getParameterTypes(),currentMethods[j].getParameterTypes())) {
114 addOK = false;
115 }
116 }
117 if(addOK) {
118 v.addElement(superMethods[i]);
119 }
120 }
121
122 retval = new Method[v.size()];
123 v.copyInto(retval);
124 allMethods.put(clazz,retval);
125 }
126 return retval;
127 }
128
129 /** Get all the methods, public, private and
130 ** otherwise, from the class, and get them from
131 ** their point of declaration.
132 **/
133 public static Method[] getAllMethodsAtDeclaration(Class clazz) {
134 Method[] retval = (Method[])allMethodsAtDeclaration.get(clazz);
135 if(retval == null) {
136 Method[] superMethods;
137 if(clazz.getSuperclass() != null) {
138 superMethods = getAllMethodsAtDeclaration(clazz.getSuperclass());
139 } else {
140 superMethods = new Method[0];
141 }
142 Vector v = new Vector();
143 Method[] currentMethods = clazz.getDeclaredMethods();
144 for(int i=0;i<superMethods.length;i++) {
145 v.addElement(superMethods[i]);
146 }
147 for(int i=0;i<superMethods.length;i++) {
148 boolean addOK = true;
149 for(int j=0;j<currentMethods.length;j++) {
150 if(getTruncatedName(superMethods[i].getName()).equals(getTruncatedName(currentMethods[j].getName()))
151 && ArrayHelper.equalsArray(superMethods[i].getParameterTypes(),currentMethods[j].getParameterTypes())) {
152 addOK = false;
153 }
154 }
155 if(addOK) {
156 v.addElement(superMethods[i]);
157 }
158 }
159
160 retval = new Method[v.size()];
161 v.copyInto(retval);
162 allMethodsAtDeclaration.put(clazz,retval);
163 }
164 return retval;
165 }
166
167 static Hashtable allFields = new Hashtable();
168 static Hashtable allFieldsAtDeclaration = new Hashtable();
169
170 /** Get all the fields, public, private and
171 ** otherwise, from the class, getting them
172 ** from the most recent class to find them.
173 **/
174 public static Field[] getAllFields(Class clazz) {
175 Field[] retval = (Field[])allFields.get(clazz);
176 if(retval == null) {
177 Field[] superFields;
178 if(clazz.getSuperclass() != null) {
179 superFields = getAllFields(clazz.getSuperclass());
180 } else {
181 superFields = new Field[0];
182 }
183 Vector v = new Vector();
184 Field[] currentFields = clazz.getDeclaredFields();
185 for(int i=0;i<currentFields.length;i++) {
186 v.addElement(currentFields[i]);
187 }
188 for(int i=0;i<superFields.length;i++) {
189 boolean addOK = true;
190 for(int j=0;j<currentFields.length;j++) {
191 if(getTruncatedName(superFields[i].getName()).equals(getTruncatedName(currentFields[j].getName()))) {
192 addOK = false;
193 }
194 }
195 if(addOK) {
196 v.addElement(superFields[i]);
197 }
198 }
199
200 retval = new Field[v.size()];
201 v.copyInto(retval);
202 allFields.put(clazz,retval);
203 }
204 return retval;
205 }
206
207 /** Get all the fields, public, private and
208 ** otherwise, from the class, and get them from
209 ** their point of declaration.
210 **/
211 public static Field[] getAllFieldsAtDeclaration(Class clazz) {
212 Field[] retval = (Field[])allFieldsAtDeclaration.get(clazz);
213 if(retval == null) {
214 Field[] superFields;
215 if(clazz.getSuperclass() != null) {
216 superFields = getAllFieldsAtDeclaration(clazz.getSuperclass());
217 } else {
218 superFields = new Field[0];
219 }
220 Vector v = new Vector();
221 Field[] currentFields = clazz.getDeclaredFields();
222 for(int i=0;i<superFields.length;i++) {
223 v.addElement(superFields[i]);
224 }
225 for(int i=0;i<superFields.length;i++) {
226 boolean addOK = true;
227 for(int j=0;j<currentFields.length;j++) {
228 if(getTruncatedName(superFields[i].getName()).equals(getTruncatedName(currentFields[j].getName()))) {
229 addOK = false;
230 }
231 }
232 if(addOK) {
233 v.addElement(superFields[i]);
234 }
235 }
236
237 retval = new Field[v.size()];
238 v.copyInto(retval);
239 allFieldsAtDeclaration.put(clazz,retval);
240 }
241 return retval;
242 }
243}
Note: See TracBrowser for help on using the repository browser.