001// ASM: a very small and fast Java bytecode manipulation framework
002// Copyright (c) 2000-2011 INRIA, France Telecom
003// All rights reserved.
004//
005// Redistribution and use in source and binary forms, with or without
006// modification, are permitted provided that the following conditions
007// are met:
008// 1. Redistributions of source code must retain the above copyright
009//    notice, this list of conditions and the following disclaimer.
010// 2. Redistributions in binary form must reproduce the above copyright
011//    notice, this list of conditions and the following disclaimer in the
012//    documentation and/or other materials provided with the distribution.
013// 3. Neither the name of the copyright holders nor the names of its
014//    contributors may be used to endorse or promote products derived from
015//    this software without specific prior written permission.
016//
017// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
018// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
019// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
020// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
021// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
022// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
023// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
024// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
025// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
026// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
027// THE POSSIBILITY OF SUCH DAMAGE.
028package io.ebean.enhance.asm;
029
030/**
031 * A visitor to visit a Java annotation. The methods of this class must be called in the following
032 * order: ( {@code visit} | {@code visitEnum} | {@code visitAnnotation} | {@code visitArray} )*
033 * {@code visitEnd}.
034 *
035 * @author Eric Bruneton
036 * @author Eugene Kuleshov
037 */
038public abstract class AnnotationVisitor {
039
040  /**
041   * The ASM API version implemented by this visitor. The value of this field must be one of the
042   * {@code ASM}<i>x</i> values in {@link Opcodes}.
043   */
044  protected final int api;
045
046  /**
047   * The annotation visitor to which this visitor must delegate method calls. May be {@literal
048   * null}.
049   */
050  protected AnnotationVisitor av;
051
052  /**
053   * Constructs a new {@link AnnotationVisitor}.
054   *
055   * @param api the ASM API version implemented by this visitor. Must be one of the {@code
056   *     ASM}<i>x</i> values in {@link Opcodes}.
057   */
058  protected AnnotationVisitor(final int api) {
059    this(api, null);
060  }
061
062  /**
063   * Constructs a new {@link AnnotationVisitor}.
064   *
065   * @param api the ASM API version implemented by this visitor. Must be one of the {@code
066   *     ASM}<i>x</i> values in {@link Opcodes}.
067   * @param annotationVisitor the annotation visitor to which this visitor must delegate method
068   *     calls. May be {@literal null}.
069   */
070  protected AnnotationVisitor(final int api, final AnnotationVisitor annotationVisitor) {
071    if (api != Opcodes.ASM9
072        && api != Opcodes.ASM8
073        && api != Opcodes.ASM7
074        && api != Opcodes.ASM6
075        && api != Opcodes.ASM5
076        && api != Opcodes.ASM4
077        && api != Opcodes.ASM10_EXPERIMENTAL) {
078      throw new IllegalArgumentException("Unsupported api " + api);
079    }
080    if (api == Opcodes.ASM10_EXPERIMENTAL) {
081      Constants.checkAsmExperimental(this);
082    }
083    this.api = api;
084    this.av = annotationVisitor;
085  }
086
087  /**
088   * The annotation visitor to which this visitor must delegate method calls. May be {@literal
089   * null}.
090   *
091   * @return the annotation visitor to which this visitor must delegate method calls, or {@literal
092   *     null}.
093   */
094  public AnnotationVisitor getDelegate() {
095    return av;
096  }
097
098  /**
099   * Visits a primitive value of the annotation.
100   *
101   * @param name the value name.
102   * @param value the actual value, whose type must be {@link Byte}, {@link Boolean}, {@link
103   *     Character}, {@link Short}, {@link Integer} , {@link Long}, {@link Float}, {@link Double},
104   *     {@link String} or {@link Type} of {@link Type#OBJECT} or {@link Type#ARRAY} sort. This
105   *     value can also be an array of byte, boolean, short, char, int, long, float or double values
106   *     (this is equivalent to using {@link #visitArray} and visiting each array element in turn,
107   *     but is more convenient).
108   */
109  public void visit(final String name, final Object value) {
110    if (av != null) {
111      av.visit(name, value);
112    }
113  }
114
115  /**
116   * Visits an enumeration value of the annotation.
117   *
118   * @param name the value name.
119   * @param descriptor the class descriptor of the enumeration class.
120   * @param value the actual enumeration value.
121   */
122  public void visitEnum(final String name, final String descriptor, final String value) {
123    if (av != null) {
124      av.visitEnum(name, descriptor, value);
125    }
126  }
127
128  /**
129   * Visits a nested annotation value of the annotation.
130   *
131   * @param name the value name.
132   * @param descriptor the class descriptor of the nested annotation class.
133   * @return a visitor to visit the actual nested annotation value, or {@literal null} if this
134   *     visitor is not interested in visiting this nested annotation. <i>The nested annotation
135   *     value must be fully visited before calling other methods on this annotation visitor</i>.
136   */
137  public AnnotationVisitor visitAnnotation(final String name, final String descriptor) {
138    if (av != null) {
139      return av.visitAnnotation(name, descriptor);
140    }
141    return null;
142  }
143
144  /**
145   * Visits an array value of the annotation. Note that arrays of primitive values (such as byte,
146   * boolean, short, char, int, long, float or double) can be passed as value to {@link #visit
147   * visit}. This is what {@link ClassReader} does for non empty arrays of primitive values.
148   *
149   * @param name the value name.
150   * @return a visitor to visit the actual array value elements, or {@literal null} if this visitor
151   *     is not interested in visiting these values. The 'name' parameters passed to the methods of
152   *     this visitor are ignored. <i>All the array values must be visited before calling other
153   *     methods on this annotation visitor</i>.
154   */
155  public AnnotationVisitor visitArray(final String name) {
156    if (av != null) {
157      return av.visitArray(name);
158    }
159    return null;
160  }
161
162  /** Visits the end of the annotation. */
163  public void visitEnd() {
164    if (av != null) {
165      av.visitEnd();
166    }
167  }
168}