125 /**
126 * The visitor to which this adapter must delegate calls. May be
127 * <tt>null</tt>.
128 */
129 private final SignatureVisitor sv;
130
131 /**
132 * Creates a new {@link CheckSignatureAdapter} object. <i>Subclasses must
133 * not use this constructor</i>. Instead, they must use the
134 * {@link #CheckSignatureAdapter(int, int, SignatureVisitor)} version.
135 *
136 * @param type
137 * the type of signature to be checked. See
138 * {@link #CLASS_SIGNATURE}, {@link #METHOD_SIGNATURE} and
139 * {@link #TYPE_SIGNATURE}.
140 * @param sv
141 * the visitor to which this adapter must delegate calls. May be
142 * <tt>null</tt>.
143 */
144 public CheckSignatureAdapter(final int type, final SignatureVisitor sv) {
145 this(Opcodes.ASM5, type, sv);
146 }
147
148 /**
149 * Creates a new {@link CheckSignatureAdapter} object.
150 *
151 * @param api
152 * the ASM API version implemented by this visitor. Must be one
153 * of {@link Opcodes#ASM4} or {@link Opcodes#ASM5}.
154 * @param type
155 * the type of signature to be checked. See
156 * {@link #CLASS_SIGNATURE}, {@link #METHOD_SIGNATURE} and
157 * {@link #TYPE_SIGNATURE}.
158 * @param sv
159 * the visitor to which this adapter must delegate calls. May be
160 * <tt>null</tt>.
161 */
162 protected CheckSignatureAdapter(final int api, final int type,
163 final SignatureVisitor sv) {
164 super(api);
165 this.type = type;
166 this.state = EMPTY;
167 this.sv = sv;
168 }
169
170 // class and method signatures
171
172 @Override
173 public void visitFormalTypeParameter(final String name) {
174 if (type == TYPE_SIGNATURE
175 || (state != EMPTY && state != FORMAL && state != BOUND)) {
176 throw new IllegalStateException();
177 }
178 CheckMethodAdapter.checkIdentifier(name, "formal type parameter");
179 state = FORMAL;
180 if (sv != null) {
181 sv.visitFormalTypeParameter(name);
182 }
183 }
184
185 @Override
186 public SignatureVisitor visitClassBound() {
187 if (state != FORMAL) {
188 throw new IllegalStateException();
189 }
190 state = BOUND;
191 SignatureVisitor v = sv == null ? null : sv.visitClassBound();
192 return new CheckSignatureAdapter(TYPE_SIGNATURE, v);
193 }
194
195 @Override
196 public SignatureVisitor visitInterfaceBound() {
197 if (state != FORMAL && state != BOUND) {
198 throw new IllegalArgumentException();
267 if (descriptor == 'V') {
268 if (!canBeVoid) {
269 throw new IllegalArgumentException();
270 }
271 } else {
272 if ("ZCBSIFJD".indexOf(descriptor) == -1) {
273 throw new IllegalArgumentException();
274 }
275 }
276 state = SIMPLE_TYPE;
277 if (sv != null) {
278 sv.visitBaseType(descriptor);
279 }
280 }
281
282 @Override
283 public void visitTypeVariable(final String name) {
284 if (type != TYPE_SIGNATURE || state != EMPTY) {
285 throw new IllegalStateException();
286 }
287 CheckMethodAdapter.checkIdentifier(name, "type variable");
288 state = SIMPLE_TYPE;
289 if (sv != null) {
290 sv.visitTypeVariable(name);
291 }
292 }
293
294 @Override
295 public SignatureVisitor visitArrayType() {
296 if (type != TYPE_SIGNATURE || state != EMPTY) {
297 throw new IllegalStateException();
298 }
299 state = SIMPLE_TYPE;
300 SignatureVisitor v = sv == null ? null : sv.visitArrayType();
301 return new CheckSignatureAdapter(TYPE_SIGNATURE, v);
302 }
303
304 @Override
305 public void visitClassType(final String name) {
306 if (type != TYPE_SIGNATURE || state != EMPTY) {
307 throw new IllegalStateException();
308 }
309 CheckMethodAdapter.checkInternalName(name, "class name");
310 state = CLASS_TYPE;
311 if (sv != null) {
312 sv.visitClassType(name);
313 }
314 }
315
316 @Override
317 public void visitInnerClassType(final String name) {
318 if (state != CLASS_TYPE) {
319 throw new IllegalStateException();
320 }
321 CheckMethodAdapter.checkIdentifier(name, "inner class name");
322 if (sv != null) {
323 sv.visitInnerClassType(name);
324 }
325 }
326
327 @Override
328 public void visitTypeArgument() {
329 if (state != CLASS_TYPE) {
330 throw new IllegalStateException();
331 }
332 if (sv != null) {
333 sv.visitTypeArgument();
334 }
335 }
336
337 @Override
338 public SignatureVisitor visitTypeArgument(final char wildcard) {
339 if (state != CLASS_TYPE) {
340 throw new IllegalStateException();
341 }
342 if ("+-=".indexOf(wildcard) == -1) {
343 throw new IllegalArgumentException();
344 }
345 SignatureVisitor v = sv == null ? null : sv.visitTypeArgument(wildcard);
346 return new CheckSignatureAdapter(TYPE_SIGNATURE, v);
347 }
348
349 @Override
350 public void visitEnd() {
351 if (state != CLASS_TYPE) {
352 throw new IllegalStateException();
353 }
354 state = END;
355 if (sv != null) {
356 sv.visitEnd();
357 }
358 }
359 }
|
125 /**
126 * The visitor to which this adapter must delegate calls. May be
127 * <tt>null</tt>.
128 */
129 private final SignatureVisitor sv;
130
131 /**
132 * Creates a new {@link CheckSignatureAdapter} object. <i>Subclasses must
133 * not use this constructor</i>. Instead, they must use the
134 * {@link #CheckSignatureAdapter(int, int, SignatureVisitor)} version.
135 *
136 * @param type
137 * the type of signature to be checked. See
138 * {@link #CLASS_SIGNATURE}, {@link #METHOD_SIGNATURE} and
139 * {@link #TYPE_SIGNATURE}.
140 * @param sv
141 * the visitor to which this adapter must delegate calls. May be
142 * <tt>null</tt>.
143 */
144 public CheckSignatureAdapter(final int type, final SignatureVisitor sv) {
145 this(Opcodes.ASM6, type, sv);
146 }
147
148 /**
149 * Creates a new {@link CheckSignatureAdapter} object.
150 *
151 * @param api
152 * the ASM API version implemented by this visitor. Must be one
153 * of {@link Opcodes#ASM4}, {@link Opcodes#ASM5} or {@link Opcodes#ASM6}.
154 * @param type
155 * the type of signature to be checked. See
156 * {@link #CLASS_SIGNATURE}, {@link #METHOD_SIGNATURE} and
157 * {@link #TYPE_SIGNATURE}.
158 * @param sv
159 * the visitor to which this adapter must delegate calls. May be
160 * <tt>null</tt>.
161 */
162 protected CheckSignatureAdapter(final int api, final int type,
163 final SignatureVisitor sv) {
164 super(api);
165 this.type = type;
166 this.state = EMPTY;
167 this.sv = sv;
168 }
169
170 // class and method signatures
171
172 @Override
173 public void visitFormalTypeParameter(final String name) {
174 if (type == TYPE_SIGNATURE
175 || (state != EMPTY && state != FORMAL && state != BOUND)) {
176 throw new IllegalStateException();
177 }
178 checkIdentifier(name, "formal type parameter");
179 state = FORMAL;
180 if (sv != null) {
181 sv.visitFormalTypeParameter(name);
182 }
183 }
184
185 @Override
186 public SignatureVisitor visitClassBound() {
187 if (state != FORMAL) {
188 throw new IllegalStateException();
189 }
190 state = BOUND;
191 SignatureVisitor v = sv == null ? null : sv.visitClassBound();
192 return new CheckSignatureAdapter(TYPE_SIGNATURE, v);
193 }
194
195 @Override
196 public SignatureVisitor visitInterfaceBound() {
197 if (state != FORMAL && state != BOUND) {
198 throw new IllegalArgumentException();
267 if (descriptor == 'V') {
268 if (!canBeVoid) {
269 throw new IllegalArgumentException();
270 }
271 } else {
272 if ("ZCBSIFJD".indexOf(descriptor) == -1) {
273 throw new IllegalArgumentException();
274 }
275 }
276 state = SIMPLE_TYPE;
277 if (sv != null) {
278 sv.visitBaseType(descriptor);
279 }
280 }
281
282 @Override
283 public void visitTypeVariable(final String name) {
284 if (type != TYPE_SIGNATURE || state != EMPTY) {
285 throw new IllegalStateException();
286 }
287 checkIdentifier(name, "type variable");
288 state = SIMPLE_TYPE;
289 if (sv != null) {
290 sv.visitTypeVariable(name);
291 }
292 }
293
294 @Override
295 public SignatureVisitor visitArrayType() {
296 if (type != TYPE_SIGNATURE || state != EMPTY) {
297 throw new IllegalStateException();
298 }
299 state = SIMPLE_TYPE;
300 SignatureVisitor v = sv == null ? null : sv.visitArrayType();
301 return new CheckSignatureAdapter(TYPE_SIGNATURE, v);
302 }
303
304 @Override
305 public void visitClassType(final String name) {
306 if (type != TYPE_SIGNATURE || state != EMPTY) {
307 throw new IllegalStateException();
308 }
309 checkClassName(name, "class name");
310 state = CLASS_TYPE;
311 if (sv != null) {
312 sv.visitClassType(name);
313 }
314 }
315
316 @Override
317 public void visitInnerClassType(final String name) {
318 if (state != CLASS_TYPE) {
319 throw new IllegalStateException();
320 }
321 checkIdentifier(name, "inner class name");
322 if (sv != null) {
323 sv.visitInnerClassType(name);
324 }
325 }
326
327 @Override
328 public void visitTypeArgument() {
329 if (state != CLASS_TYPE) {
330 throw new IllegalStateException();
331 }
332 if (sv != null) {
333 sv.visitTypeArgument();
334 }
335 }
336
337 @Override
338 public SignatureVisitor visitTypeArgument(final char wildcard) {
339 if (state != CLASS_TYPE) {
340 throw new IllegalStateException();
341 }
342 if ("+-=".indexOf(wildcard) == -1) {
343 throw new IllegalArgumentException();
344 }
345 SignatureVisitor v = sv == null ? null : sv.visitTypeArgument(wildcard);
346 return new CheckSignatureAdapter(TYPE_SIGNATURE, v);
347 }
348
349 @Override
350 public void visitEnd() {
351 if (state != CLASS_TYPE) {
352 throw new IllegalStateException();
353 }
354 state = END;
355 if (sv != null) {
356 sv.visitEnd();
357 }
358 }
359
360 private void checkClassName(final String name, final String msg) {
361 if (name == null || name.length() == 0) {
362 throw new IllegalArgumentException("Invalid " + msg
363 + " (must not be null or empty)");
364 }
365 for (int i = 0; i < name.length(); ++i) {
366 if (".;[<>:".indexOf(name.charAt(i)) != -1) {
367 throw new IllegalArgumentException("Invalid " + msg
368 + " (must not contain . ; [ < > or :): " + name);
369 }
370 }
371 }
372
373 private void checkIdentifier(final String name, final String msg) {
374 if (name == null || name.length() == 0) {
375 throw new IllegalArgumentException("Invalid " + msg
376 + " (must not be null or empty)");
377 }
378 for (int i = 0; i < name.length(); ++i) {
379 if (".;[/<>:".indexOf(name.charAt(i)) != -1) {
380 throw new IllegalArgumentException("Invalid " + msg
381 + " (must not contain . ; [ / < > or :): " + name);
382 }
383 }
384 }
385 }
|