OpenCores
URL https://opencores.org/ocsvn/scarts/scarts/trunk

Subversion Repositories scarts

[/] [scarts/] [trunk/] [toolchain/] [scarts-gcc/] [gcc-4.1.1/] [libjava/] [classpath/] [gnu/] [java/] [lang/] [ClassHelper.java] - Blame information for rev 14

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 14 jlechner
/* ClassHelper.java -- Utility methods to augment java.lang.Class
2
   Copyright (C) 1998, 2002 Free Software Foundation, Inc.
3
 
4
This file is part of GNU Classpath.
5
 
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.
10
 
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.
15
 
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.
20
 
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.
25
 
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. */
37
 
38
 
39
package gnu.java.lang;
40
 
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;
49
 
50
/**
51
 * ClassHelper has various methods that ought to have been in Class.
52
 *
53
 * @author John Keiser
54
 * @author Eric Blake (ebb9@email.byu.edu)
55
 */
56
public class ClassHelper
57
{
58
  /**
59
   * Strip the package part from the class name.
60
   *
61
   * @param clazz the class to get the truncated name from
62
   * @return the truncated class name
63
   */
64
  public static String getTruncatedClassName(Class clazz)
65
  {
66
    return getTruncatedName(clazz.getName());
67
  }
68
 
69
  /**
70
   * Strip the package part from the class name, or the class part from
71
   * the method or field name.
72
   *
73
   * @param name the name to truncate
74
   * @return the truncated name
75
   */
76
  public static String getTruncatedName(String name)
77
  {
78
    int lastInd = name.lastIndexOf('.');
79
    if (lastInd == -1)
80
      return name;
81
    return name.substring(lastInd + 1);
82
  }
83
 
84
  /** Cache of methods found in getAllMethods(). */
85
  private static Map allMethods = new HashMap();
86
 
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.
92
   *
93
   * @param clazz the class to start at
94
   * @return all methods declared or inherited in clazz
95
   */
96
  public static Method[] getAllMethods(Class clazz)
97
  {
98
    Method[] retval = (Method[]) allMethods.get(clazz);
99
    if (retval == null)
100
      {
101
        Set methods = new HashSet();
102
        Class c = clazz;
103
        while (c != null)
104
          {
105
            Method[] currentMethods = c.getDeclaredMethods();
106
          loop:
107
            for (int i = 0; i < currentMethods.length; i++)
108
              {
109
                Method current = currentMethods[i];
110
                int size = methods.size();
111
                Iterator iter = methods.iterator();
112
                while (--size >= 0)
113
                  {
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;
120
                  }
121
                methods.add(current);
122
              }
123
            c = c.getSuperclass();
124
          }
125
        retval = new Method[methods.size()];
126
        methods.toArray(retval);
127
        allMethods.put(clazz, retval);
128
      }
129
    return retval;
130
  }
131
 
132
  /** Cache of fields found in getAllFields(). */
133
  private static Map allFields = new HashMap();
134
 
135
  /**
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.
140
   *
141
   * @param clazz the class to start at
142
   * @return all fields declared or inherited in clazz
143
   */
144
  public static Field[] getAllFields(Class clazz)
145
  {
146
    Field[] retval = (Field[]) allFields.get(clazz);
147
    if (retval == null)
148
      {
149
        Set fields = new HashSet();
150
        Class c = clazz;
151
        while (c != null)
152
          {
153
            Field[] currentFields = c.getDeclaredFields();
154
          loop:
155
            for (int i = 0; i < currentFields.length; i++)
156
              {
157
                Field current = currentFields[i];
158
                int size = fields.size();
159
                Iterator iter = fields.iterator();
160
                while (--size >= 0)
161
                  {
162
                    Field override = (Field) iter.next();
163
                    if (current.getName().equals(override.getName())
164
                        && current.getType() == override.getType())
165
                      continue loop;
166
                  }
167
                fields.add(current);
168
              }
169
            c = c.getSuperclass();
170
          }
171
        retval = new Field[fields.size()];
172
        fields.toArray(retval);
173
        allFields.put(clazz, retval);
174
      }
175
    return retval;
176
  }
177
}

powered by: WebSVN 2.1.0

© copyright 1999-2025 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.