KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > proxy > compiler > Utility


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.proxy.compiler;
23
24 import org.apache.bcel.Constants;
25 import org.apache.bcel.generic.BasicType;
26 import org.apache.bcel.generic.ObjectType;
27 import org.apache.bcel.generic.Type;
28
29 import org.jboss.util.UnreachableStatementException;
30
31 /**
32  * Some Routines to convert from <code>java.lang.Class</code> to
33  * <code>org.apache.bcel.generic.Type</code>. These get round some
34  * inconsistencies with Class.getName() wrt primitives.
35  *
36  * <pre>
37  * e.g.
38  *
39  * <code>Character.Type.getName()</code> returns char.
40  *
41  * </pre>
42  *
43  * <p>
44  * I think it should return C. Don't know if this is a code bug. But there's a bug
45  * on Bug Parade (#4369208) about the javadoc being misleading.
46  *
47  * @see java.lang.Class#getName
48  *
49  * @version <tt>$Revision: 37459 $</tt>
50  * @author <a HREF="mailto:neale@isismanor.co.uk">Neale Swinnerton</a>
51  * @author <a HREF="mailto:jason@planet57.com">Jason Dillon</a>
52  */

53 public abstract class Utility
54    extends org.apache.bcel.classfile.Utility
55 {
56    /**
57     * Get the <code>org.apache.bcel.generic.Type</code> for a class.
58     * This handles the case where the class represents an n-dimensional
59     * array by relying on the fact that <code>Class.getName()</code>
60     * on an array returns the signature
61     *
62     * <pre>
63     * e.g.
64     *
65     * <code>new Object[].getClass().getName()</code> returns [Ljava.lang.Object;
66     *
67     * </pre>
68     *
69     * @see Utility
70     *
71     * @param clazz a <code>Class</code> value
72     * @return a <code>Type</code> value
73     */

74    public static Type getType(Class JavaDoc clazz) {
75       if (clazz.isPrimitive()) {
76          if (clazz.equals(Boolean.TYPE) ) {
77             return Type.BOOLEAN;
78          }
79          else if (clazz.equals(Byte.TYPE) ) {
80             return Type.BYTE;
81          }
82          else if (clazz.equals(Character.TYPE) ) {
83             return Type.CHAR;
84          }
85          else if (clazz.equals(Double.TYPE) ) {
86             return Type.DOUBLE;
87          }
88          else if (clazz.equals(Float.TYPE) ) {
89             return Type.FLOAT;
90          }
91          else if (clazz.equals(Integer.TYPE) ) {
92             return Type.INT;
93          }
94          else if (clazz.equals(Long.TYPE) ) {
95             return Type.LONG;
96          }
97          else if (clazz.equals(Short.TYPE) ) {
98             return Type.SHORT;
99          }
100          else if (clazz.equals(Void.TYPE) ) {
101             return Type.VOID;
102          }
103
104          // should never get here
105
throw new UnreachableStatementException();
106       }
107
108       // if we get this far it is not a primitive
109
String JavaDoc name = clazz.getName();
110
111       if (clazz.isArray()) {
112          return Type.getType(name);
113       }
114       
115       return new ObjectType(name);
116    }
117    
118    /**
119     * Get the <code>org.apache.bcel.generic.Type</code> for an array of Classes
120     *
121     * @param classes a <code>Class[]</code> value
122     * @return a <code>Type[]</code> value
123     */

124    public static Type[] getTypes(Class JavaDoc[] classes) {
125       Type[] types = new Type[classes.length];
126
127       for (int i = 0; i < classes.length; i++) {
128          types[i] = getType(classes[i]);
129       }
130
131       return types;
132    }
133
134    /**
135     * Get the Object equivalent Class name for a primitive
136     *
137     * <pre>
138     * e.g
139     *
140     * int <-> java.lang.Integer
141     * char <-> Character
142     *
143     * </pre>
144     *
145     * @param t a <code>BasicType</code> value
146     * @return a <code>String</code> value
147     *
148     * @throws IllegalArgumentException Unexpected type
149     */

150    public static String JavaDoc getObjectEquivalentClassName(BasicType t) {
151       switch (t.getType()) {
152       case Constants.T_INT:
153          return "java.lang.Integer";
154
155       case Constants.T_SHORT:
156          return "java.lang.Short";
157
158       case Constants.T_BOOLEAN:
159          return "java.lang.Boolean";
160
161       case Constants.T_CHAR:
162          return "java.lang.Character";
163
164       case Constants.T_BYTE:
165          return "java.lang.Byte";
166
167       case Constants.T_FLOAT:
168          return "java.lang.Float";
169
170       case Constants.T_DOUBLE:
171          return "java.lang.Double";
172
173       case Constants.T_LONG:
174          return "java.lang.Long";
175
176       default:
177          throw new IllegalArgumentException JavaDoc("Unexpected Type: " + t);
178       }
179    }
180       
181 }
182
Popular Tags