KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mchange > v1 > lang > ClassUtils


1 /*
2  * Distributed as part of c3p0 v.0.9.1
3  *
4  * Copyright (C) 2005 Machinery For Change, Inc.
5  *
6  * Author: Steve Waldman <swaldman@mchange.com>
7  *
8  * This library is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License version 2.1, as
10  * published by the Free Software Foundation.
11  *
12  * This software is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this software; see the file LICENSE. If not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */

22
23
24 package com.mchange.v1.lang;
25
26 import java.util.*;
27 import com.mchange.v1.jvm.*;
28
29 public final class ClassUtils
30 {
31     final static String JavaDoc[] EMPTY_SA = new String JavaDoc[0];
32
33     static Map primitivesToClasses;
34
35     static
36     {
37     HashMap tmp = new HashMap();
38     tmp.put( "boolean", boolean.class );
39     tmp.put( "int", int.class );
40     tmp.put( "char", char.class );
41     tmp.put( "short", short.class );
42     tmp.put( "int", int.class );
43     tmp.put( "long", long.class );
44     tmp.put( "float", float.class );
45     tmp.put( "double", double.class );
46     tmp.put( "void", void.class );
47
48     primitivesToClasses = Collections.unmodifiableMap( tmp );
49     }
50
51     public static Set allAssignableFrom(Class JavaDoc type)
52     {
53     Set out = new HashSet();
54
55     //type itself and superclasses (if any)
56
for (Class JavaDoc cl = type; cl != null; cl = cl.getSuperclass())
57         out.add( cl );
58
59     //super interfaces (if any)
60
addSuperInterfacesToSet( type, out );
61     return out;
62     }
63
64     public static String JavaDoc simpleClassName(Class JavaDoc cl)
65     {
66     String JavaDoc scn;
67     int array_level = 0;
68     while (cl.isArray())
69         {
70         ++array_level;
71         cl = cl.getComponentType();
72         }
73     scn = simpleClassName( cl.getName() );
74     if ( array_level > 0 )
75         {
76         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(16);
77         sb.append( scn );
78         for( int i = 0; i < array_level; ++i)
79             sb.append("[]");
80         return sb.toString();
81         }
82     else
83         return scn;
84     }
85
86     private static String JavaDoc simpleClassName(String JavaDoc fqcn)
87     {
88        int pkgdot = fqcn.lastIndexOf('.');
89        if (pkgdot < 0)
90           return fqcn;
91        String JavaDoc scn = fqcn.substring(pkgdot + 1);
92        if (scn.indexOf('$') >= 0)
93           {
94              StringBuffer JavaDoc sb = new StringBuffer JavaDoc(scn);
95              for (int i = 0, len = sb.length(); i < len; ++i)
96              {
97                  if (sb.charAt(i) == '$')
98                    sb.setCharAt(i, '.');
99              }
100              return sb.toString();
101           }
102        else
103          return scn;
104     }
105
106     public static boolean isPrimitive(String JavaDoc typeStr)
107     { return (primitivesToClasses.get( typeStr ) != null); }
108
109     public static Class JavaDoc classForPrimitive(String JavaDoc typeStr)
110     { return (Class JavaDoc) primitivesToClasses.get( typeStr ); }
111     
112     public static Class JavaDoc forName(String JavaDoc fqcnOrPrimitive ) throws ClassNotFoundException JavaDoc
113     {
114         Class JavaDoc out = classForPrimitive( fqcnOrPrimitive );
115         if (out == null)
116             out = Class.forName( fqcnOrPrimitive );
117         return out;
118     }
119
120     public static Class JavaDoc forName( String JavaDoc fqOrSimple, String JavaDoc[] importPkgs, String JavaDoc[] importClasses )
121     throws AmbiguousClassNameException, ClassNotFoundException JavaDoc
122     {
123     try
124         { return Class.forName( fqOrSimple ); }
125     catch ( ClassNotFoundException JavaDoc e )
126         { return classForSimpleName( fqOrSimple, importPkgs, importClasses ); }
127     }
128
129     public static Class JavaDoc classForSimpleName( String JavaDoc simpleName, String JavaDoc[] importPkgs, String JavaDoc[] importClasses )
130     throws AmbiguousClassNameException, ClassNotFoundException JavaDoc
131     {
132     Set checkSet = new HashSet();
133     Class JavaDoc out = classForPrimitive( simpleName );
134
135     if (out == null)
136         {
137         if (importPkgs == null)
138             importPkgs = EMPTY_SA;
139         
140         if (importClasses == null)
141             importClasses = EMPTY_SA;
142         
143         for (int i = 0, len = importClasses.length; i < len; ++i)
144             {
145             String JavaDoc importSimpleName = fqcnLastElement( importClasses[i] );
146             if (! checkSet.add( importSimpleName ) )
147                 throw new IllegalArgumentException JavaDoc("Duplicate imported classes: " +
148                                    importSimpleName);
149             if ( simpleName.equals( importSimpleName ) )
150                 //we won't duplicate assign. we'd have caught it above
151
out = Class.forName( importClasses[i] );
152             }
153         if (out == null)
154             {
155             try { out = Class.forName("java.lang." + simpleName); }
156             catch (ClassNotFoundException JavaDoc e)
157                 { /* just means we haven't found it yet */ }
158
159             for (int i = 0, len = importPkgs.length; i < len; ++i)
160                 {
161                 try
162                     {
163                     String JavaDoc tryClass = importPkgs[i] + '.' + simpleName;
164                     Class JavaDoc test = Class.forName( tryClass );
165                     if ( out == null )
166                         out = test;
167                     else
168                         throw new AmbiguousClassNameException( simpleName, out, test );
169                     }
170                 catch (ClassNotFoundException JavaDoc e)
171                     { /* just means we haven't found it yet */ }
172                 }
173             }
174         }
175     if (out == null)
176         throw new ClassNotFoundException JavaDoc( "Could not find a class whose unqualified name is \042" +
177                           simpleName + "\042 with the imports supplied. Import packages are " +
178                           Arrays.asList( importPkgs ) + "; class imports are " +
179                           Arrays.asList( importClasses ) );
180     else
181         return out;
182     }
183
184     public static String JavaDoc resolvableTypeName( Class JavaDoc type, String JavaDoc[] importPkgs, String JavaDoc[] importClasses )
185     throws ClassNotFoundException JavaDoc
186     {
187     String JavaDoc simpleName = simpleClassName( type );
188     try
189         { classForSimpleName( simpleName, importPkgs, importClasses ); }
190     catch ( AmbiguousClassNameException e )
191         { return type.getName(); }
192     return simpleName;
193     }
194
195     public static String JavaDoc fqcnLastElement(String JavaDoc fqcn)
196     {
197        int pkgdot = fqcn.lastIndexOf('.');
198        if (pkgdot < 0)
199           return fqcn;
200        return fqcn.substring(pkgdot + 1);
201     }
202
203
204     /* does not add type itself, only its superinterfaces */
205     private static void addSuperInterfacesToSet(Class JavaDoc type, Set set)
206     {
207     Class JavaDoc[] ifaces = type.getInterfaces();
208     for (int i = 0, len = ifaces.length; i < len; ++i)
209         {
210         set.add( ifaces[i] );
211         addSuperInterfacesToSet( ifaces[i], set );
212         }
213     }
214
215     private ClassUtils()
216     {}
217 }
218
Popular Tags