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