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 java.util.Collections;
032import java.util.Map;
033
034/**
035 * A {@link Remapper} using a {@link Map} to define its mapping.
036 *
037 * @author Eugene Kuleshov
038 */
039public class SimpleRemapper extends Remapper {
040
041  private final Map<String, String> mapping;
042
043  /**
044   * Constructs a new {@link SimpleRemapper} with the given mapping.
045   *
046   * @param mapping a map specifying a remapping as follows:
047   *     <ul>
048   *       <li>for method names, the key is the owner, name and descriptor of the method (in the
049   *           form &lt;owner&gt;.&lt;name&gt;&lt;descriptor&gt;), and the value is the new method
050   *           name.
051   *       <li>for invokedynamic method names, the key is the name and descriptor of the method (in
052   *           the form .&lt;name&gt;&lt;descriptor&gt;), and the value is the new method name.
053   *       <li>for field names, the key is the owner and name of the field (in the form
054   *           &lt;owner&gt;.&lt;name&gt;), and the value is the new field name.
055   *       <li>for internal names, the key is the old internal name, and the value is the new
056   *           internal name.
057   *     </ul>
058   */
059  public SimpleRemapper(final Map<String, String> mapping) {
060    this.mapping = mapping;
061  }
062
063  /**
064   * Constructs a new {@link SimpleRemapper} with the given mapping.
065   *
066   * @param oldName the key corresponding to a method, field or internal name (see {@link
067   *     #SimpleRemapper(Map)} for the format of these keys).
068   * @param newName the new method, field or internal name.
069   */
070  public SimpleRemapper(final String oldName, final String newName) {
071    this.mapping = Collections.singletonMap(oldName, newName);
072  }
073
074  @Override
075  public String mapMethodName(final String owner, final String name, final String descriptor) {
076    String remappedName = map(owner + '.' + name + descriptor);
077    return remappedName == null ? name : remappedName;
078  }
079
080  @Override
081  public String mapInvokeDynamicMethodName(final String name, final String descriptor) {
082    String remappedName = map('.' + name + descriptor);
083    return remappedName == null ? name : remappedName;
084  }
085
086  @Override
087  public String mapFieldName(final String owner, final String name, final String descriptor) {
088    String remappedName = map(owner + '.' + name);
089    return remappedName == null ? name : remappedName;
090  }
091
092  @Override
093  public String map(final String key) {
094    return mapping.get(key);
095  }
096}