KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > wsm > processor > apt > TypeMirrorUtil


1 package org.apache.beehive.wsm.processor.apt;
2
3 /*
4  * Copyright 2004 The Apache Software Foundation
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * 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  * $Header:$
19  */

20
21 import com.sun.mirror.type.ArrayType;
22 import com.sun.mirror.type.PrimitiveType;
23 import com.sun.mirror.type.TypeMirror;
24 import com.sun.mirror.type.VoidType;
25
26 public class TypeMirrorUtil {
27
28     // returns a class object of the specified type.
29
public static Class JavaDoc classForName(TypeMirror type) throws ClassNotFoundException JavaDoc {
30
31         Class JavaDoc clazz = null;
32         
33         try {
34             if (type instanceof PrimitiveType) {
35                 clazz = getPrimitiveClass(((PrimitiveType) type).getKind());
36             }
37             else if (type instanceof VoidType) {
38                 clazz = void.class;
39             }
40             else if (type instanceof ArrayType) {
41                 Object JavaDoc[] typeOfArray = new Object JavaDoc[]{new Object JavaDoc()};
42                 int arrayDepth = arrayDepth((ArrayType) type, 0, typeOfArray);
43                 StringBuffer JavaDoc className = new StringBuffer JavaDoc();
44                 for (int i = 0; i < arrayDepth; i++) {
45                     className.append("[");
46                 }
47                 TypeMirror mirrorTypeOfArray = (TypeMirror) typeOfArray[0];
48                 if (mirrorTypeOfArray instanceof PrimitiveType) {
49                     // e.g. an array of int will be "[I".
50
className.append(getTypeSignatureOfPrimitiveType(((PrimitiveType) mirrorTypeOfArray).getKind()));
51                 }
52                 else {
53                     // e.g. an array of String will be "[Ljava.lang.String;".
54
className.append("L").append(((TypeMirror) typeOfArray[0]).toString()).append(";");
55                 }
56                 clazz = findClass(className.toString());
57             }
58             else {
59                 clazz = findClass(type.toString());
60             }
61         }
62         catch (ClassNotFoundException JavaDoc e) {
63             throw new ClassNotFoundException JavaDoc(type.toString());
64         }
65         return clazz;
66     }
67
68     private static Class JavaDoc getPrimitiveClass(PrimitiveType.Kind kind) throws ClassNotFoundException JavaDoc {
69         // todo: change order of the cases for better performance.
70
switch (kind) {
71             case BOOLEAN:
72                 return boolean.class;
73             case BYTE:
74                 return byte.class;
75             case CHAR:
76                 return char.class;
77             case DOUBLE:
78                 return double.class;
79             case FLOAT:
80                 return float.class;
81             case INT:
82                 return int.class;
83             case LONG:
84                 return long.class;
85             case SHORT:
86                 return short.class;
87         }
88
89         throw new ClassNotFoundException JavaDoc();
90     }
91
92     private static String JavaDoc getTypeSignatureOfPrimitiveType(PrimitiveType.Kind kind) throws ClassNotFoundException JavaDoc {
93         // todo: change order of the cases for better performance.
94
switch (kind) {
95             case BOOLEAN:
96                 return "Z";
97             case BYTE:
98                 return "B";
99             case CHAR:
100                 return "C";
101             case DOUBLE:
102                 return "D";
103             case FLOAT:
104                 return "F";
105             case INT:
106                 return "I";
107             case LONG:
108                 return "J";
109             case SHORT:
110                 return "S";
111         }
112
113         throw new ClassNotFoundException JavaDoc();
114     }
115
116     private static int arrayDepth(ArrayType type, int depth, Object JavaDoc[] typeOfArray) {
117         if (type.getComponentType() instanceof ArrayType) {
118             return arrayDepth((ArrayType) type.getComponentType(), ++depth, typeOfArray);
119         } else {
120             typeOfArray[0] = type.getComponentType();
121         }
122         return ++depth;
123     }
124     
125     /**
126      * Since we work using the Mirror API, we need to look for inner classes
127      * ourselves. This method assumes that the class we're looking for is
128      * readily available through the class loader.
129      * @param className
130      * @return
131      * @throws ClassNotFoundException
132      */

133     private static Class JavaDoc findClass(String JavaDoc className) throws ClassNotFoundException JavaDoc {
134         Class JavaDoc clazz = null;
135         int idx = className.lastIndexOf('.');
136         if (-1 == idx) {
137             clazz = Class.forName(className);
138         }
139         else {
140             try {
141                 clazz = Class.forName(className);
142             }
143             catch (ClassNotFoundException JavaDoc e) {
144                 char[] chars = className.toCharArray();
145                 for (int i = chars.length - 1; i >= 0 ; i--) {
146                     if ('.' == chars[i]) {
147                         chars[i] = '$';
148                         break;
149                     }
150                 }
151                 clazz = findClass(new String JavaDoc(chars));
152             }
153         }
154         return clazz;
155     }
156 }
157
Popular Tags