2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / libjava / gnu / java / lang / ClassHelper.java
blob16d699e3453e18311d2d8d76ce12aec1d9c6774f
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., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 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.util.*;
42 import java.lang.reflect.*;
44 /**
45 * ClassHelper has various methods that ought to have been in Class.
47 * @author John Keiser
48 * @author Eric Blake <ebb9@email.byu.edu>
50 public class ClassHelper
52 /**
53 * Strip the package part from the class name.
55 * @param clazz the class to get the truncated name from
56 * @return the truncated class name
58 public static String getTruncatedClassName(Class clazz)
60 return getTruncatedName(clazz.getName());
63 /**
64 * Strip the package part from the class name, or the class part from
65 * the method or field name.
67 * @param name the name to truncate
68 * @return the truncated name
70 public static String getTruncatedName(String name)
72 int lastInd = name.lastIndexOf('.');
73 if (lastInd == -1)
74 return name;
75 return name.substring(lastInd + 1);
78 /**
79 * Strip the last portion of the name (after the last dot).
81 * @param name the name to get package of
82 * @return the package name, or "" if no package
84 public static String getPackagePortion(String name)
86 int lastInd = name.lastIndexOf('.');
87 if (lastInd == -1)
88 return "";
89 return name.substring(0, lastInd);
92 /** Cache of methods found in getAllMethods(). */
93 private static Map allMethods = new HashMap();
95 /**
96 * Get all the methods, public, private and otherwise, from the class,
97 * getting them from the most recent class to find them. This may not
98 * be quite the correct approach, as this includes methods that are not
99 * inherited or accessible from clazz, so beware.
101 * @param clazz the class to start at
102 * @return all methods declared or inherited in clazz
104 public static Method[] getAllMethods(Class clazz)
106 Method[] retval = (Method[]) allMethods.get(clazz);
107 if (retval == null)
109 Set methods = new HashSet();
110 Class c = clazz;
111 while (c != null)
113 Method[] currentMethods = c.getDeclaredMethods();
114 loop:
115 for (int i = 0; i < currentMethods.length; i++)
117 Method current = currentMethods[i];
118 int size = methods.size();
119 Iterator iter = methods.iterator();
120 while (--size >= 0)
122 Method override = (Method) iter.next();
123 if (current.getName().equals(override.getName())
124 && Arrays.equals(current.getParameterTypes(),
125 override.getParameterTypes())
126 && current.getReturnType() == override.getReturnType())
127 continue loop;
129 methods.add(current);
131 c = c.getSuperclass();
133 retval = new Method[methods.size()];
134 methods.toArray(retval);
135 allMethods.put(clazz, retval);
137 return retval;
140 /** Cache of fields found in getAllFields(). */
141 private static Map allFields = new HashMap();
144 * Get all the fields, public, private and otherwise, from the class,
145 * getting them from the most recent class to find them. This may not
146 * be quite the correct approach, as this includes fields that are not
147 * inherited or accessible from clazz, so beware.
149 * @param clazz the class to start at
150 * @return all fields declared or inherited in clazz
152 public static Field[] getAllFields(Class clazz)
154 Field[] retval = (Field[]) allFields.get(clazz);
155 if (retval == null)
157 Set fields = new HashSet();
158 Class c = clazz;
159 while (c != null)
161 Field[] currentFields = c.getDeclaredFields();
162 loop:
163 for (int i = 0; i < currentFields.length; i++)
165 Field current = currentFields[i];
166 int size = fields.size();
167 Iterator iter = fields.iterator();
168 while (--size >= 0)
170 Field override = (Field) iter.next();
171 if (current.getName().equals(override.getName())
172 && current.getType() == override.getType())
173 continue loop;
175 fields.add(current);
177 c = c.getSuperclass();
179 retval = new Field[fields.size()];
180 fields.toArray(retval);
181 allFields.put(clazz, retval);
183 return retval;