KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > interop > util > JavaType


1 /**
2  *
3  * Copyright 2004-2005 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.geronimo.interop.util;
19
20 import java.util.HashMap JavaDoc;
21
22
23 public abstract class JavaType {
24     private static HashMap JavaDoc _wrapper = new HashMap JavaDoc();
25
26     static {
27         _wrapper.put("boolean", "java.lang.Boolean");
28         _wrapper.put("byte", "java.lang.Byte");
29         _wrapper.put("char", "java.lang.Character");
30         _wrapper.put("double", "java.lang.Double");
31         _wrapper.put("float", "java.lang.Float");
32         _wrapper.put("int", "java.lang.Integer");
33         _wrapper.put("long", "java.lang.Long");
34         _wrapper.put("short", "java.lang.Short");
35     }
36
37     /**
38      * * Return the name of a type as would be referenced in source code,
39      * * e.g. "int", "byte[]", "java.lang.String", "java.lang.Object[][]".
40      */

41     public static String JavaDoc getName(Class JavaDoc t) {
42         if (t.isArray()) {
43             return getName(t.getComponentType()) + "[]";
44         } else {
45             return t.getName().replace('$', '.');
46         }
47     }
48
49     public static String JavaDoc wrapper(String JavaDoc type) {
50         return (String JavaDoc) _wrapper.get(type);
51     }
52
53     public static String JavaDoc wrapper(Class JavaDoc type) {
54         return wrapper(getName(type));
55     }
56
57     public static String JavaDoc wrap(String JavaDoc type, String JavaDoc name) {
58         String JavaDoc w = wrapper(type);
59         return w != null ? ("new " + w + "(" + name + ")") : name;
60     }
61
62     public static String JavaDoc wrap(Class JavaDoc type, String JavaDoc name) {
63         return wrap(getName(type), name);
64     }
65
66     public static String JavaDoc unwrap(String JavaDoc type, String JavaDoc name) {
67         String JavaDoc w = wrapper(type);
68         return w != null ? ("((" + w + ")" + name + ")." + type + "Value()") : name;
69     }
70
71     public static String JavaDoc unwrap(Class JavaDoc type, String JavaDoc name) {
72         return unwrap(getName(type), name);
73     }
74
75     public static String JavaDoc unwrapObject(Class JavaDoc type, Object JavaDoc expression) {
76         return unwrapObject(getName(type), expression);
77     }
78
79     public static String JavaDoc unwrapObject(String JavaDoc type, Object JavaDoc expression) {
80         String JavaDoc w = wrapper(type);
81         if (w == null) {
82             if (type.equals("java.lang.Object")) {
83                 return expression.toString();
84             } else {
85                 return "(" + type + ")" + expression;
86             }
87         } else {
88             return "((" + w + ")" + expression + ")." + type + "Value()";
89         }
90     }
91 }
92
Popular Tags