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

changeset 427
6ba399eff2cb
parent 54
eaf608c64fec
child 554
9d9f26857129
equal deleted inserted replaced
426:d1e62f78c48b 427:6ba399eff2cb
25 25
26 package com.sun.tools.classfile; 26 package com.sun.tools.classfile;
27 27
28 import java.util.ArrayList; 28 import java.util.ArrayList;
29 import java.util.List; 29 import java.util.List;
30 import com.sun.tools.classfile.Type.*;
30 31
31 /** 32 /**
32 * See JVMS3 4.4.4. 33 * See JVMS3 4.4.4.
33 * 34 *
34 * <p><b>This is NOT part of any API supported by Sun Microsystems. If 35 * <p><b>This is NOT part of any API supported by Sun Microsystems. If
48 return type; 49 return type;
49 } 50 }
50 51
51 @Override 52 @Override
52 public int getParameterCount(ConstantPool constant_pool) throws ConstantPoolException { 53 public int getParameterCount(ConstantPool constant_pool) throws ConstantPoolException {
53 Type.MethodType m = (Type.MethodType) getType(constant_pool); 54 MethodType m = (MethodType) getType(constant_pool);
54 return m.argTypes.size(); 55 return m.paramTypes.size();
55 } 56 }
56 57
57 @Override 58 @Override
58 public String getParameterTypes(ConstantPool constant_pool) throws ConstantPoolException { 59 public String getParameterTypes(ConstantPool constant_pool) throws ConstantPoolException {
59 Type.MethodType m = (Type.MethodType) getType(constant_pool); 60 MethodType m = (MethodType) getType(constant_pool);
60 StringBuilder sb = new StringBuilder(); 61 StringBuilder sb = new StringBuilder();
61 sb.append("("); 62 sb.append("(");
62 String sep = ""; 63 String sep = "";
63 for (Type argType: m.argTypes) { 64 for (Type paramType: m.paramTypes) {
64 sb.append(sep); 65 sb.append(sep);
65 sb.append(argType); 66 sb.append(paramType);
66 sep = ", "; 67 sep = ", ";
67 } 68 }
68 sb.append(")"); 69 sb.append(")");
69 return sb.toString(); 70 return sb.toString();
70 } 71 }
71 72
72 @Override 73 @Override
73 public String getReturnType(ConstantPool constant_pool) throws ConstantPoolException { 74 public String getReturnType(ConstantPool constant_pool) throws ConstantPoolException {
74 Type.MethodType m = (Type.MethodType) getType(constant_pool); 75 MethodType m = (MethodType) getType(constant_pool);
75 return m.returnType.toString(); 76 return m.returnType.toString();
76 } 77 }
77 78
78 @Override 79 @Override
79 public String getFieldType(ConstantPool constant_pool) throws ConstantPoolException { 80 public String getFieldType(ConstantPool constant_pool) throws ConstantPoolException {
82 83
83 private Type parse(String sig) { 84 private Type parse(String sig) {
84 this.sig = sig; 85 this.sig = sig;
85 sigp = 0; 86 sigp = 0;
86 87
87 List<Type> typeArgTypes = null; 88 List<TypeParamType> typeParamTypes = null;
88 if (sig.charAt(sigp) == '<') 89 if (sig.charAt(sigp) == '<')
89 typeArgTypes = parseTypeArgTypes(); 90 typeParamTypes = parseTypeParamTypes();
90 91
91 if (sig.charAt(sigp) == '(') { 92 if (sig.charAt(sigp) == '(') {
92 List<Type> argTypes = parseTypeSignatures(')'); 93 List<Type> paramTypes = parseTypeSignatures(')');
93 Type returnType = parseTypeSignature(); 94 Type returnType = parseTypeSignature();
94 List<Type> throwsTypes = null; 95 List<Type> throwsTypes = null;
95 while (sigp < sig.length() && sig.charAt(sigp) == '^') { 96 while (sigp < sig.length() && sig.charAt(sigp) == '^') {
96 sigp++; 97 sigp++;
97 if (throwsTypes == null) 98 if (throwsTypes == null)
98 throwsTypes = new ArrayList<Type>(); 99 throwsTypes = new ArrayList<Type>();
99 throwsTypes.add(parseTypeSignature()); 100 throwsTypes.add(parseTypeSignature());
100 } 101 }
101 return new Type.MethodType(typeArgTypes, argTypes, returnType, throwsTypes); 102 return new MethodType(typeParamTypes, paramTypes, returnType, throwsTypes);
102 } else { 103 } else {
103 Type t = parseTypeSignature(); 104 Type t = parseTypeSignature();
104 if (typeArgTypes == null && sigp == sig.length()) 105 if (typeParamTypes == null && sigp == sig.length())
105 return t; 106 return t;
106 Type superclass = t; 107 Type superclass = t;
107 List<Type> superinterfaces = new ArrayList<Type>(); 108 List<Type> superinterfaces = null;
108 while (sigp < sig.length()) 109 while (sigp < sig.length()) {
110 if (superinterfaces == null)
111 superinterfaces = new ArrayList<Type>();
109 superinterfaces.add(parseTypeSignature()); 112 superinterfaces.add(parseTypeSignature());
110 return new Type.ClassSigType(typeArgTypes, superclass, superinterfaces); 113 }
114 return new ClassSigType(typeParamTypes, superclass, superinterfaces);
111 115
112 } 116 }
113 } 117 }
114 118
115 private Type parseTypeSignature() { 119 private Type parseTypeSignature() {
116 switch (sig.charAt(sigp)) { 120 switch (sig.charAt(sigp)) {
117 case 'B': 121 case 'B':
118 sigp++; 122 sigp++;
119 return new Type.SimpleType("byte"); 123 return new SimpleType("byte");
120 124
121 case 'C': 125 case 'C':
122 sigp++; 126 sigp++;
123 return new Type.SimpleType("char"); 127 return new SimpleType("char");
124 128
125 case 'D': 129 case 'D':
126 sigp++; 130 sigp++;
127 return new Type.SimpleType("double"); 131 return new SimpleType("double");
128 132
129 case 'F': 133 case 'F':
130 sigp++; 134 sigp++;
131 return new Type.SimpleType("float"); 135 return new SimpleType("float");
132 136
133 case 'I': 137 case 'I':
134 sigp++; 138 sigp++;
135 return new Type.SimpleType("int"); 139 return new SimpleType("int");
136 140
137 case 'J': 141 case 'J':
138 sigp++; 142 sigp++;
139 return new Type.SimpleType("long"); 143 return new SimpleType("long");
140 144
141 case 'L': 145 case 'L':
142 return parseClassTypeSignature(); 146 return parseClassTypeSignature();
143 147
144 case 'S': 148 case 'S':
145 sigp++; 149 sigp++;
146 return new Type.SimpleType("short"); 150 return new SimpleType("short");
147 151
148 case 'T': 152 case 'T':
149 return parseTypeVariableSignature(); 153 return parseTypeVariableSignature();
150 154
151 case 'V': 155 case 'V':
152 sigp++; 156 sigp++;
153 return new Type.SimpleType("void"); 157 return new SimpleType("void");
154 158
155 case 'Z': 159 case 'Z':
156 sigp++; 160 sigp++;
157 return new Type.SimpleType("boolean"); 161 return new SimpleType("boolean");
158 162
159 case '[': 163 case '[':
160 sigp++; 164 sigp++;
161 return new Type.ArrayType(parseTypeSignature()); 165 return new ArrayType(parseTypeSignature());
162 166
163 case '*': 167 case '*':
164 sigp++; 168 sigp++;
165 return new Type.WildcardType(); 169 return new WildcardType();
166 170
167 case '+': 171 case '+':
168 sigp++; 172 sigp++;
169 return new Type.WildcardType("extends", parseTypeSignature()); 173 return new WildcardType(WildcardType.Kind.EXTENDS, parseTypeSignature());
170 174
171 case '-': 175 case '-':
172 sigp++; 176 sigp++;
173 return new Type.WildcardType("super", parseTypeSignature()); 177 return new WildcardType(WildcardType.Kind.SUPER, parseTypeSignature());
174 178
175 default: 179 default:
176 throw new IllegalStateException(debugInfo()); 180 throw new IllegalStateException(debugInfo());
177 } 181 }
178 } 182 }
192 return parseClassTypeSignatureRest(); 196 return parseClassTypeSignatureRest();
193 } 197 }
194 198
195 private Type parseClassTypeSignatureRest() { 199 private Type parseClassTypeSignatureRest() {
196 StringBuilder sb = new StringBuilder(); 200 StringBuilder sb = new StringBuilder();
197 Type t = null; 201 List<Type> argTypes = null;
198 char sigch; 202 ClassType t = null;
199 while (true) { 203 char sigch ;
204
205 do {
200 switch (sigch = sig.charAt(sigp)) { 206 switch (sigch = sig.charAt(sigp)) {
201 case '/': 207 case '<':
202 sigp++; 208 argTypes = parseTypeSignatures('>');
203 sb.append(".");
204 break; 209 break;
205 210
206 case '.': 211 case '.':
207 sigp++;
208 if (t == null)
209 t = new Type.SimpleType(sb.toString());
210 return new Type.InnerClassType(t, parseClassTypeSignatureRest());
211
212 case ';': 212 case ';':
213 sigp++; 213 sigp++;
214 if (t == null) 214 t = new ClassType(t, sb.toString(), argTypes);
215 t = new Type.SimpleType(sb.toString()); 215 sb.setLength(0);
216 return t; 216 argTypes = null;
217
218 case '<':
219 List<Type> argTypes = parseTypeSignatures('>');
220 t = new Type.ClassType(sb.toString(), argTypes);
221 break; 217 break;
222 218
223 default: 219 default:
224 sigp++; 220 sigp++;
225 sb.append(sigch); 221 sb.append(sigch);
226 break; 222 break;
227 } 223 }
228 } 224 } while (sigch != ';');
229 } 225
230 226 return t;
231 private List<Type> parseTypeArgTypes() { 227 }
228
229 private List<TypeParamType> parseTypeParamTypes() {
232 assert sig.charAt(sigp) == '<'; 230 assert sig.charAt(sigp) == '<';
233 sigp++; 231 sigp++;
234 List<Type> types = null; 232 List<TypeParamType> types = new ArrayList<TypeParamType>();
235 types = new ArrayList<Type>();
236 while (sig.charAt(sigp) != '>') 233 while (sig.charAt(sigp) != '>')
237 types.add(parseTypeArgType()); 234 types.add(parseTypeParamType());
238 sigp++; 235 sigp++;
239 return types; 236 return types;
240 } 237 }
241 238
242 private Type parseTypeArgType() { 239 private TypeParamType parseTypeParamType() {
243 int sep = sig.indexOf(":", sigp); 240 int sep = sig.indexOf(":", sigp);
244 String name = sig.substring(sigp, sep); 241 String name = sig.substring(sigp, sep);
245 Type classBound = null; 242 Type classBound = null;
246 List<Type> interfaceBounds = null; 243 List<Type> interfaceBounds = null;
247 sigp = sep + 1; 244 sigp = sep + 1;
251 sigp++; 248 sigp++;
252 if (interfaceBounds == null) 249 if (interfaceBounds == null)
253 interfaceBounds = new ArrayList<Type>(); 250 interfaceBounds = new ArrayList<Type>();
254 interfaceBounds.add(parseTypeSignature()); 251 interfaceBounds.add(parseTypeSignature());
255 } 252 }
256 return new Type.TypeArgType(name, classBound, interfaceBounds); 253 return new TypeParamType(name, classBound, interfaceBounds);
257 } 254 }
258 255
259 private Type parseTypeVariableSignature() { 256 private Type parseTypeVariableSignature() {
260 sigp++; 257 sigp++;
261 int sep = sig.indexOf(';', sigp); 258 int sep = sig.indexOf(';', sigp);
262 Type t = new Type.SimpleType(sig.substring(sigp, sep)); 259 Type t = new SimpleType(sig.substring(sigp, sep));
263 sigp = sep + 1; 260 sigp = sep + 1;
264 return t; 261 return t;
265 } 262 }
266 263
267 private String debugInfo() { 264 private String debugInfo() {

mercurial