KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > hivemind > impl > JavaTypeUtils


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

15 package org.apache.hivemind.impl;
16
17 import java.util.HashMap JavaDoc;
18 import java.util.Map JavaDoc;
19
20 /**
21  * Holds a utility method that converts java type names (as they might appear in source code) into
22  * JVM class names.
23  *
24  * @author Howard M. Lewis Ship
25  * @since 1.1
26  */

27 public class JavaTypeUtils
28 {
29     /**
30      * Mapping between a primitive type and its Java VM representation Used for the encoding of
31      * array types
32      */

33     private static Map JavaDoc PRIMITIVE_TYPE_CODES = new HashMap JavaDoc();
34
35     static
36     {
37         PRIMITIVE_TYPE_CODES.put("boolean", "Z");
38         PRIMITIVE_TYPE_CODES.put("short", "S");
39         PRIMITIVE_TYPE_CODES.put("int", "I");
40         PRIMITIVE_TYPE_CODES.put("long", "J");
41         PRIMITIVE_TYPE_CODES.put("float", "F");
42         PRIMITIVE_TYPE_CODES.put("double", "D");
43         PRIMITIVE_TYPE_CODES.put("char", "C");
44         PRIMITIVE_TYPE_CODES.put("byte", "B");
45     }
46
47     /**
48      * Map from Java type name to Class.
49      */

50     private static final Map JavaDoc PRIMITIVE_CLASSES = new HashMap JavaDoc();
51
52     static
53     {
54         PRIMITIVE_CLASSES.put("boolean", boolean.class);
55         PRIMITIVE_CLASSES.put("short", short.class);
56         PRIMITIVE_CLASSES.put("char", char.class);
57         PRIMITIVE_CLASSES.put("byte", byte.class);
58         PRIMITIVE_CLASSES.put("int", int.class);
59         PRIMITIVE_CLASSES.put("long", long.class);
60         PRIMITIVE_CLASSES.put("float", float.class);
61         PRIMITIVE_CLASSES.put("double", double.class);
62     }
63
64     private JavaTypeUtils()
65     {
66         // Prevent instantiation
67
}
68
69     /**
70      * Translates types from standard Java format to Java VM format. For example, java.util.Locale
71      * remains java.util.Locale, but int[][] is translated to [[I and java.lang.Object[] to
72      * [Ljava.lang.Object;
73      */

74     public static String JavaDoc getJVMClassName(String JavaDoc type)
75     {
76         // if it is not an array, just return the type itself
77
if (!type.endsWith("[]"))
78             return type;
79
80         // if it is an array, convert it to JavaVM-style format
81
StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
82
83         while (type.endsWith("[]"))
84         {
85             buffer.append("[");
86             type = type.substring(0, type.length() - 2);
87         }
88
89         String JavaDoc primitiveIdentifier = (String JavaDoc) PRIMITIVE_TYPE_CODES.get(type);
90         if (primitiveIdentifier != null)
91             buffer.append(primitiveIdentifier);
92         else
93         {
94             buffer.append("L");
95             buffer.append(type);
96             buffer.append(";");
97         }
98
99         return buffer.toString();
100     }
101
102     /**
103      * Translates a primitive type ("boolean", "char", etc.) to the corresponding
104      * Class.
105      *
106      * @return the corresponding class, or null if type is not a primitive type.
107      *
108      */

109     
110     public static Class JavaDoc getPrimtiveClass(String JavaDoc type)
111     {
112         return (Class JavaDoc) PRIMITIVE_CLASSES.get(type);
113     }
114 }
Popular Tags