01: /**
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */package org.apache.openejb.util;
17:
18: import java.util.HashMap;
19: import java.lang.reflect.Array;
20:
21: /**
22: * @version $Rev: 602704 $ $Date: 2007-12-09 09:58:22 -0800 $
23: */
24: public class Classes {
25:
26: private static final HashMap<String, Class> primitives = new HashMap();
27: static {
28: Classes.primitives.put("boolean", boolean.class);
29: Classes.primitives.put("byte", byte.class);
30: Classes.primitives.put("char", char.class);
31: Classes.primitives.put("short", short.class);
32: Classes.primitives.put("int", int.class);
33: Classes.primitives.put("long", long.class);
34: Classes.primitives.put("float", float.class);
35: Classes.primitives.put("double", double.class);
36: }
37:
38: public static Class forName(String string, ClassLoader classLoader)
39: throws ClassNotFoundException {
40: int arrayDimentions = 0;
41: while (string.endsWith("[]")) {
42: string = string.substring(0, string.length() - 2);
43: arrayDimentions++;
44: }
45:
46: Class clazz = primitives.get(string);
47:
48: if (clazz == null)
49: clazz = Class.forName(string, true, classLoader);
50:
51: if (arrayDimentions == 0) {
52: return clazz;
53: }
54: return Array.newInstance(clazz, new int[arrayDimentions])
55: .getClass();
56: }
57:
58: public static String packageName(Class clazz) {
59: return packageName(clazz.getName());
60: }
61:
62: public static String packageName(String clazzName) {
63: int i = clazzName.lastIndexOf('.');
64: if (i > 0) {
65: return clazzName.substring(0, i);
66: } else {
67: return "";
68: }
69: }
70: }
|