Tue, 13 Oct 2009 15:26:30 -0700
6891079: Compiler allows invalid binary literals 0b and oBL
Reviewed-by: darcy
1 /*
2 * Copyright 2008 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
26 package com.sun.tools.classfile;
28 import java.util.Arrays;
29 import java.util.HashSet;
30 import java.util.List;
31 import java.util.Set;
33 /*
34 * <p><b>This is NOT part of any API supported by Sun Microsystems. If
35 * you write code that depends on this, you do so at your own risk.
36 * This code and its internal interfaces are subject to change or
37 * deletion without notice.</b>
38 */
39 public abstract class Type {
40 protected Type() { }
41 public abstract <R,D> R accept(Visitor<R,D> visitor, D data);
43 protected static void append(StringBuilder sb, String prefix, List<? extends Type> types, String suffix) {
44 sb.append(prefix);
45 String sep = "";
46 for (Type t: types) {
47 sb.append(sep);
48 sb.append(t);
49 sep = ", ";
50 }
51 sb.append(suffix);
52 }
54 protected static void appendIfNotEmpty(StringBuilder sb, String prefix, List<? extends Type> types, String suffix) {
55 if (types != null && types.size() > 0)
56 append(sb, prefix, types, suffix);
57 }
59 public interface Visitor<R,P> {
60 R visitSimpleType(SimpleType type, P p);
61 R visitArrayType(ArrayType type, P p);
62 R visitMethodType(MethodType type, P p);
63 R visitClassSigType(ClassSigType type, P p);
64 R visitClassType(ClassType type, P p);
65 R visitInnerClassType(InnerClassType type, P p);
66 R visitTypeArgType(TypeArgType type, P p);
67 R visitWildcardType(WildcardType type, P p);
68 }
70 public static class SimpleType extends Type {
71 public SimpleType(String name) {
72 this.name = name;
73 }
75 public <R, D> R accept(Visitor<R, D> visitor, D data) {
76 return visitor.visitSimpleType(this, data);
77 }
79 public boolean isPrimitiveType() {
80 return primitiveTypes.contains(name);
81 }
82 // where
83 private static final Set<String> primitiveTypes = new HashSet<String>(Arrays.asList(
84 "boolean", "byte", "char", "double", "float", "int", "long", "short", "void"));
86 @Override
87 public String toString() {
88 return name;
89 }
91 public final String name;
92 }
94 public static class ArrayType extends Type {
95 public ArrayType(Type elemType) {
96 this.elemType = elemType;
97 }
99 public <R, D> R accept(Visitor<R, D> visitor, D data) {
100 return visitor.visitArrayType(this, data);
101 }
103 @Override
104 public String toString() {
105 return elemType + "[]";
106 }
108 public final Type elemType;
109 }
111 public static class MethodType extends Type {
112 public MethodType(List<? extends Type> argTypes, Type resultType) {
113 this(null, argTypes, resultType, null);
114 }
116 public MethodType(List<? extends Type> typeArgTypes,
117 List<? extends Type> argTypes,
118 Type returnType,
119 List<? extends Type> throwsTypes) {
120 this.typeArgTypes = typeArgTypes;
121 this.argTypes = argTypes;
122 this.returnType = returnType;
123 this.throwsTypes = throwsTypes;
124 }
126 public <R, D> R accept(Visitor<R, D> visitor, D data) {
127 return visitor.visitMethodType(this, data);
128 }
130 @Override
131 public String toString() {
132 StringBuilder sb = new StringBuilder();
133 appendIfNotEmpty(sb, "<", typeArgTypes, "> ");
134 sb.append(returnType);
135 append(sb, " (", argTypes, ")");
136 appendIfNotEmpty(sb, " throws ", throwsTypes, "");
137 return sb.toString();
138 }
140 public final List<? extends Type> typeArgTypes;
141 public final List<? extends Type> argTypes;
142 public final Type returnType;
143 public final List<? extends Type> throwsTypes;
144 }
146 public static class ClassSigType extends Type {
147 public ClassSigType(List<Type> typeArgTypes, Type superclassType, List<Type> superinterfaceTypes) {
148 this.typeArgTypes = typeArgTypes;
149 this.superclassType = superclassType;
150 this.superinterfaceTypes = superinterfaceTypes;
151 }
153 public <R, D> R accept(Visitor<R, D> visitor, D data) {
154 return visitor.visitClassSigType(this, data);
155 }
157 @Override
158 public String toString() {
159 StringBuilder sb = new StringBuilder();
160 appendIfNotEmpty(sb, "<", typeArgTypes, ">");
161 if (superclassType != null) {
162 sb.append(" extends ");
163 sb.append(superclassType);
164 }
165 appendIfNotEmpty(sb, " implements ", superinterfaceTypes, "");
166 return sb.toString();
167 }
169 public final List<Type> typeArgTypes;
170 public final Type superclassType;
171 public final List<Type> superinterfaceTypes;
172 }
174 public static class ClassType extends Type {
175 public ClassType(String name, List<Type> typeArgs) {
176 this.name = name;
177 this.typeArgs = typeArgs;
178 }
180 public <R, D> R accept(Visitor<R, D> visitor, D data) {
181 return visitor.visitClassType(this, data);
182 }
184 @Override
185 public String toString() {
186 StringBuilder sb = new StringBuilder();
187 sb.append(name);
188 appendIfNotEmpty(sb, "<", typeArgs, ">");
189 return sb.toString();
190 }
192 public final String name;
193 public final List<Type> typeArgs;
194 }
197 public static class InnerClassType extends Type {
198 public InnerClassType(Type outerType, Type innerType) {
199 this.outerType = outerType;
200 this.innerType = innerType;
201 }
203 public <R, D> R accept(Visitor<R, D> visitor, D data) {
204 return visitor.visitInnerClassType(this, data);
205 }
207 @Override
208 public String toString() {
209 return outerType + "." + innerType;
210 }
212 public final Type outerType;
213 public final Type innerType;
214 }
216 public static class TypeArgType extends Type {
217 public TypeArgType(String name, Type classBound, List<Type> interfaceBounds) {
218 this.name = name;
219 this.classBound = classBound;
220 this.interfaceBounds = interfaceBounds;
221 }
223 public <R, D> R accept(Visitor<R, D> visitor, D data) {
224 return visitor.visitTypeArgType(this, data);
225 }
227 @Override
228 public String toString() {
229 StringBuilder sb = new StringBuilder();
230 sb.append(name);
231 String sep = " extends ";
232 if (classBound != null) {
233 sb.append(sep);
234 sb.append(classBound);
235 sep = " & ";
236 }
237 if (interfaceBounds != null) {
238 for (Type bound: interfaceBounds) {
239 sb.append(sep);
240 sb.append(bound);
241 sep = " & ";
242 }
243 }
244 return sb.toString();
245 }
247 public final String name;
248 public final Type classBound;
249 public final List<Type> interfaceBounds;
250 }
252 public static class WildcardType extends Type {
253 public WildcardType() {
254 this(null, null);
255 }
257 public WildcardType(String kind, Type boundType) {
258 this.kind = kind;
259 this.boundType = boundType;
260 }
262 public <R, D> R accept(Visitor<R, D> visitor, D data) {
263 return visitor.visitWildcardType(this, data);
264 }
266 @Override
267 public String toString() {
268 if (kind == null)
269 return "?";
270 else
271 return "? " + kind + " " + boundType;
272 }
274 public final String kind;
275 public final Type boundType;
276 }
277 }