KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > util > ClassLoading


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq.util;
19
20 import java.lang.reflect.Array JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.Map JavaDoc;
23
24 /**
25  * Utilities for loading classes.
26  *
27  * @version $Rev: 109957 $ $Date$
28  */

29 public class ClassLoading {
30
31     /**
32      * Load a class for the given name. <p/>
33      * <p>
34      * Handles loading primitive types as well as VM class and array syntax.
35      *
36      * @param className
37      * The name of the Class to be loaded.
38      * @param classLoader
39      * The class loader to load the Class object from.
40      * @return The Class object for the given name.
41      * @throws ClassNotFoundException
42      * Failed to load Class object.
43      */

44     public static Class JavaDoc loadClass(final String JavaDoc className, final ClassLoader JavaDoc classLoader) throws ClassNotFoundException JavaDoc {
45         if (className == null) {
46             throw new IllegalArgumentException JavaDoc("className is null");
47         }
48
49         // First just try to load
50
try {
51             return load(className, classLoader);
52         } catch (ClassNotFoundException JavaDoc ignore) {
53             // handle special cases below
54
}
55
56         Class JavaDoc type = null;
57
58         // Check if it is a primitive type
59
type = getPrimitiveType(className);
60         if (type != null)
61             return type;
62
63         // Check if it is a vm primitive
64
type = getVMPrimitiveType(className);
65         if (type != null)
66             return type;
67
68         // Handle VM class syntax (Lclassname;)
69
if (className.charAt(0) == 'L' && className.charAt(className.length() - 1) == ';') {
70             String JavaDoc name = className.substring(1, className.length() - 1);
71             return load(name, classLoader);
72         }
73
74         // Handle VM array syntax ([type)
75
if (className.charAt(0) == '[') {
76             int arrayDimension = className.lastIndexOf('[') + 1;
77             String JavaDoc componentClassName = className.substring(arrayDimension, className.length());
78             type = loadClass(componentClassName, classLoader);
79
80             int dim[] = new int[arrayDimension];
81             java.util.Arrays.fill(dim, 0);
82             return Array.newInstance(type, dim).getClass();
83         }
84
85         // Handle user friendly type[] syntax
86
if (className.endsWith("[]")) {
87             // get the base component class name and the arrayDimensions
88
int arrayDimension = 0;
89             String JavaDoc componentClassName = className;
90             while (componentClassName.endsWith("[]")) {
91                 componentClassName = componentClassName.substring(0, componentClassName.length() - 2);
92                 arrayDimension++;
93             }
94
95             // load the base type
96
type = loadClass(componentClassName, classLoader);
97
98             // return the array type
99
int[] dim = new int[arrayDimension];
100             java.util.Arrays.fill(dim, 0);
101             return Array.newInstance(type, dim).getClass();
102         }
103
104         // Else we can not load (give up)
105
throw new ClassNotFoundException JavaDoc(className);
106     }
107
108     private static Class JavaDoc load(final String JavaDoc className, final ClassLoader JavaDoc classLoader) throws ClassNotFoundException JavaDoc {
109         if (classLoader == null)
110             return Class.forName(className);
111         else
112             return classLoader.loadClass(className);
113     }
114
115     public static String JavaDoc getClassName(Class JavaDoc clazz) {
116         StringBuffer JavaDoc rc = new StringBuffer JavaDoc();
117         while (clazz.isArray()) {
118             rc.append('[');
119             clazz = clazz.getComponentType();
120         }
121         if (!clazz.isPrimitive()) {
122             rc.append('L');
123             rc.append(clazz.getName());
124             rc.append(';');
125         } else {
126             rc.append(VM_PRIMITIVES_REVERSE.get(clazz));
127         }
128         return rc.toString();
129     }
130
131     /**
132      * Primitive type name -> class map.
133      */

134     private static final Map JavaDoc PRIMITIVES = new HashMap JavaDoc();
135
136     /** Setup the primitives map. */
137     static {
138         PRIMITIVES.put("boolean", Boolean.TYPE);
139         PRIMITIVES.put("byte", Byte.TYPE);
140         PRIMITIVES.put("char", Character.TYPE);
141         PRIMITIVES.put("short", Short.TYPE);
142         PRIMITIVES.put("int", Integer.TYPE);
143         PRIMITIVES.put("long", Long.TYPE);
144         PRIMITIVES.put("float", Float.TYPE);
145         PRIMITIVES.put("double", Double.TYPE);
146         PRIMITIVES.put("void", Void.TYPE);
147     }
148
149     /**
150      * Get the primitive type for the given primitive name.
151      *
152      * @param name
153      * Primitive type name (boolean, byte, int, ...)
154      * @return Primitive type or null.
155      */

156     private static Class JavaDoc getPrimitiveType(final String JavaDoc name) {
157         return (Class JavaDoc) PRIMITIVES.get(name);
158     }
159
160     /**
161      * VM primitive type name -> primitive type
162      */

163     private static final HashMap JavaDoc VM_PRIMITIVES = new HashMap JavaDoc();
164
165     /** Setup the vm primitives map. */
166     static {
167         VM_PRIMITIVES.put("B", byte.class);
168         VM_PRIMITIVES.put("C", char.class);
169         VM_PRIMITIVES.put("D", double.class);
170         VM_PRIMITIVES.put("F", float.class);
171         VM_PRIMITIVES.put("I", int.class);
172         VM_PRIMITIVES.put("J", long.class);
173         VM_PRIMITIVES.put("S", short.class);
174         VM_PRIMITIVES.put("Z", boolean.class);
175         VM_PRIMITIVES.put("V", void.class);
176     }
177
178     /**
179      * VM primitive type primitive type -> name
180      */

181     private static final HashMap JavaDoc VM_PRIMITIVES_REVERSE = new HashMap JavaDoc();
182
183     /** Setup the vm primitives reverse map. */
184     static {
185         VM_PRIMITIVES_REVERSE.put(byte.class, "B");
186         VM_PRIMITIVES_REVERSE.put(char.class, "C");
187         VM_PRIMITIVES_REVERSE.put(double.class, "D");
188         VM_PRIMITIVES_REVERSE.put(float.class, "F");
189         VM_PRIMITIVES_REVERSE.put(int.class, "I");
190         VM_PRIMITIVES_REVERSE.put(long.class, "J");
191         VM_PRIMITIVES_REVERSE.put(short.class, "S");
192         VM_PRIMITIVES_REVERSE.put(boolean.class, "Z");
193         VM_PRIMITIVES_REVERSE.put(void.class, "V");
194     }
195
196     /**
197      * Get the primitive type for the given VM primitive name. <p/>
198      * <p>
199      * Mapping:
200      *
201      * <pre>
202      *
203      * B - byte
204      * C - char
205      * D - double
206      * F - float
207      * I - int
208      * J - long
209      * S - short
210      * Z - boolean
211      * V - void
212      *
213      * </pre>
214      *
215      * @param name
216      * VM primitive type name (B, C, J, ...)
217      * @return Primitive type or null.
218      */

219     private static Class JavaDoc getVMPrimitiveType(final String JavaDoc name) {
220         return (Class JavaDoc) VM_PRIMITIVES.get(name);
221     }
222
223     /**
224      * Map of primitive types to their wrapper classes
225      */

226     private static final Map JavaDoc PRIMITIVE_WRAPPERS = new HashMap JavaDoc();
227
228     /** Setup the wrapper map. */
229     static {
230         PRIMITIVE_WRAPPERS.put(Boolean.TYPE, Boolean JavaDoc.class);
231         PRIMITIVE_WRAPPERS.put(Byte.TYPE, Byte JavaDoc.class);
232         PRIMITIVE_WRAPPERS.put(Character.TYPE, Character JavaDoc.class);
233         PRIMITIVE_WRAPPERS.put(Double.TYPE, Double JavaDoc.class);
234         PRIMITIVE_WRAPPERS.put(Float.TYPE, Float JavaDoc.class);
235         PRIMITIVE_WRAPPERS.put(Integer.TYPE, Integer JavaDoc.class);
236         PRIMITIVE_WRAPPERS.put(Long.TYPE, Long JavaDoc.class);
237         PRIMITIVE_WRAPPERS.put(Short.TYPE, Short JavaDoc.class);
238         PRIMITIVE_WRAPPERS.put(Void.TYPE, Void JavaDoc.class);
239     }
240 }
241
Popular Tags