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.ModuleVisitor;
032import io.ebean.enhance.asm.Opcodes;
033
034/**
035 * A {@link ModuleVisitor} that remaps types with a {@link Remapper}.
036 *
037 * @author Remi Forax
038 */
039public class ModuleRemapper extends ModuleVisitor {
040
041  /** The remapper used to remap the types in the visited module. */
042  protected final Remapper remapper;
043
044  /**
045   * Constructs a new {@link ModuleRemapper}. <i>Subclasses must not use this constructor</i>.
046   * Instead, they must use the {@link #ModuleRemapper(int, ModuleVisitor, Remapper)} version.
047   *
048   * @param moduleVisitor the module visitor this remapper must deleted to.
049   * @param remapper the remapper to use to remap the types in the visited module.
050   */
051  public ModuleRemapper(final ModuleVisitor moduleVisitor, final Remapper remapper) {
052    this(Opcodes.ASM7, moduleVisitor, remapper);
053  }
054
055  /**
056   * Constructs a new {@link ModuleRemapper}.
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 moduleVisitor the module visitor this remapper must deleted to.
062   * @param remapper the remapper to use to remap the types in the visited module.
063   */
064  protected ModuleRemapper(
065    final int api, final ModuleVisitor moduleVisitor, final Remapper remapper) {
066    super(api, moduleVisitor);
067    this.remapper = remapper;
068  }
069
070  @Override
071  public void visitMainClass(final String mainClass) {
072    super.visitMainClass(remapper.mapType(mainClass));
073  }
074
075  @Override
076  public void visitPackage(final String packaze) {
077    super.visitPackage(remapper.mapPackageName(packaze));
078  }
079
080  @Override
081  public void visitRequire(final String module, final int access, final String version) {
082    super.visitRequire(remapper.mapModuleName(module), access, version);
083  }
084
085  @Override
086  public void visitExport(final String packaze, final int access, final String... modules) {
087    String[] remappedModules = null;
088    if (modules != null) {
089      remappedModules = new String[modules.length];
090      for (int i = 0; i < modules.length; ++i) {
091        remappedModules[i] = remapper.mapModuleName(modules[i]);
092      }
093    }
094    super.visitExport(remapper.mapPackageName(packaze), access, remappedModules);
095  }
096
097  @Override
098  public void visitOpen(final String packaze, final int access, final String... modules) {
099    String[] remappedModules = null;
100    if (modules != null) {
101      remappedModules = new String[modules.length];
102      for (int i = 0; i < modules.length; ++i) {
103        remappedModules[i] = remapper.mapModuleName(modules[i]);
104      }
105    }
106    super.visitOpen(remapper.mapPackageName(packaze), access, remappedModules);
107  }
108
109  @Override
110  public void visitUse(final String service) {
111    super.visitUse(remapper.mapType(service));
112  }
113
114  @Override
115  public void visitProvide(final String service, final String... providers) {
116    String[] remappedProviders = new String[providers.length];
117    for (int i = 0; i < providers.length; ++i) {
118      remappedProviders[i] = remapper.mapType(providers[i]);
119    }
120    super.visitProvide(remapper.mapType(service), remappedProviders);
121  }
122}