KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fr > dyade > aaa > util > TypeLoader


1 /*
2  * Copyright (C) 2000 ScalAgent Distributed Technologies
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17  * USA.
18  *
19  */

20 package fr.dyade.aaa.util;
21
22 import java.lang.reflect.*;
23
24 public class TypeLoader {
25
26   /**
27    * If the specified type is primitive,
28    * the associated wrapper class is returned
29    * (e.g. java.lang.Long for a primitive long).
30    *
31    * @return the type or <code>NULL</code> if
32    * it has not been found.
33    */

34   public static Class JavaDoc loadType(String JavaDoc typeName,
35                                ClassLoader JavaDoc classLoader) {
36     try {
37       return Class.forName(typeName);
38     } catch (ClassNotFoundException JavaDoc exc) {
39       try {
40         if (classLoader != null) {
41           return classLoader.loadClass(typeName);
42         }
43       } catch (ClassNotFoundException JavaDoc exc2) {}
44     }
45     
46     // This may be either:
47
// - a primitive type
48
// - an array type if no instance of this
49
// array of this type has been created yet.
50
// - an unknown type
51
if (typeName.equals("int")) {
52       return Integer JavaDoc.class;
53     } else if (typeName.equals("long")) {
54       return Long JavaDoc.class;
55     } else if (typeName.equals("boolean")) {
56       return Boolean JavaDoc.class;
57     } else if (typeName.equals("short")) {
58       return Short JavaDoc.class;
59     } else if (typeName.equals("double")) {
60       return Double JavaDoc.class;
61     } else if (typeName.equals("float")) {
62       return Float JavaDoc.class;
63     } else if (typeName.length() > 4 &&
64                typeName.charAt(0) == '[' &&
65                typeName.charAt(1) == 'L' &&
66                typeName.charAt(typeName.length() - 1) == ';') {
67       String JavaDoc eltClassName = typeName.substring(
68         2, typeName.length() - 1);
69       Class JavaDoc eltClass = loadType(eltClassName, classLoader);
70       if (eltClass == null) return null;
71       // Have to create an instance to get the type (class)
72
Object JavaDoc arrayInstance = Array.newInstance(eltClass, 0);
73       return arrayInstance.getClass();
74     } else return null;
75   }
76 }
77
Popular Tags