1 30 package com.tc.asm.util; 31 32 import com.tc.asm.AnnotationVisitor; 33 import com.tc.asm.Type; 34 35 40 public class CheckAnnotationAdapter implements AnnotationVisitor { 41 42 private AnnotationVisitor av; 43 44 private boolean named; 45 46 private boolean end; 47 48 public CheckAnnotationAdapter(final AnnotationVisitor av) { 49 this(av, true); 50 } 51 52 CheckAnnotationAdapter( 53 final AnnotationVisitor av, 54 final boolean named) 55 { 56 this.av = av; 57 this.named = named; 58 } 59 60 public void visit(final String name, final Object value) { 61 checkEnd(); 62 checkName(name); 63 if (!(value instanceof Byte || value instanceof Boolean 64 || value instanceof Character || value instanceof Short 65 || value instanceof Integer || value instanceof Long 66 || value instanceof Float || value instanceof Double 67 || value instanceof String || value instanceof Type 68 || value instanceof byte[] || value instanceof boolean[] 69 || value instanceof char[] || value instanceof short[] 70 || value instanceof int[] || value instanceof long[] 71 || value instanceof float[] || value instanceof double[])) 72 { 73 throw new IllegalArgumentException ("Invalid annotation value"); 74 } 75 av.visit(name, value); 76 } 77 78 public void visitEnum( 79 final String name, 80 final String desc, 81 final String value) 82 { 83 checkEnd(); 84 checkName(name); 85 CheckMethodAdapter.checkDesc(desc, false); 86 if (value == null) { 87 throw new IllegalArgumentException ("Invalid enum value"); 88 } 89 av.visitEnum(name, desc, value); 90 } 91 92 public AnnotationVisitor visitAnnotation( 93 final String name, 94 final String desc) 95 { 96 checkEnd(); 97 checkName(name); 98 CheckMethodAdapter.checkDesc(desc, false); 99 return new CheckAnnotationAdapter(av.visitAnnotation(name, desc)); 100 } 101 102 public AnnotationVisitor visitArray(final String name) { 103 checkEnd(); 104 checkName(name); 105 return new CheckAnnotationAdapter(av.visitArray(name), false); 106 } 107 108 public void visitEnd() { 109 checkEnd(); 110 end = true; 111 av.visitEnd(); 112 } 113 114 private void checkEnd() { 115 if (end) { 116 throw new IllegalStateException ("Cannot call a visit method after visitEnd has been called"); 117 } 118 } 119 120 private void checkName(final String name) { 121 if (named && name == null) { 122 throw new IllegalArgumentException ("Annotation value name must not be null"); 123 } 124 } 125 } 126 | Popular Tags |