Dead
[official-gcc.git] / gomp-20050608-branch / libjava / classpath / gnu / java / lang / ClassHelper.java
blob14c8a39c478ef809b29e77fb3b6e8da8ad06a198
1 /* ClassHelper.java -- Utility methods to augment java.lang.Class
2 Copyright (C) 1998, 2002 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 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)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package gnu.java.lang;
41 import java.lang.reflect.Field;
42 import java.lang.reflect.Method;
43 import java.util.Arrays;
44 import java.util.HashMap;
45 import java.util.HashSet;
46 import java.util.Iterator;
47 import java.util.Map;
48 import java.util.Set;
50 /**
51 * ClassHelper has various methods that ought to have been in Class.
53 * @author John Keiser
54 * @author Eric Blake (ebb9@email.byu.edu)
56 public class ClassHelper
58 /**
59 * Strip the package part from the class name.
61 * @param clazz the class to get the truncated name from
62 * @return the truncated class name
64 public static String getTruncatedClassName(Class clazz)
66 return getTruncatedName(clazz.getName());
69 /**
70 * Strip the package part from the class name, or the class part from
71 * the method or field name.
73 * @param name the name to truncate
74 * @return the truncated name
76 public static String getTruncatedName(String name)
78 int lastInd = name.lastIndexOf('.');
79 if (lastInd == -1)
80 return name;
81 return name.substring(lastInd + 1);
84 /** Cache of methods found in getAllMethods(). */
85 private static Map allMethods = new HashMap();
87 /**
88 * Get all the methods, public, private and otherwise, from the class,
89 * getting them from the most recent class to find them. This may not
90 * be quite the correct approach, as this includes methods that are not
91 * inherited or accessible from clazz, so beware.
93 * @param clazz the class to start at
94 * @return all methods declared or inherited in clazz
96 public static Method[] getAllMethods(Class clazz)
98 Method[] retval = (Method[]) allMethods.get(clazz);
99 if (retval == null)
101 Set methods = new HashSet();
102 Class c = clazz;
103 while (c != null)
105 Method[] currentMethods = c.getDeclaredMethods();
106 loop:
107 for (int i = 0; i < currentMethods.length; i++)
109 Method current = currentMethods[i];
110 int size = methods.size();
111 Iterator iter = methods.iterator();
112 while (--size >= 0)
114 Method override = (Method) iter.next();
115 if (current.getName().equals(override.getName())
116 && Arrays.equals(current.getParameterTypes(),
117 override.getParameterTypes())
118 && current.getReturnType() == override.getReturnType())
119 continue loop;
121 methods.add(current);
123 c = c.getSuperclass();
125 retval = new Method[methods.size()];
126 methods.toArray(retval);
127 allMethods.put(clazz, retval);
129 return retval;
132 /** Cache of fields found in getAllFields(). */
133 private static Map allFields = new HashMap();
136 * Get all the fields, public, private and otherwise, from the class,
137 * getting them from the most recent class to find them. This may not
138 * be quite the correct approach, as this includes fields that are not
139 * inherited or accessible from clazz, so beware.
141 * @param clazz the class to start at
142 * @return all fields declared or inherited in clazz
144 public static Field[] getAllFields(Class clazz)
146 Field[] retval = (Field[]) allFields.get(clazz);
147 if (retval == null)
149 Set fields = new HashSet();
150 Class c = clazz;
151 while (c != null)
153 Field[] currentFields = c.getDeclaredFields();
154 loop:
155 for (int i = 0; i < currentFields.length; i++)
157 Field current = currentFields[i];
158 int size = fields.size();
159 Iterator iter = fields.iterator();
160 while (--size >= 0)
162 Field override = (Field) iter.next();
163 if (current.getName().equals(override.getName())
164 && current.getType() == override.getType())
165 continue loop;
167 fields.add(current);
169 c = c.getSuperclass();
171 retval = new Field[fields.size()];
172 fields.toArray(retval);
173 allFields.put(clazz, retval);
175 return retval;