001: /***
002: * ASM: a very small and fast Java bytecode manipulation framework
003: * Copyright (c) 2000-2005 INRIA, France Telecom
004: * All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: * 1. Redistributions of source code must retain the above copyright
010: * notice, this list of conditions and the following disclaimer.
011: * 2. Redistributions in binary form must reproduce the above copyright
012: * notice, this list of conditions and the following disclaimer in the
013: * documentation and/or other materials provided with the distribution.
014: * 3. Neither the name of the copyright holders nor the names of its
015: * contributors may be used to endorse or promote products derived from
016: * this software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
020: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
021: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
022: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
023: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
024: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
025: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
026: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
027: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
028: * THE POSSIBILITY OF SUCH DAMAGE.
029: */package org.drools.asm.util;
030:
031: import org.drools.asm.AnnotationVisitor;
032: import org.drools.asm.Type;
033:
034: /**
035: * An {@link AnnotationVisitor} that checks that its methods are properly used.
036: *
037: * @author Eric Bruneton
038: */
039: public class CheckAnnotationAdapter implements AnnotationVisitor {
040:
041: private AnnotationVisitor av;
042:
043: private boolean named;
044:
045: private boolean end;
046:
047: public CheckAnnotationAdapter(final AnnotationVisitor av) {
048: this (av, true);
049: }
050:
051: CheckAnnotationAdapter(final AnnotationVisitor av,
052: final boolean named) {
053: this .av = av;
054: this .named = named;
055: }
056:
057: public void visit(final String name, final Object value) {
058: checkEnd();
059: checkName(name);
060: if (!(value instanceof Byte || value instanceof Boolean
061: || value instanceof Character || value instanceof Short
062: || value instanceof Integer || value instanceof Long
063: || value instanceof Float || value instanceof Double
064: || value instanceof String || value instanceof Type
065: || value instanceof byte[]
066: || value instanceof boolean[]
067: || value instanceof char[] || value instanceof short[]
068: || value instanceof int[] || value instanceof long[]
069: || value instanceof float[] || value instanceof double[])) {
070: throw new IllegalArgumentException(
071: "Invalid annotation value");
072: }
073: this .av.visit(name, value);
074: }
075:
076: public void visitEnum(final String name, final String desc,
077: final String value) {
078: checkEnd();
079: checkName(name);
080: CheckMethodAdapter.checkDesc(desc, false);
081: if (value == null) {
082: throw new IllegalArgumentException("Invalid enum value");
083: }
084: this .av.visitEnum(name, desc, value);
085: }
086:
087: public AnnotationVisitor visitAnnotation(final String name,
088: final String desc) {
089: checkEnd();
090: checkName(name);
091: CheckMethodAdapter.checkDesc(desc, false);
092: return new CheckAnnotationAdapter(this .av.visitAnnotation(name,
093: desc));
094: }
095:
096: public AnnotationVisitor visitArray(final String name) {
097: checkEnd();
098: checkName(name);
099: return new CheckAnnotationAdapter(this .av.visitArray(name),
100: false);
101: }
102:
103: public void visitEnd() {
104: checkEnd();
105: this .end = true;
106: this .av.visitEnd();
107: }
108:
109: private void checkEnd() {
110: if (this .end) {
111: throw new IllegalStateException(
112: "Cannot call a visit method after visitEnd has been called");
113: }
114: }
115:
116: private void checkName(final String name) {
117: if (this .named && name == null) {
118: throw new IllegalArgumentException(
119: "Annotation value name must not be null");
120: }
121: }
122: }
|