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)
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
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
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;
51 * ClassHelper has various methods that ought to have been in Class.
54 * @author Eric Blake (ebb9@email.byu.edu)
56 public class ClassHelper
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());
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('.');
81 return name.substring(lastInd + 1);
85 * Return the name of the class as written by the user.
86 * This is used by the various reflection toString methods.
87 * It differs from {@link Class#getName()} in that it prints
88 * arrays with trailing "[]"s. Note that it does not treat
89 * member classes specially, so a dollar sign may still appear
90 * in the result. This is intentional.
91 * @param klass the class
92 * @return a pretty form of the class' name
94 public static String getUserName(Class klass)
97 while (klass.isArray())
100 klass = klass.getComponentType();
102 String name = klass.getName();
105 CPStringBuilder b = new CPStringBuilder(name.length() + 2 * arrayCount);
107 for (int i = 0; i < arrayCount; ++i)
112 /** Cache of methods found in getAllMethods(). */
113 private static Map allMethods = new HashMap();
116 * Get all the methods, public, private and otherwise, from the class,
117 * getting them from the most recent class to find them. This may not
118 * be quite the correct approach, as this includes methods that are not
119 * inherited or accessible from clazz, so beware.
121 * @param clazz the class to start at
122 * @return all methods declared or inherited in clazz
124 public static Method[] getAllMethods(Class clazz)
126 Method[] retval = (Method[]) allMethods.get(clazz);
129 Set methods = new HashSet();
133 Method[] currentMethods = c.getDeclaredMethods();
135 for (int i = 0; i < currentMethods.length; i++)
137 Method current = currentMethods[i];
138 int size = methods.size();
139 Iterator iter = methods.iterator();
142 Method override = (Method) iter.next();
143 if (current.getName().equals(override.getName())
144 && Arrays.equals(current.getParameterTypes(),
145 override.getParameterTypes())
146 && current.getReturnType() == override.getReturnType())
149 methods.add(current);
151 c = c.getSuperclass();
153 retval = new Method[methods.size()];
154 methods.toArray(retval);
155 allMethods.put(clazz, retval);
160 /** Cache of fields found in getAllFields(). */
161 private static Map allFields = new HashMap();
164 * Get all the fields, public, private and otherwise, from the class,
165 * getting them from the most recent class to find them. This may not
166 * be quite the correct approach, as this includes fields that are not
167 * inherited or accessible from clazz, so beware.
169 * @param clazz the class to start at
170 * @return all fields declared or inherited in clazz
172 public static Field[] getAllFields(Class clazz)
174 Field[] retval = (Field[]) allFields.get(clazz);
177 Set fields = new HashSet();
181 Field[] currentFields = c.getDeclaredFields();
183 for (int i = 0; i < currentFields.length; i++)
185 Field current = currentFields[i];
186 int size = fields.size();
187 Iterator iter = fields.iterator();
190 Field override = (Field) iter.next();
191 if (current.getName().equals(override.getName())
192 && current.getType() == override.getType())
197 c = c.getSuperclass();
199 retval = new Field[fields.size()];
200 fields.toArray(retval);
201 allFields.put(clazz, retval);