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 {@link
042   * Opcodes#ASM4}, {@link Opcodes#ASM5}, {@link Opcodes#ASM6} or {@link Opcodes#ASM7}.
043   */
044  protected final int api;
045
046  /** The annotation visitor to which this visitor must delegate method calls. May be null. */
047  protected AnnotationVisitor av;
048
049  /**
050   * Constructs a new {@link AnnotationVisitor}.
051   *
052   * @param api the ASM API version implemented by this visitor. Must be one of {@link
053   *     Opcodes#ASM4}, {@link Opcodes#ASM5}, {@link Opcodes#ASM6} or {@link Opcodes#ASM7}.
054   */
055  public AnnotationVisitor(final int api) {
056    this(api, null);
057  }
058
059  /**
060   * Constructs a new {@link AnnotationVisitor}.
061   *
062   * @param api the ASM API version implemented by this visitor. Must be one of {@link
063   *     Opcodes#ASM4}, {@link Opcodes#ASM5}, {@link Opcodes#ASM6} or {@link Opcodes#ASM7}.
064   * @param annotationVisitor the annotation visitor to which this visitor must delegate method
065   *     calls. May be null.
066   */
067  public AnnotationVisitor(final int api, final AnnotationVisitor annotationVisitor) {
068    if (api != Opcodes.ASM7 && api != Opcodes.ASM6 && api != Opcodes.ASM5 && api != Opcodes.ASM4) {
069      throw new IllegalArgumentException("Unsupported api " + api);
070    }
071    this.api = api;
072    this.av = annotationVisitor;
073  }
074
075  /**
076   * Visits a primitive value of the annotation.
077   *
078   * @param name the value name.
079   * @param value the actual value, whose type must be {@link Byte}, {@link Boolean}, {@link
080   *     Character}, {@link Short}, {@link Integer} , {@link Long}, {@link Float}, {@link Double},
081   *     {@link String} or {@link Type} of {@link Type#OBJECT} or {@link Type#ARRAY} sort. This
082   *     value can also be an array of byte, boolean, short, char, int, long, float or double values
083   *     (this is equivalent to using {@link #visitArray} and visiting each array element in turn,
084   *     but is more convenient).
085   */
086  public void visit(final String name, final Object value) {
087    if (av != null) {
088      av.visit(name, value);
089    }
090  }
091
092  /**
093   * Visits an enumeration value of the annotation.
094   *
095   * @param name the value name.
096   * @param descriptor the class descriptor of the enumeration class.
097   * @param value the actual enumeration value.
098   */
099  public void visitEnum(final String name, final String descriptor, final String value) {
100    if (av != null) {
101      av.visitEnum(name, descriptor, value);
102    }
103  }
104
105  /**
106   * Visits a nested annotation value of the annotation.
107   *
108   * @param name the value name.
109   * @param descriptor the class descriptor of the nested annotation class.
110   * @return a visitor to visit the actual nested annotation value, or {@literal null} if this
111   *     visitor is not interested in visiting this nested annotation. <i>The nested annotation
112   *     value must be fully visited before calling other methods on this annotation visitor</i>.
113   */
114  public AnnotationVisitor visitAnnotation(final String name, final String descriptor) {
115    if (av != null) {
116      return av.visitAnnotation(name, descriptor);
117    }
118    return null;
119  }
120
121  /**
122   * Visits an array value of the annotation. Note that arrays of primitive types (such as byte,
123   * boolean, short, char, int, long, float or double) can be passed as value to {@link #visit
124   * visit}. This is what {@link ClassReader} does.
125   *
126   * @param name the value name.
127   * @return a visitor to visit the actual array value elements, or {@literal null} if this visitor
128   *     is not interested in visiting these values. The 'name' parameters passed to the methods of
129   *     this visitor are ignored. <i>All the array values must be visited before calling other
130   *     methods on this annotation visitor</i>.
131   */
132  public AnnotationVisitor visitArray(final String name) {
133    if (av != null) {
134      return av.visitArray(name);
135    }
136    return null;
137  }
138
139  /** Visits the end of the annotation. */
140  public void visitEnd() {
141    if (av != null) {
142      av.visitEnd();
143    }
144  }
145}