KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > util > Classes


1 /*
2   Copyright (C) 2001 Laurent Martelli <laurent@aopsys.com>
3
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU Lesser General Public License as
6   published by the Free Software Foundation; either version 2 of the
7   License, or (at your option) any later version.
8
9   This program is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12   GNU Lesser General Public License for more details.
13
14   You should have received a copy of the GNU Lesser General Public License
15   along with this program; if not, write to the Free Software
16   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */

17
18 package org.objectweb.jac.util;
19
20 import java.lang.reflect.Array JavaDoc;
21
22 public class Classes
23 {
24
25     /**
26      * Return the java.lang type wrapped for a primitive type. For
27      * instance, the wrapper type for int is java.lang.Integer.
28      *
29      * @param type the type you want the wrapper type for.
30      * @return the wrapper type for type if type is a primitive
31      * type, otherwise type itself.
32      */

33     public static Class JavaDoc getPrimitiveTypeWrapper(Class JavaDoc type) {
34         if (!type.isPrimitive())
35             return type;
36         if (type == int.class)
37             return Integer JavaDoc.class;
38         else if (type == long.class)
39             return Long JavaDoc.class;
40         else if (type == short.class)
41             return Short JavaDoc.class;
42         else if (type == float.class)
43             return Float JavaDoc.class;
44         else if (type == double.class)
45             return Double JavaDoc.class;
46         else if (type == boolean.class)
47             return Boolean JavaDoc.class;
48         else if (type == byte.class)
49             return Byte JavaDoc.class;
50         else if (type == void.class)
51             return Void JavaDoc.class;
52         else
53             return type;
54     }
55
56     /**
57      * Return the java.lang type wrapped for a primitive type. For
58      * instance, the wrapper type for int is java.lang.Integer.
59      *
60      * @param type the type you want the wrapper type for.
61      * @return the wrapper type for type if type is a primitive
62      * type, otherwise type itself.
63      */

64     public static String JavaDoc getPrimitiveTypeWrapper(String JavaDoc type) {
65         if (type == null)
66             return "java.lang.Void";
67         if (type.equals("int"))
68             return "java.lang.Integer";
69         else if (type.equals("long"))
70             return "java.lang.Long";
71         else if (type.equals("short"))
72             return "java.lang.Short";
73         else if (type.equals("float"))
74             return "java.lang.Float";
75         else if (type.equals("double"))
76             return "java.lang.Double";
77         else if (type.equals("boolean"))
78             return "java.lang.Boolean";
79         else if (type.equals("byte"))
80             return "java.lang.Byte";
81         else if (type.equals("void"))
82             return "java.lang.Void";
83         else
84             return type;
85     }
86
87
88     /**
89      * Return the java.lang type wrapped for a primitive type. For
90      * instance, the wrapper type for int is java.lang.Integer.
91      *
92      * @param type the type you want the wrapper type for.
93      * @return the wrapper type for type if type is a primitive
94      * type, otherwise type itself.
95      */

96     public static boolean isPrimitiveType(String JavaDoc type) {
97         return
98             type.equals("void") ||
99             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 JavaDoc[] convertPrimitiveArray(Object JavaDoc primitiveArray) {
112         Class JavaDoc type = primitiveArray.getClass().getComponentType();
113         int length = Array.getLength(primitiveArray);
114         Object JavaDoc[] result = (Object JavaDoc[])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 }
122
Popular Tags