/* * JBoss, the OpenSource J2EE webOS * * Distributable under LGPL license. * See terms of license at gnu.org. */ package org.jboss.proxy.compiler; import org.apache.bcel.Constants; import org.apache.bcel.generic.BasicType; import org.apache.bcel.generic.ObjectType; import org.apache.bcel.generic.Type; import org.jboss.util.UnreachableStatementException; /** * Some Routines to convert from java.lang.Class to * org.apache.bcel.generic.Type. These get round some * inconsistencies with Class.getName() wrt primitives. * *
 * e.g. 
 *
 *  Character.Type.getName() returns char.
 *
 * 
* *

* I think it should return C. Don't know if this is a code bug. But there's a bug * on Bug Parade (#4369208) about the javadoc being misleading. * * @see java.lang.Class#getName * * @version $Revision: 1.2 $ * @author Neale Swinnerton * @author Jason Dillon */ public abstract class Utility extends org.apache.bcel.classfile.Utility { /** * Get the org.apache.bcel.generic.Type for a class. * This handles the case where the class represents an n-dimensional * array by relying on the fact that Class.getName() * on an array returns the signature * *

    * e.g.
    *
    *   new Object[].getClass().getName() returns [Ljava.lang.Object;
    *
    * 
* * @see Utility * * @param clazz a Class value * @return a Type value */ public static Type getType(Class clazz) { if (clazz.isPrimitive()) { if (clazz.equals(Boolean.TYPE) ) { return Type.BOOLEAN; } else if (clazz.equals(Byte.TYPE) ) { return Type.BYTE; } else if (clazz.equals(Character.TYPE) ) { return Type.CHAR; } else if (clazz.equals(Double.TYPE) ) { return Type.DOUBLE; } else if (clazz.equals(Float.TYPE) ) { return Type.FLOAT; } else if (clazz.equals(Integer.TYPE) ) { return Type.INT; } else if (clazz.equals(Long.TYPE) ) { return Type.LONG; } else if (clazz.equals(Short.TYPE) ) { return Type.SHORT; } else if (clazz.equals(Void.TYPE) ) { return Type.VOID; } // should never get here throw new UnreachableStatementException(); } // if we get this far it is not a primitive String name = clazz.getName(); if (clazz.isArray()) { return Type.getType(name); } return new ObjectType(name); } /** * Get the org.apache.bcel.generic.Type for an array of Classes * * @param classes a Class[] value * @return a Type[] value */ public static Type[] getTypes(Class[] classes) { Type[] types = new Type[classes.length]; for (int i = 0; i < classes.length; i++) { types[i] = getType(classes[i]); } return types; } /** * Get the Object equivalent Class name for a primitive * *
    * e.g
    *
    *   int   <-> java.lang.Integer
    *   char  <-> Character
    *
    * 
* * @param t a BasicType value * @return a String value * * @throws IllegalArgumentException Unexpected type */ public static String getObjectEquivalentClassName(BasicType t) { switch (t.getType()) { case Constants.T_INT: return "java.lang.Integer"; case Constants.T_SHORT: return "java.lang.Short"; case Constants.T_BOOLEAN: return "java.lang.Boolean"; case Constants.T_CHAR: return "java.lang.Character"; case Constants.T_BYTE: return "java.lang.Byte"; case Constants.T_FLOAT: return "java.lang.Float"; case Constants.T_DOUBLE: return "java.lang.Double"; case Constants.T_LONG: return "java.lang.Long"; default: throw new IllegalArgumentException("Unexpected Type: " + t); } } }