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   * Visits a primitive value of the annotation.
089   *
090   * @param name the value name.
091   * @param value the actual value, whose type must be {@link Byte}, {@link Boolean}, {@link
092   *     Character}, {@link Short}, {@link Integer} , {@link Long}, {@link Float}, {@link Double},
093   *     {@link String} or {@link Type} of {@link Type#OBJECT} or {@link Type#ARRAY} sort. This
094   *     value can also be an array of byte, boolean, short, char, int, long, float or double values
095   *     (this is equivalent to using {@link #visitArray} and visiting each array element in turn,
096   *     but is more convenient).
097   */
098  public void visit(final String name, final Object value) {
099    if (av != null) {
100      av.visit(name, value);
101    }
102  }
103
104  /**
105   * Visits an enumeration value of the annotation.
106   *
107   * @param name the value name.
108   * @param descriptor the class descriptor of the enumeration class.
109   * @param value the actual enumeration value.
110   */
111  public void visitEnum(final String name, final String descriptor, final String value) {
112    if (av != null) {
113      av.visitEnum(name, descriptor, value);
114    }
115  }
116
117  /**
118   * Visits a nested annotation value of the annotation.
119   *
120   * @param name the value name.
121   * @param descriptor the class descriptor of the nested annotation class.
122   * @return a visitor to visit the actual nested annotation value, or {@literal null} if this
123   *     visitor is not interested in visiting this nested annotation. <i>The nested annotation
124   *     value must be fully visited before calling other methods on this annotation visitor</i>.
125   */
126  public AnnotationVisitor visitAnnotation(final String name, final String descriptor) {
127    if (av != null) {
128      return av.visitAnnotation(name, descriptor);
129    }
130    return null;
131  }
132
133  /**
134   * Visits an array value of the annotation. Note that arrays of primitive values (such as byte,
135   * boolean, short, char, int, long, float or double) can be passed as value to {@link #visit
136   * visit}. This is what {@link ClassReader} does for non empty arrays of primitive values.
137   *
138   * @param name the value name.
139   * @return a visitor to visit the actual array value elements, or {@literal null} if this visitor
140   *     is not interested in visiting these values. The 'name' parameters passed to the methods of
141   *     this visitor are ignored. <i>All the array values must be visited before calling other
142   *     methods on this annotation visitor</i>.
143   */
144  public AnnotationVisitor visitArray(final String name) {
145    if (av != null) {
146      return av.visitArray(name);
147    }
148    return null;
149  }
150
151  /** Visits the end of the annotation. */
152  public void visitEnd() {
153    if (av != null) {
154      av.visitEnd();
155    }
156  }
157}