001 // Copyright 2004, 2005 The Apache Software Foundation
002 //
003 // Licensed under the Apache License, Version 2.0 (the "License");
004 // you may not use this file except in compliance with the License.
005 // You may obtain a copy of the License at
006 //
007 // http://www.apache.org/licenses/LICENSE-2.0
008 //
009 // Unless required by applicable law or agreed to in writing, software
010 // distributed under the License is distributed on an "AS IS" BASIS,
011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012 // See the License for the specific language governing permissions and
013 // limitations under the License.
014
015 package org.apache.hivemind.impl;
016
017 import java.util.HashMap;
018 import java.util.Map;
019
020 /**
021 * Holds a utility method that converts java type names (as they might appear in source code) into
022 * JVM class names.
023 *
024 * @author Howard M. Lewis Ship
025 * @since 1.1
026 */
027 public class JavaTypeUtils
028 {
029 /**
030 * Mapping between a primitive type and its Java VM representation Used for the encoding of
031 * array types
032 */
033 private static Map PRIMITIVE_TYPE_CODES = new HashMap();
034
035 static
036 {
037 PRIMITIVE_TYPE_CODES.put("boolean", "Z");
038 PRIMITIVE_TYPE_CODES.put("short", "S");
039 PRIMITIVE_TYPE_CODES.put("int", "I");
040 PRIMITIVE_TYPE_CODES.put("long", "J");
041 PRIMITIVE_TYPE_CODES.put("float", "F");
042 PRIMITIVE_TYPE_CODES.put("double", "D");
043 PRIMITIVE_TYPE_CODES.put("char", "C");
044 PRIMITIVE_TYPE_CODES.put("byte", "B");
045 }
046
047 /**
048 * Map from Java type name to Class.
049 */
050 private static final Map PRIMITIVE_CLASSES = new HashMap();
051
052 static
053 {
054 PRIMITIVE_CLASSES.put("boolean", boolean.class);
055 PRIMITIVE_CLASSES.put("short", short.class);
056 PRIMITIVE_CLASSES.put("char", char.class);
057 PRIMITIVE_CLASSES.put("byte", byte.class);
058 PRIMITIVE_CLASSES.put("int", int.class);
059 PRIMITIVE_CLASSES.put("long", long.class);
060 PRIMITIVE_CLASSES.put("float", float.class);
061 PRIMITIVE_CLASSES.put("double", double.class);
062 }
063
064 private JavaTypeUtils()
065 {
066 // Prevent instantiation
067 }
068
069 /**
070 * Translates types from standard Java format to Java VM format. For example, java.util.Locale
071 * remains java.util.Locale, but int[][] is translated to [[I and java.lang.Object[] to
072 * [Ljava.lang.Object;
073 */
074 public static String getJVMClassName(String type)
075 {
076 // if it is not an array, just return the type itself
077 if (!type.endsWith("[]"))
078 return type;
079
080 // if it is an array, convert it to JavaVM-style format
081 StringBuffer buffer = new StringBuffer();
082
083 while (type.endsWith("[]"))
084 {
085 buffer.append("[");
086 type = type.substring(0, type.length() - 2);
087 }
088
089 String primitiveIdentifier = (String) PRIMITIVE_TYPE_CODES.get(type);
090 if (primitiveIdentifier != null)
091 buffer.append(primitiveIdentifier);
092 else
093 {
094 buffer.append("L");
095 buffer.append(type);
096 buffer.append(";");
097 }
098
099 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 getPrimtiveClass(String type)
111 {
112 return (Class) PRIMITIVE_CLASSES.get(type);
113 }
114 }