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 com.tc.asm.util;
030:
031: import com.tc.asm.AnnotationVisitor;
032: import com.tc.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: if (av != null) {
074: av.visit(name, value);
075: }
076: }
077:
078: public void visitEnum(final String name, final String desc,
079: final String value) {
080: checkEnd();
081: checkName(name);
082: CheckMethodAdapter.checkDesc(desc, false);
083: if (value == null) {
084: throw new IllegalArgumentException("Invalid enum value");
085: }
086: if (av != null) {
087: av.visitEnum(name, desc, value);
088: }
089: }
090:
091: public AnnotationVisitor visitAnnotation(final String name,
092: final String desc) {
093: checkEnd();
094: checkName(name);
095: CheckMethodAdapter.checkDesc(desc, false);
096: return new CheckAnnotationAdapter(av == null ? null : av
097: .visitAnnotation(name, desc));
098: }
099:
100: public AnnotationVisitor visitArray(final String name) {
101: checkEnd();
102: checkName(name);
103: return new CheckAnnotationAdapter(av == null ? null : av
104: .visitArray(name), false);
105: }
106:
107: public void visitEnd() {
108: checkEnd();
109: end = true;
110: if (av != null) {
111: av.visitEnd();
112: }
113: }
114:
115: private void checkEnd() {
116: if (end) {
117: throw new IllegalStateException(
118: "Cannot call a visit method after visitEnd has been called");
119: }
120: }
121:
122: private void checkName(final String name) {
123: if (named && name == null) {
124: throw new IllegalArgumentException(
125: "Annotation value name must not be null");
126: }
127: }
128: }
|