|
|||||||||||||||||||
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
JavaTypeUtils.java | 100% | 100% | 66.7% | 97.4% |
|
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;
|
|
18 |
import java.util.Map;
|
|
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 PRIMITIVE_TYPE_CODES = new HashMap(); |
|
34 |
|
|
35 |
static
|
|
36 |
{ |
|
37 | 1 |
PRIMITIVE_TYPE_CODES.put("boolean", "Z"); |
38 | 1 |
PRIMITIVE_TYPE_CODES.put("short", "S"); |
39 | 1 |
PRIMITIVE_TYPE_CODES.put("int", "I"); |
40 | 1 |
PRIMITIVE_TYPE_CODES.put("long", "J"); |
41 | 1 |
PRIMITIVE_TYPE_CODES.put("float", "F"); |
42 | 1 |
PRIMITIVE_TYPE_CODES.put("double", "D"); |
43 | 1 |
PRIMITIVE_TYPE_CODES.put("char", "C"); |
44 | 1 |
PRIMITIVE_TYPE_CODES.put("byte", "B"); |
45 |
} |
|
46 |
|
|
47 |
/**
|
|
48 |
* Map from Java type name to Class.
|
|
49 |
*/
|
|
50 |
private static final Map PRIMITIVE_CLASSES = new HashMap(); |
|
51 |
|
|
52 |
static
|
|
53 |
{ |
|
54 | 1 |
PRIMITIVE_CLASSES.put("boolean", boolean.class); |
55 | 1 |
PRIMITIVE_CLASSES.put("short", short.class); |
56 | 1 |
PRIMITIVE_CLASSES.put("char", char.class); |
57 | 1 |
PRIMITIVE_CLASSES.put("byte", byte.class); |
58 | 1 |
PRIMITIVE_CLASSES.put("int", int.class); |
59 | 1 |
PRIMITIVE_CLASSES.put("long", long.class); |
60 | 1 |
PRIMITIVE_CLASSES.put("float", float.class); |
61 | 1 |
PRIMITIVE_CLASSES.put("double", double.class); |
62 |
} |
|
63 |
|
|
64 | 0 |
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 | 15828 |
public static String getJVMClassName(String type) |
75 |
{ |
|
76 |
// if it is not an array, just return the type itself
|
|
77 | 15828 |
if (!type.endsWith("[]")) |
78 | 15820 |
return type;
|
79 |
|
|
80 |
// if it is an array, convert it to JavaVM-style format
|
|
81 | 8 |
StringBuffer buffer = new StringBuffer();
|
82 |
|
|
83 | 8 |
while (type.endsWith("[]")) |
84 |
{ |
|
85 | 12 |
buffer.append("[");
|
86 | 12 |
type = type.substring(0, type.length() - 2); |
87 |
} |
|
88 |
|
|
89 | 8 |
String primitiveIdentifier = (String) PRIMITIVE_TYPE_CODES.get(type); |
90 | 8 |
if (primitiveIdentifier != null) |
91 | 4 |
buffer.append(primitiveIdentifier); |
92 |
else
|
|
93 |
{ |
|
94 | 4 |
buffer.append("L");
|
95 | 4 |
buffer.append(type); |
96 | 4 |
buffer.append(";");
|
97 |
} |
|
98 |
|
|
99 | 8 |
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 | 15826 |
public static Class getPrimtiveClass(String type) |
111 |
{ |
|
112 | 15826 |
return (Class) PRIMITIVE_CLASSES.get(type);
|
113 |
} |
|
114 |
} |
|