001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.proxy.compiler;
023:
024: import org.apache.bcel.Constants;
025: import org.apache.bcel.generic.BasicType;
026: import org.apache.bcel.generic.ObjectType;
027: import org.apache.bcel.generic.Type;
028:
029: import org.jboss.util.UnreachableStatementException;
030:
031: /**
032: * Some Routines to convert from <code>java.lang.Class</code> to
033: * <code>org.apache.bcel.generic.Type</code>. These get round some
034: * inconsistencies with Class.getName() wrt primitives.
035: *
036: * <pre>
037: * e.g.
038: *
039: * <code>Character.Type.getName()</code> returns char.
040: *
041: * </pre>
042: *
043: * <p>
044: * I think it should return C. Don't know if this is a code bug. But there's a bug
045: * on Bug Parade (#4369208) about the javadoc being misleading.
046: *
047: * @see java.lang.Class#getName
048: *
049: * @version <tt>$Revision: 57209 $</tt>
050: * @author <a href="mailto:neale@isismanor.co.uk">Neale Swinnerton</a>
051: * @author <a href="mailto:jason@planet57.com">Jason Dillon</a>
052: */
053: public abstract class Utility extends org.apache.bcel.classfile.Utility {
054: /**
055: * Get the <code>org.apache.bcel.generic.Type</code> for a class.
056: * This handles the case where the class represents an n-dimensional
057: * array by relying on the fact that <code>Class.getName()</code>
058: * on an array returns the signature
059: *
060: * <pre>
061: * e.g.
062: *
063: * <code>new Object[].getClass().getName()</code> returns [Ljava.lang.Object;
064: *
065: * </pre>
066: *
067: * @see Utility
068: *
069: * @param clazz a <code>Class</code> value
070: * @return a <code>Type</code> value
071: */
072: public static Type getType(Class clazz) {
073: if (clazz.isPrimitive()) {
074: if (clazz.equals(Boolean.TYPE)) {
075: return Type.BOOLEAN;
076: } else if (clazz.equals(Byte.TYPE)) {
077: return Type.BYTE;
078: } else if (clazz.equals(Character.TYPE)) {
079: return Type.CHAR;
080: } else if (clazz.equals(Double.TYPE)) {
081: return Type.DOUBLE;
082: } else if (clazz.equals(Float.TYPE)) {
083: return Type.FLOAT;
084: } else if (clazz.equals(Integer.TYPE)) {
085: return Type.INT;
086: } else if (clazz.equals(Long.TYPE)) {
087: return Type.LONG;
088: } else if (clazz.equals(Short.TYPE)) {
089: return Type.SHORT;
090: } else if (clazz.equals(Void.TYPE)) {
091: return Type.VOID;
092: }
093:
094: // should never get here
095: throw new UnreachableStatementException();
096: }
097:
098: // if we get this far it is not a primitive
099: String name = clazz.getName();
100:
101: if (clazz.isArray()) {
102: return Type.getType(name);
103: }
104:
105: return new ObjectType(name);
106: }
107:
108: /**
109: * Get the <code>org.apache.bcel.generic.Type</code> for an array of Classes
110: *
111: * @param classes a <code>Class[]</code> value
112: * @return a <code>Type[]</code> value
113: */
114: public static Type[] getTypes(Class[] classes) {
115: Type[] types = new Type[classes.length];
116:
117: for (int i = 0; i < classes.length; i++) {
118: types[i] = getType(classes[i]);
119: }
120:
121: return types;
122: }
123:
124: /**
125: * Get the Object equivalent Class name for a primitive
126: *
127: * <pre>
128: * e.g
129: *
130: * int <-> java.lang.Integer
131: * char <-> Character
132: *
133: * </pre>
134: *
135: * @param t a <code>BasicType</code> value
136: * @return a <code>String</code> value
137: *
138: * @throws IllegalArgumentException Unexpected type
139: */
140: public static String getObjectEquivalentClassName(BasicType t) {
141: switch (t.getType()) {
142: case Constants.T_INT:
143: return "java.lang.Integer";
144:
145: case Constants.T_SHORT:
146: return "java.lang.Short";
147:
148: case Constants.T_BOOLEAN:
149: return "java.lang.Boolean";
150:
151: case Constants.T_CHAR:
152: return "java.lang.Character";
153:
154: case Constants.T_BYTE:
155: return "java.lang.Byte";
156:
157: case Constants.T_FLOAT:
158: return "java.lang.Float";
159:
160: case Constants.T_DOUBLE:
161: return "java.lang.Double";
162:
163: case Constants.T_LONG:
164: return "java.lang.Long";
165:
166: default:
167: throw new IllegalArgumentException("Unexpected Type: " + t);
168: }
169: }
170:
171: }
|