KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > gwt > dev > util > TypeInfo


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

16 package com.google.gwt.dev.util;
17
18 import java.lang.reflect.Method JavaDoc;
19
20 /**
21  * Java type helpers used.
22  */

23 public class TypeInfo {
24
25   public static final int NOT_FOUND = 0; // used with all of the enumerated
26
public static final int TYPE_ARRAY = 0x200000;
27   public static final int TYPE_COLLECTION = 0x180000;
28   public static final int TYPE_COLLECTION_LIST = 0x100000;
29   public static final int TYPE_COLLECTION_SET = 0x080000;
30   public static final int TYPE_PRIM = 0x07fc00;
31   public static final int TYPE_PRIM_BOOLEAN = 0x008000;
32   public static final int TYPE_PRIM_BYTE = 0x000800;
33   public static final int TYPE_PRIM_CHAR = 0x000400;
34   public static final int TYPE_PRIM_DOUBLE = 0x020000;
35   public static final int TYPE_PRIM_FLOAT = 0x010000;
36   public static final int TYPE_PRIM_INT = 0x002000;
37   public static final int TYPE_PRIM_LONG = 0x004000;
38   public static final int TYPE_PRIM_SHORT = 0x001000;
39   public static final int TYPE_PRIM_VOID = 0x040000;
40   public static final int TYPE_USER = 0x400000;
41   public static final int TYPE_WRAP = 0x0003ff;
42   public static final int TYPE_WRAP_BOOLEAN = 0x000040;
43   public static final int TYPE_WRAP_BYTE = 0x000004;
44   public static final int TYPE_WRAP_CHAR = 0x000002;
45   public static final int TYPE_WRAP_DATE = 0x000200;
46   public static final int TYPE_WRAP_DOUBLE = 0x000100;
47   public static final int TYPE_WRAP_FLOAT = 0x000080;
48   public static final int TYPE_WRAP_INT = 0x000010;
49   public static final int TYPE_WRAP_LONG = 0x000020;
50   public static final int TYPE_WRAP_SHORT = 0x000008;
51   // types below
52
public static final int TYPE_WRAP_STRING = 0x000001;
53
54   public static int classifyType(Class JavaDoc type) {
55     int ret = isPrimitiveType(type);
56     if (ret != 0) {
57       return ret;
58     }
59
60     ret = isPrimitiveWrapperType(type);
61     if (ret != 0) {
62       return ret;
63     }
64
65     if (type.isArray()) {
66       return TYPE_ARRAY;
67     }
68
69     return TYPE_USER;
70   }
71
72   public static Method JavaDoc getInterfaceMethod(Class JavaDoc intf, String JavaDoc methodName,
73       Class JavaDoc[] paramTypes, boolean includeInherited) {
74     try {
75       return intf.getDeclaredMethod(methodName, paramTypes);
76     } catch (NoSuchMethodException JavaDoc e) {
77       if (includeInherited) {
78         Class JavaDoc[] superintfs = intf.getInterfaces();
79         for (int i = 0; i < superintfs.length; i++) {
80           Method JavaDoc method = getInterfaceMethod(superintfs[i], methodName,
81             paramTypes, true);
82           if (method != null) {
83             return method;
84           }
85         }
86       }
87
88       return null;
89     }
90   }
91
92   public static String JavaDoc getJavahMangledIdent(String JavaDoc before) {
93     StringBuffer JavaDoc after = new StringBuffer JavaDoc();
94     char[] chars = before.toCharArray();
95     for (int i = 0; i < chars.length; i++) {
96       char c = chars[i];
97       if (c == '_') {
98         after.append("_1");
99       } else if (c == ';') {
100         after.append("_2");
101       } else if (c == '[') {
102         after.append("_3");
103       } else if (c == '/') {
104         after.append('_');
105       } else if (Character.isJavaIdentifierPart(c)) {
106         after.append(c);
107       } else {
108         throw new UnsupportedOperationException JavaDoc("Cannot handle non-ascii yet");
109       }
110     }
111     return after.toString();
112   }
113
114   public static String JavaDoc getSourceRepresentation(Class JavaDoc type) {
115     // Primitives
116
//
117
if (type.equals(Integer.TYPE)) {
118       return "int";
119     } else if (type.equals(Long.TYPE)) {
120       return "long";
121     } else if (type.equals(Short.TYPE)) {
122       return "short";
123     } else if (type.equals(Byte.TYPE)) {
124       return "byte";
125     } else if (type.equals(Character.TYPE)) {
126       return "char";
127     } else if (type.equals(Boolean.TYPE)) {
128       return "boolean";
129     } else if (type.equals(Float.TYPE)) {
130       return "float";
131     } else if (type.equals(Double.TYPE)) {
132       return "double";
133     }
134
135     // Arrays
136
//
137
if (type.isArray()) {
138       Class JavaDoc componentType = type.getComponentType();
139       return getSourceRepresentation(componentType) + "[]";
140     }
141
142     // Everything else
143
//
144
return type.getName().replace('$', '.');
145   }
146
147   public static int isPrimitiveType(Class JavaDoc type) {
148     if (type.equals(Integer.TYPE)) {
149       return TYPE_PRIM_INT;
150     } else if (type.equals(Long.TYPE)) {
151       return TYPE_PRIM_LONG;
152     } else if (type.equals(Character.TYPE)) {
153       return TYPE_PRIM_CHAR;
154     } else if (type.equals(Byte.TYPE)) {
155       return TYPE_PRIM_BYTE;
156     } else if (type.equals(Short.TYPE)) {
157       return TYPE_PRIM_SHORT;
158     } else if (type.equals(Boolean.TYPE)) {
159       return TYPE_PRIM_BOOLEAN;
160     } else if (type.equals(Float.TYPE)) {
161       return TYPE_PRIM_FLOAT;
162     } else if (type.equals(Double.TYPE)) {
163       return TYPE_PRIM_DOUBLE;
164     } else if (type.equals(Void.TYPE)) {
165       return TYPE_PRIM_VOID;
166     } else {
167       return NOT_FOUND;
168     }
169   }
170
171   public static int isPrimitiveWrapperType(Class JavaDoc type) {
172     if (type.equals(String JavaDoc.class)) {
173       return TYPE_WRAP_STRING;
174     } else if (type.equals(Integer JavaDoc.class)) {
175       return TYPE_WRAP_INT;
176     } else if (type.equals(Long JavaDoc.class)) {
177       return TYPE_WRAP_LONG;
178     } else if (type.equals(Character JavaDoc.class)) {
179       return TYPE_WRAP_CHAR;
180     } else if (type.equals(Byte JavaDoc.class)) {
181       return TYPE_WRAP_BYTE;
182     } else if (type.equals(Short JavaDoc.class)) {
183       return TYPE_WRAP_SHORT;
184     } else if (type.equals(Boolean JavaDoc.class)) {
185       return TYPE_WRAP_BOOLEAN;
186     } else if (type.equals(Float JavaDoc.class)) {
187       return TYPE_WRAP_FLOAT;
188     } else if (type.equals(Double JavaDoc.class)) {
189       return TYPE_WRAP_DOUBLE;
190     } else {
191       return NOT_FOUND;
192     }
193   }
194 }
Popular Tags