Source code: com/puppycrawl/tools/checkstyle/checks/usage/transmogrify/LiteralResolver.java
1
2 // Transmogrify License
3 //
4 // Copyright (c) 2001, ThoughtWorks, Inc.
5 // All rights reserved.
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions
8 // are met:
9 // - Redistributions of source code must retain the above copyright notice,
10 // this list of conditions and the following disclaimer.
11 // - Redistributions in binary form must reproduce the above copyright
12 // notice, this list of conditions and the following disclaimer in the
13 // documentation and/or other materials provided with the distribution.
14 // Neither the name of the ThoughtWorks, Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from this
16 // software without specific prior written permission.
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
21 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29 package com.puppycrawl.tools.checkstyle.checks.usage.transmogrify;
30
31 import java.util.HashMap;
32 import java.util.Map;
33
34 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
35
36 /**
37 * Resolves primitive identifiers (int, double) to their corresponding
38 * <code>ClassDef</code>. This class uses the Singleton pattern.
39 *
40 * @author <a href="mailto:smileyy@thoughtworks.com">andrew</a>
41 * @version 1.0
42 * @since 1.0
43 */
44
45 public class LiteralResolver {
46
47 private static Map intMap;
48 private static Map nameMap;
49 private static Map classMap;
50
51 static {
52 nameMap = new HashMap();
53
54 nameMap.put("boolean", new ExternalClass(Boolean.TYPE));
55 nameMap.put("byte", new ExternalClass(Byte.TYPE));
56 nameMap.put("char", new ExternalClass(Character.TYPE));
57 nameMap.put("short", new ExternalClass(Short.TYPE));
58 nameMap.put("int", new ExternalClass(Integer.TYPE));
59 nameMap.put("float", new ExternalClass(Float.TYPE));
60 nameMap.put("long", new ExternalClass(Long.TYPE));
61 nameMap.put("double", new ExternalClass(Double.TYPE));
62
63 intMap = new HashMap();
64
65 intMap.put(
66 new Integer(TokenTypes.LITERAL_BOOLEAN),
67 new ExternalClass(Boolean.TYPE));
68 intMap.put(
69 new Integer(TokenTypes.LITERAL_BYTE),
70 new ExternalClass(Byte.TYPE));
71 intMap.put(
72 new Integer(TokenTypes.LITERAL_CHAR),
73 new ExternalClass(Character.TYPE));
74 intMap.put(
75 new Integer(TokenTypes.LITERAL_SHORT),
76 new ExternalClass(Short.TYPE));
77 intMap.put(
78 new Integer(TokenTypes.LITERAL_INT),
79 new ExternalClass(Integer.TYPE));
80 intMap.put(
81 new Integer(TokenTypes.LITERAL_FLOAT),
82 new ExternalClass(Float.TYPE));
83 intMap.put(
84 new Integer(TokenTypes.LITERAL_LONG),
85 new ExternalClass(Long.TYPE));
86 intMap.put(
87 new Integer(TokenTypes.LITERAL_DOUBLE),
88 new ExternalClass(Double.TYPE));
89 intMap.put(
90 new Integer(TokenTypes.STRING_LITERAL),
91 new ExternalClass("".getClass()));
92
93 classMap = new HashMap();
94 classMap.put(
95 new ExternalClass(Boolean.TYPE),
96 SymTabASTFactory.create(TokenTypes.LITERAL_BOOLEAN, "boolean"));
97 classMap.put(
98 new ExternalClass(Byte.TYPE),
99 SymTabASTFactory.create(TokenTypes.LITERAL_BYTE, "byte"));
100 classMap.put(
101 new ExternalClass(Character.TYPE),
102 SymTabASTFactory.create(TokenTypes.LITERAL_CHAR, "char"));
103 classMap.put(
104 new ExternalClass(Short.TYPE),
105 SymTabASTFactory.create(TokenTypes.LITERAL_SHORT, "short"));
106 classMap.put(
107 new ExternalClass(Integer.TYPE),
108 SymTabASTFactory.create(TokenTypes.LITERAL_INT, "int"));
109 classMap.put(
110 new ExternalClass(Float.TYPE),
111 SymTabASTFactory.create(TokenTypes.LITERAL_FLOAT, "float"));
112 classMap.put(
113 new ExternalClass(Long.TYPE),
114 SymTabASTFactory.create(TokenTypes.LITERAL_LONG, "long"));
115 classMap.put(
116 new ExternalClass(Double.TYPE),
117 SymTabASTFactory.create(TokenTypes.LITERAL_DOUBLE, "double"));
118
119 }
120
121 /**
122 * Returns a <code>LiteralResolver</code>
123 *
124 * @return a <code>LiteralResolver</code>
125 */
126
127 public static LiteralResolver getResolver() {
128 return new LiteralResolver();
129 }
130
131 /**
132 * Returns the <code>ClassDef</code> for a primitive type reference.
133 *
134 * <p>
135 * We could probably do without passing in the context, if we could figure
136 * out a way to access the base scope.
137 * </p>
138 *
139 * @param literalType the JavaTokenType for the literal type
140 * @param context the scope in which the search performed
141 * @return returns the <code>ClassDef</code>corresponding to the primitive
142 * type
143 */
144
145 public static IClass getDefinition(int literalType) {
146 Integer key = new Integer(literalType);
147 return (IClass) intMap.get(key);
148 }
149
150 public static IClass getDefinition(String name) {
151 return (IClass) nameMap.get(name);
152 }
153
154 public static SymTabAST getASTNode(IClass primitive) {
155 return (SymTabAST) classMap.get(primitive);
156 }
157 }