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.
028
029package io.ebean.enhance.asm.commons;
030
031import io.ebean.enhance.asm.AnnotationVisitor;
032import io.ebean.enhance.asm.Opcodes;
033
034/**
035 * An {@link AnnotationVisitor} that remaps types with a {@link Remapper}.
036 *
037 * @author Eugene Kuleshov
038 */
039public class AnnotationRemapper extends AnnotationVisitor {
040
041  /** The remapper used to remap the types in the visited annotation. */
042  protected final Remapper remapper;
043
044  /**
045   * Constructs a new {@link AnnotationRemapper}. <i>Subclasses must not use this constructor</i>.
046   * Instead, they must use the {@link #AnnotationRemapper(int, AnnotationVisitor,Remapper)} version.
047   *
048   * @param annotationVisitor the annotation visitor this remapper must deleted to.
049   * @param remapper the remapper to use to remap the types in the visited annotation.
050   */
051  public AnnotationRemapper(final AnnotationVisitor annotationVisitor, final Remapper remapper) {
052    this(Opcodes.ASM7, annotationVisitor, remapper);
053  }
054
055  /**
056   * Constructs a new {@link AnnotationRemapper}.
057   *
058   * @param api the ASM API version supported by this remapper. Must be one of {@link
059   *     Opcodes#ASM4}, {@link Opcodes#ASM5} or {@link
060   *     Opcodes#ASM6}.
061   * @param annotationVisitor the annotation visitor this remapper must deleted to.
062   * @param remapper the remapper to use to remap the types in the visited annotation.
063   */
064  protected AnnotationRemapper(
065    final int api, final AnnotationVisitor annotationVisitor, final Remapper remapper) {
066    super(api, annotationVisitor);
067    this.remapper = remapper;
068  }
069
070  @Override
071  public void visit(final String name, final Object value) {
072    super.visit(name, remapper.mapValue(value));
073  }
074
075  @Override
076  public void visitEnum(final String name, final String descriptor, final String value) {
077    super.visitEnum(name, remapper.mapDesc(descriptor), value);
078  }
079
080  @Override
081  public AnnotationVisitor visitAnnotation(final String name, final String descriptor) {
082    AnnotationVisitor annotationVisitor = super.visitAnnotation(name, remapper.mapDesc(descriptor));
083    if (annotationVisitor == null) {
084      return null;
085    } else {
086      return annotationVisitor == av
087          ? this
088          : new AnnotationRemapper(api, annotationVisitor, remapper);
089    }
090  }
091
092  @Override
093  public AnnotationVisitor visitArray(final String name) {
094    AnnotationVisitor annotationVisitor = super.visitArray(name);
095    if (annotationVisitor == null) {
096      return null;
097    } else {
098      return annotationVisitor == av
099          ? this
100          : new AnnotationRemapper(api, annotationVisitor, remapper);
101    }
102  }
103}