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