KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > facelets > compiler > ReflectionUtil


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

14
15 package com.sun.facelets.compiler;
16
17 import java.lang.reflect.Array JavaDoc;
18 import java.util.Arrays JavaDoc;
19
20 /**
21  * Utilities for Managing Serialization and Reflection
22  *
23  * @author Jacob Hookom [jacob@hookom.net]
24  * @version $Change: 181177 $$DateTime: 2001/06/26 08:45:09 $$Author: jhook $
25  */

26 class ReflectionUtil {
27
28     protected static final String JavaDoc[] EMPTY_STRING = new String JavaDoc[0];
29
30     protected static final String JavaDoc[] PRIMITIVE_NAMES = new String JavaDoc[] { "boolean",
31             "byte", "char", "double", "float", "int", "long", "short", "void" };
32
33     protected static final Class JavaDoc[] PRIMITIVES = new Class JavaDoc[] { boolean.class,
34             byte.class, char.class, double.class, float.class, int.class,
35             long.class, short.class, Void.TYPE };
36
37     /**
38      *
39      */

40     private ReflectionUtil() {
41         super();
42     }
43
44     public static Class JavaDoc forName(String JavaDoc name) throws ClassNotFoundException JavaDoc {
45         if (null == name || "".equals(name)) {
46             return null;
47         }
48         Class JavaDoc c = forNamePrimitive(name);
49         if (c == null) {
50             if (name.endsWith("[]")) {
51                 String JavaDoc nc = name.substring(0, name.length() - 2);
52                 c = Class.forName(nc, true, Thread.currentThread().getContextClassLoader());
53                 c = Array.newInstance(c, 0).getClass();
54             } else {
55                 c = Class.forName(name, true, Thread.currentThread().getContextClassLoader());
56             }
57         }
58         return c;
59     }
60
61     protected static Class JavaDoc forNamePrimitive(String JavaDoc name) {
62         if (name.length() <= 8) {
63             int p = Arrays.binarySearch(PRIMITIVE_NAMES, name);
64             if (p >= 0) {
65                 return PRIMITIVES[p];
66             }
67         }
68         return null;
69     }
70 }
71
Popular Tags