001    /*
002      Copyright (C) 2001 Laurent Martelli <laurent@aopsys.com>
003    
004      This program is free software; you can redistribute it and/or modify
005      it under the terms of the GNU Lesser General Public License as
006      published by the Free Software Foundation; either version 2 of the
007      License, or (at your option) any later version.
008    
009      This program is distributed in the hope that it will be useful,
010      but WITHOUT ANY WARRANTY; without even the implied warranty of
011      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012      GNU Lesser General Public License for more details.
013    
014      You should have received a copy of the GNU Lesser General Public License
015      along with this program; if not, write to the Free Software
016      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
017    
018    package org.objectweb.jac.util;
019    
020    import java.lang.reflect.Array;
021    
022    public class Classes 
023    {
024    
025        /**
026         * Return the java.lang type wrapped for a primitive type. For
027         * instance, the wrapper type for int is java.lang.Integer.
028         *
029         * @param type the type you want the wrapper type for.  
030         * @return the wrapper type for type if type is a primitive
031         * type, otherwise type itself.
032         */
033        public static Class getPrimitiveTypeWrapper(Class type) {
034            if (!type.isPrimitive()) 
035                return type;
036            if (type == int.class)
037                return Integer.class;
038            else if (type == long.class)
039                return Long.class;
040            else if (type == short.class)
041                return Short.class;
042            else if (type == float.class)
043                return Float.class;
044            else if (type == double.class)
045                return Double.class;
046            else if (type == boolean.class)
047                return Boolean.class;
048            else if (type == byte.class)
049                return Byte.class;
050            else if (type == void.class)
051                return Void.class;
052            else
053                return type;
054        }
055    
056        /**
057         * Return the java.lang type wrapped for a primitive type. For
058         * instance, the wrapper type for int is java.lang.Integer.
059         *
060         * @param type the type you want the wrapper type for.  
061         * @return the wrapper type for type if type is a primitive
062         * type, otherwise type itself.
063         */
064        public static String getPrimitiveTypeWrapper(String type) {
065            if (type == null) 
066                return "java.lang.Void";
067            if (type.equals("int"))
068                return "java.lang.Integer";
069            else if (type.equals("long"))
070                return "java.lang.Long";
071            else if (type.equals("short"))
072                return "java.lang.Short";
073            else if (type.equals("float"))
074                return "java.lang.Float";
075            else if (type.equals("double"))
076                return "java.lang.Double";
077            else if (type.equals("boolean"))
078                return "java.lang.Boolean";
079            else if (type.equals("byte"))
080                return "java.lang.Byte";
081            else if (type.equals("void"))
082                return "java.lang.Void";
083            else
084                return type;
085        }
086    
087    
088        /**
089         * Return the java.lang type wrapped for a primitive type. For
090         * instance, the wrapper type for int is java.lang.Integer.
091         *
092         * @param type the type you want the wrapper type for.  
093         * @return the wrapper type for type if type is a primitive
094         * type, otherwise type itself.
095         */
096        public static boolean isPrimitiveType(String type) {
097            return 
098                type.equals("void") || 
099                type.equals("int") || 
100                type.equals("long") || 
101                type.equals("short") || 
102                type.equals("float") || 
103                type.equals("double") || 
104                type.equals("boolean") || 
105                type.equals("byte");
106        }
107    
108        /**
109         * Convert an array of a primitive type to an array of Object.
110         */
111        public static Object[] convertPrimitiveArray(Object primitiveArray) {
112            Class type = primitiveArray.getClass().getComponentType();
113            int length = Array.getLength(primitiveArray);
114            Object[] result = (Object[])Array.newInstance(getPrimitiveTypeWrapper(type),
115                                                          length);
116            for (int i=0; i<length; i++) {
117                Array.set(result,i,Array.get(primitiveArray,i));
118            }
119            return result;
120        }
121    }