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 field. The methods of this class must be called in the following order:
032 * ( {@code visitAnnotation} | {@code visitTypeAnnotation} | {@code visitAttribute} )* {@code
033 * visitEnd}.
034 *
035 * @author Eric Bruneton
036 */
037public abstract class FieldVisitor {
038
039  /**
040   * The ASM API version implemented by this visitor. The value of this field must be one of {@link
041   * Opcodes#ASM4}, {@link Opcodes#ASM5}, {@link Opcodes#ASM6} or {@link Opcodes#ASM7}.
042   */
043  protected final int api;
044
045  /** The field visitor to which this visitor must delegate method calls. May be null. */
046  protected FieldVisitor fv;
047
048  /**
049   * Constructs a new {@link FieldVisitor}.
050   *
051   * @param api the ASM API version implemented by this visitor. Must be one of {@link
052   *     Opcodes#ASM4}, {@link Opcodes#ASM5}, {@link Opcodes#ASM6} or {@link Opcodes#ASM7}.
053   */
054  public FieldVisitor(final int api) {
055    this(api, null);
056  }
057
058  /**
059   * Constructs a new {@link FieldVisitor}.
060   *
061   * @param api the ASM API version implemented by this visitor. Must be one of {@link
062   *     Opcodes#ASM4}, {@link Opcodes#ASM5}, {@link Opcodes#ASM6} or {@link Opcodes#ASM7}.
063   * @param fieldVisitor the field visitor to which this visitor must delegate method calls. May be
064   *     null.
065   */
066  public FieldVisitor(final int api, final FieldVisitor fieldVisitor) {
067    if (api != Opcodes.ASM7 && api != Opcodes.ASM6 && api != Opcodes.ASM5 && api != Opcodes.ASM4) {
068      throw new IllegalArgumentException("Unsupported api " + api);
069    }
070    this.api = api;
071    this.fv = fieldVisitor;
072  }
073
074  /**
075   * Visits an annotation of the field.
076   *
077   * @param descriptor the class descriptor of the annotation class.
078   * @param visible {@literal true} if the annotation is visible at runtime.
079   * @return a visitor to visit the annotation values, or {@literal null} if this visitor is not
080   *     interested in visiting this annotation.
081   */
082  public AnnotationVisitor visitAnnotation(final String descriptor, final boolean visible) {
083    if (fv != null) {
084      return fv.visitAnnotation(descriptor, visible);
085    }
086    return null;
087  }
088
089  /**
090   * Visits an annotation on the type of the field.
091   *
092   * @param typeRef a reference to the annotated type. The sort of this type reference must be
093   *     {@link TypeReference#FIELD}. See {@link TypeReference}.
094   * @param typePath the path to the annotated type argument, wildcard bound, array element type, or
095   *     static inner type within 'typeRef'. May be {@literal null} if the annotation targets
096   *     'typeRef' as a whole.
097   * @param descriptor the class descriptor of the annotation class.
098   * @param visible {@literal true} if the annotation is visible at runtime.
099   * @return a visitor to visit the annotation values, or {@literal null} if this visitor is not
100   *     interested in visiting this annotation.
101   */
102  public AnnotationVisitor visitTypeAnnotation(
103      final int typeRef, final TypePath typePath, final String descriptor, final boolean visible) {
104    if (api < Opcodes.ASM5) {
105      throw new UnsupportedOperationException("This feature requires ASM5");
106    }
107    if (fv != null) {
108      return fv.visitTypeAnnotation(typeRef, typePath, descriptor, visible);
109    }
110    return null;
111  }
112
113  /**
114   * Visits a non standard attribute of the field.
115   *
116   * @param attribute an attribute.
117   */
118  public void visitAttribute(final Attribute attribute) {
119    if (fv != null) {
120      fv.visitAttribute(attribute);
121    }
122  }
123
124  /**
125   * Visits the end of the field. This method, which is the last one to be called, is used to inform
126   * the visitor that all the annotations and attributes of the field have been visited.
127   */
128  public void visitEnd() {
129    if (fv != null) {
130      fv.visitEnd();
131    }
132  }
133}