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