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