61 import jdk.internal.org.objectweb.asm.AnnotationVisitor; 62 import jdk.internal.org.objectweb.asm.Opcodes; 63 import jdk.internal.org.objectweb.asm.Type; 64 65 /** 66 * An {@link AnnotationVisitor} that checks that its methods are properly used. 67 * 68 * @author Eric Bruneton 69 */ 70 public class CheckAnnotationAdapter extends AnnotationVisitor { 71 72 private final boolean named; 73 74 private boolean end; 75 76 public CheckAnnotationAdapter(final AnnotationVisitor av) { 77 this(av, true); 78 } 79 80 CheckAnnotationAdapter(final AnnotationVisitor av, final boolean named) { 81 super(Opcodes.ASM4, av); 82 this.named = named; 83 } 84 85 @Override 86 public void visit(final String name, final Object value) { 87 checkEnd(); 88 checkName(name); 89 if (!(value instanceof Byte || value instanceof Boolean 90 || value instanceof Character || value instanceof Short 91 || value instanceof Integer || value instanceof Long 92 || value instanceof Float || value instanceof Double 93 || value instanceof String || value instanceof Type 94 || value instanceof byte[] || value instanceof boolean[] 95 || value instanceof char[] || value instanceof short[] 96 || value instanceof int[] || value instanceof long[] 97 || value instanceof float[] || value instanceof double[])) 98 { 99 throw new IllegalArgumentException("Invalid annotation value"); 100 } 101 if (value instanceof Type) { 102 int sort = ((Type) value).getSort(); 103 if (sort != Type.OBJECT && sort != Type.ARRAY) { 104 throw new IllegalArgumentException("Invalid annotation value"); 105 } 106 } 107 if (av != null) { 108 av.visit(name, value); 109 } 110 } 111 112 @Override 113 public void visitEnum( 114 final String name, 115 final String desc, 116 final String value) 117 { 118 checkEnd(); 119 checkName(name); 120 CheckMethodAdapter.checkDesc(desc, false); 121 if (value == null) { 122 throw new IllegalArgumentException("Invalid enum value"); 123 } 124 if (av != null) { 125 av.visitEnum(name, desc, value); 126 } 127 } 128 129 @Override 130 public AnnotationVisitor visitAnnotation( 131 final String name, 132 final String desc) 133 { 134 checkEnd(); 135 checkName(name); 136 CheckMethodAdapter.checkDesc(desc, false); 137 return new CheckAnnotationAdapter(av == null 138 ? null 139 : av.visitAnnotation(name, desc)); 140 } 141 142 @Override 143 public AnnotationVisitor visitArray(final String name) { 144 checkEnd(); 145 checkName(name); 146 return new CheckAnnotationAdapter(av == null 147 ? null 148 : av.visitArray(name), false); 149 } 150 151 @Override 152 public void visitEnd() { 153 checkEnd(); 154 end = true; 155 if (av != null) { 156 av.visitEnd(); 157 } 158 } 159 160 private void checkEnd() { 161 if (end) { 162 throw new IllegalStateException("Cannot call a visit method after visitEnd has been called"); 163 } 164 } 165 166 private void checkName(final String name) { 167 if (named && name == null) { 168 throw new IllegalArgumentException("Annotation value name must not be null"); 169 } 170 } 171 } | 61 import jdk.internal.org.objectweb.asm.AnnotationVisitor; 62 import jdk.internal.org.objectweb.asm.Opcodes; 63 import jdk.internal.org.objectweb.asm.Type; 64 65 /** 66 * An {@link AnnotationVisitor} that checks that its methods are properly used. 67 * 68 * @author Eric Bruneton 69 */ 70 public class CheckAnnotationAdapter extends AnnotationVisitor { 71 72 private final boolean named; 73 74 private boolean end; 75 76 public CheckAnnotationAdapter(final AnnotationVisitor av) { 77 this(av, true); 78 } 79 80 CheckAnnotationAdapter(final AnnotationVisitor av, final boolean named) { 81 super(Opcodes.ASM5, av); 82 this.named = named; 83 } 84 85 @Override 86 public void visit(final String name, final Object value) { 87 checkEnd(); 88 checkName(name); 89 if (!(value instanceof Byte || value instanceof Boolean 90 || value instanceof Character || value instanceof Short 91 || value instanceof Integer || value instanceof Long 92 || value instanceof Float || value instanceof Double 93 || value instanceof String || value instanceof Type 94 || value instanceof byte[] || value instanceof boolean[] 95 || value instanceof char[] || value instanceof short[] 96 || value instanceof int[] || value instanceof long[] 97 || value instanceof float[] || value instanceof double[])) { 98 throw new IllegalArgumentException("Invalid annotation value"); 99 } 100 if (value instanceof Type) { 101 int sort = ((Type) value).getSort(); 102 if (sort != Type.OBJECT && sort != Type.ARRAY) { 103 throw new IllegalArgumentException("Invalid annotation value"); 104 } 105 } 106 if (av != null) { 107 av.visit(name, value); 108 } 109 } 110 111 @Override 112 public void visitEnum(final String name, final String desc, 113 final String value) { 114 checkEnd(); 115 checkName(name); 116 CheckMethodAdapter.checkDesc(desc, false); 117 if (value == null) { 118 throw new IllegalArgumentException("Invalid enum value"); 119 } 120 if (av != null) { 121 av.visitEnum(name, desc, value); 122 } 123 } 124 125 @Override 126 public AnnotationVisitor visitAnnotation(final String name, 127 final String desc) { 128 checkEnd(); 129 checkName(name); 130 CheckMethodAdapter.checkDesc(desc, false); 131 return new CheckAnnotationAdapter(av == null ? null 132 : av.visitAnnotation(name, desc)); 133 } 134 135 @Override 136 public AnnotationVisitor visitArray(final String name) { 137 checkEnd(); 138 checkName(name); 139 return new CheckAnnotationAdapter(av == null ? null 140 : av.visitArray(name), false); 141 } 142 143 @Override 144 public void visitEnd() { 145 checkEnd(); 146 end = true; 147 if (av != null) { 148 av.visitEnd(); 149 } 150 } 151 152 private void checkEnd() { 153 if (end) { 154 throw new IllegalStateException( 155 "Cannot call a visit method after visitEnd has been called"); 156 } 157 } 158 159 private void checkName(final String name) { 160 if (named && name == null) { 161 throw new IllegalArgumentException( 162 "Annotation value name must not be null"); 163 } 164 } 165 } |