src/share/classes/com/sun/tools/classfile/Type.java

changeset 46
7708bd6d800d
child 65
0d4aa3c00af5
equal deleted inserted replaced
42:f7e64b33d5a4 46:7708bd6d800d
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 */
25
26 package com.sun.tools.classfile;
27
28 import java.util.List;
29
30 /*
31 * <p><b>This is NOT part of any API supported by Sun Microsystems. If
32 * you write code that depends on this, you do so at your own risk.
33 * This code and its internal interfaces are subject to change or
34 * deletion without notice.</b>
35 */
36 public class Type {
37 protected Type() { }
38
39 public boolean isObject() {
40 return false;
41 }
42
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 }
53
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 }
58
59 public static class SimpleType extends Type {
60 public SimpleType(String name) {
61 this.name = name;
62 }
63
64 @Override
65 public String toString() {
66 return name;
67 }
68
69 @Override
70 public boolean isObject() {
71 return name.equals("java.lang.Object");
72 }
73
74 public final String name;
75 }
76
77 public static class ArrayType extends Type {
78 public ArrayType(Type elemType) {
79 this.elemType = elemType;
80 }
81
82 @Override
83 public String toString() {
84 return elemType + "[]";
85 }
86
87 public final Type elemType;
88 }
89
90 public static class MethodType extends Type {
91 public MethodType(List<? extends Type> argTypes, Type resultType) {
92 this(null, argTypes, resultType, null);
93 }
94
95 public MethodType(List<? extends Type> typeArgTypes,
96 List<? extends Type> argTypes,
97 Type returnType,
98 List<? extends Type> throwsTypes) {
99 this.typeArgTypes = typeArgTypes;
100 this.argTypes = argTypes;
101 this.returnType = returnType;
102 this.throwsTypes = throwsTypes;
103 }
104
105 @Override
106 public String toString() {
107 StringBuilder sb = new StringBuilder();
108 appendIfNotEmpty(sb, "<", typeArgTypes, "> ");
109 sb.append(returnType);
110 append(sb, " (", argTypes, ")");
111 appendIfNotEmpty(sb, " throws ", throwsTypes, "");
112 return sb.toString();
113 }
114
115 public final List<? extends Type> typeArgTypes;
116 public final List<? extends Type> argTypes;
117 public final Type returnType;
118 public final List<? extends Type> throwsTypes;
119 }
120
121 public static class ClassSigType extends Type {
122 public ClassSigType(List<Type> typeArgTypes, Type superclassType, List<Type> superinterfaceTypes) {
123 this.typeArgTypes = typeArgTypes;
124 this.superclassType = superclassType;
125 this.superinterfaceTypes = superinterfaceTypes;
126 }
127
128 @Override
129 public String toString() {
130 StringBuilder sb = new StringBuilder();
131 appendIfNotEmpty(sb, "<", typeArgTypes, ">");
132 if (superclassType != null && !superclassType.isObject()) {
133 sb.append(" extends ");
134 sb.append(superclassType);
135 }
136 appendIfNotEmpty(sb, " implements ", superinterfaceTypes, "");
137 return sb.toString();
138 }
139
140 public final List<Type> typeArgTypes;
141 public final Type superclassType;
142 public final List<Type> superinterfaceTypes;
143 }
144
145 public static class ClassType extends Type {
146 public ClassType(String name, List<Type> typeArgs) {
147 this.name = name;
148 this.typeArgs = typeArgs;
149 }
150
151 @Override
152 public String toString() {
153 StringBuilder sb = new StringBuilder();
154 sb.append(name);
155 appendIfNotEmpty(sb, "<", typeArgs, ">");
156 return sb.toString();
157 }
158
159 public final String name;
160 public final List<Type> typeArgs;
161 }
162
163
164 public static class InnerClassType extends Type {
165 public InnerClassType(Type outerType, Type innerType) {
166 this.outerType = outerType;
167 this.innerType = innerType;
168 }
169
170 @Override
171 public String toString() {
172 return outerType + "." + innerType;
173 }
174
175 public final Type outerType;
176 public final Type innerType;
177 }
178
179 public static class TypeArgType extends Type {
180 public TypeArgType(String name, Type classBound, List<Type> interfaceBounds) {
181 this.name = name;
182 this.classBound = classBound;
183 this.interfaceBounds = interfaceBounds;
184 }
185
186 @Override
187 public String toString() {
188 StringBuilder sb = new StringBuilder();
189 sb.append(name);
190 String sep = " extends ";
191 if (classBound != null && !classBound.isObject()) {
192 sb.append(sep);
193 sb.append(classBound);
194 sep = " & ";
195 }
196 if (interfaceBounds != null) {
197 for (Type bound: interfaceBounds) {
198 sb.append(sep);
199 sb.append(bound);
200 sep = " & ";
201 }
202 }
203 return sb.toString();
204 }
205
206 public final String name;
207 public final Type classBound;
208 public final List<Type> interfaceBounds;
209 }
210
211 public static class WildcardType extends Type {
212 public WildcardType() {
213 this(null, null);
214 }
215
216 public WildcardType(String kind, Type boundType) {
217 this.kind = kind;
218 this.boundType = boundType;
219 }
220
221 @Override
222 public String toString() {
223 if (kind == null)
224 return "?";
225 else
226 return "? " + kind + " " + boundType;
227 }
228
229 public final String kind;
230 public final Type boundType;
231 }
232 }

mercurial