KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gcc > util > JavaType


1 /*
2  * Copyright 2004 The Apache Software Foundation or its licensors, as
3  * applicable.
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
14  * implied.
15  *
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */

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

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