1 /*
   2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   3  *
   4  * This code is free software; you can redistribute it and/or modify it
   5  * under the terms of the GNU General Public License version 2 only, as
   6  * published by the Free Software Foundation.  Oracle designates this
   7  * particular file as subject to the "Classpath" exception as provided
   8  * by Oracle in the LICENSE file that accompanied this code.
   9  *
  10  * This code is distributed in the hope that it will be useful, but WITHOUT
  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  */
  24 
  25 /*
  26  * This file is available under and governed by the GNU General Public
  27  * License version 2 only, as published by the Free Software Foundation.
  28  * However, the following notice accompanied the original version of this
  29  * file:
  30  *
  31  * ASM: a very small and fast Java bytecode manipulation framework
  32  * Copyright (c) 2000-2011 INRIA, France Telecom
  33  * All rights reserved.
  34  *
  35  * Redistribution and use in source and binary forms, with or without
  36  * modification, are permitted provided that the following conditions
  37  * are met:
  38  * 1. Redistributions of source code must retain the above copyright
  39  *    notice, this list of conditions and the following disclaimer.
  40  * 2. Redistributions in binary form must reproduce the above copyright
  41  *    notice, this list of conditions and the following disclaimer in the
  42  *    documentation and/or other materials provided with the distribution.
  43  * 3. Neither the name of the copyright holders nor the names of its
  44  *    contributors may be used to endorse or promote products derived from
  45  *    this software without specific prior written permission.
  46  *
  47  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  48  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  49  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  50  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  51  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  52  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  53  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  54  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  55  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  56  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  57  * THE POSSIBILITY OF SUCH DAMAGE.
  58  */
  59 package jdk.internal.org.objectweb.asm.commons;
  60 
  61 import jdk.internal.org.objectweb.asm.ConstantDynamic;
  62 import jdk.internal.org.objectweb.asm.Handle;
  63 import jdk.internal.org.objectweb.asm.Opcodes;
  64 import jdk.internal.org.objectweb.asm.Type;
  65 import jdk.internal.org.objectweb.asm.signature.SignatureReader;
  66 import jdk.internal.org.objectweb.asm.signature.SignatureVisitor;
  67 import jdk.internal.org.objectweb.asm.signature.SignatureWriter;
  68 
  69 /**
  70  * A class responsible for remapping types and names.
  71  *
  72  * @author Eugene Kuleshov
  73  */
  74 public abstract class Remapper {
  75 
  76     /**
  77       * Returns the given descriptor, remapped with {@link #map(String)}.
  78       *
  79       * @param descriptor a type descriptor.
  80       * @return the given descriptor, with its [array element type] internal name remapped with {@link
  81       *     #map(String)} (if the descriptor corresponds to an array or object type, otherwise the
  82       *     descriptor is returned as is).
  83       */
  84     public String mapDesc(final String descriptor) {
  85         return mapType(Type.getType(descriptor)).getDescriptor();
  86     }
  87 
  88     /**
  89       * Returns the given {@link Type}, remapped with {@link #map(String)} or {@link
  90       * #mapMethodDesc(String)}.
  91       *
  92       * @param type a type, which can be a method type.
  93       * @return the given type, with its [array element type] internal name remapped with {@link
  94       *     #map(String)} (if the type is an array or object type, otherwise the type is returned as
  95       *     is) or, of the type is a method type, with its descriptor remapped with {@link
  96       *     #mapMethodDesc(String)}.
  97       */
  98     private Type mapType(final Type type) {
  99         switch (type.getSort()) {
 100             case Type.ARRAY:
 101                 StringBuilder remappedDescriptor = new StringBuilder();
 102                 for (int i = 0; i < type.getDimensions(); ++i) {
 103                     remappedDescriptor.append('[');
 104                 }
 105                 remappedDescriptor.append(mapType(type.getElementType()).getDescriptor());
 106                 return Type.getType(remappedDescriptor.toString());
 107             case Type.OBJECT:
 108                 String remappedInternalName = map(type.getInternalName());
 109                 return remappedInternalName != null ? Type.getObjectType(remappedInternalName) : type;
 110             case Type.METHOD:
 111                 return Type.getMethodType(mapMethodDesc(type.getDescriptor()));
 112             default:
 113                 return type;
 114         }
 115     }
 116 
 117     /**
 118       * Returns the given internal name, remapped with {@link #map(String)}.
 119       *
 120       * @param internalName the internal name (or array type descriptor) of some (array) class.
 121       * @return the given internal name, remapped with {@link #map(String)}.
 122       */
 123     public String mapType(final String internalName) {
 124         if (internalName == null) {
 125             return null;
 126         }
 127         return mapType(Type.getObjectType(internalName)).getInternalName();
 128     }
 129 
 130     /**
 131       * Returns the given internal names, remapped with {@link #map(String)}.
 132       *
 133       * @param internalNames the internal names (or array type descriptors) of some (array) classes.
 134       * @return the given internal name, remapped with {@link #map(String)}.
 135       */
 136     public String[] mapTypes(final String[] internalNames) {
 137         String[] remappedInternalNames = null;
 138         for (int i = 0; i < internalNames.length; ++i) {
 139             String internalName = internalNames[i];
 140             String remappedInternalName = mapType(internalName);
 141             if (remappedInternalName != null) {
 142                 if (remappedInternalNames == null) {
 143                     remappedInternalNames = new String[internalNames.length];
 144                     System.arraycopy(internalNames, 0, remappedInternalNames, 0, internalNames.length);
 145                 }
 146                 remappedInternalNames[i] = remappedInternalName;
 147             }
 148         }
 149         return remappedInternalNames != null ? remappedInternalNames : internalNames;
 150     }
 151 
 152     /**
 153       * Returns the given method descriptor, with its argument and return type descriptors remapped
 154       * with {@link #mapDesc(String)}.
 155       *
 156       * @param methodDescriptor a method descriptor.
 157       * @return the given method descriptor, with its argument and return type descriptors remapped
 158       *     with {@link #mapDesc(String)}.
 159       */
 160     public String mapMethodDesc(final String methodDescriptor) {
 161         if ("()V".equals(methodDescriptor)) {
 162             return methodDescriptor;
 163         }
 164 
 165         StringBuilder stringBuilder = new StringBuilder("(");
 166         for (Type argumentType : Type.getArgumentTypes(methodDescriptor)) {
 167             stringBuilder.append(mapType(argumentType).getDescriptor());
 168         }
 169         Type returnType = Type.getReturnType(methodDescriptor);
 170         if (returnType == Type.VOID_TYPE) {
 171             stringBuilder.append(")V");
 172         } else {
 173             stringBuilder.append(')').append(mapType(returnType).getDescriptor());
 174         }
 175         return stringBuilder.toString();
 176     }
 177 
 178     /**
 179       * Returns the given value, remapped with this remapper. Possible values are {@link Boolean},
 180       * {@link Byte}, {@link Short}, {@link Character}, {@link Integer}, {@link Long}, {@link Double},
 181       * {@link Float}, {@link String}, {@link Type}, {@link Handle}, {@link ConstantDynamic} or arrays
 182       * of primitive types .
 183       *
 184       * @param value an object. Only {@link Type}, {@link Handle} and {@link ConstantDynamic} values
 185       *     are remapped.
 186       * @return the given value, remapped with this remapper.
 187       */
 188     public Object mapValue(final Object value) {
 189         if (value instanceof Type) {
 190             return mapType((Type) value);
 191         }
 192         if (value instanceof Handle) {
 193             Handle handle = (Handle) value;
 194             return new Handle(
 195                     handle.getTag(),
 196                     mapType(handle.getOwner()),
 197                     mapMethodName(handle.getOwner(), handle.getName(), handle.getDesc()),
 198                     handle.getTag() <= Opcodes.H_PUTSTATIC
 199                             ? mapDesc(handle.getDesc())
 200                             : mapMethodDesc(handle.getDesc()),
 201                     handle.isInterface());
 202         }
 203         if (value instanceof ConstantDynamic) {
 204             ConstantDynamic constantDynamic = (ConstantDynamic) value;
 205             int bootstrapMethodArgumentCount = constantDynamic.getBootstrapMethodArgumentCount();
 206             Object[] remappedBootstrapMethodArguments = new Object[bootstrapMethodArgumentCount];
 207             for (int i = 0; i < bootstrapMethodArgumentCount; ++i) {
 208                 remappedBootstrapMethodArguments[i] =
 209                         mapValue(constantDynamic.getBootstrapMethodArgument(i));
 210             }
 211             String descriptor = constantDynamic.getDescriptor();
 212             return new ConstantDynamic(
 213                     mapInvokeDynamicMethodName(constantDynamic.getName(), descriptor),
 214                     mapDesc(descriptor),
 215                     (Handle) mapValue(constantDynamic.getBootstrapMethod()),
 216                     remappedBootstrapMethodArguments);
 217         }
 218         return value;
 219     }
 220 
 221     /**
 222       * Returns the given signature, remapped with the {@link SignatureVisitor} returned by {@link
 223       * #createSignatureRemapper(SignatureVisitor)}.
 224       *
 225       * @param signature a <i>JavaTypeSignature</i>, <i>ClassSignature</i> or <i>MethodSignature</i>.
 226       * @param typeSignature whether the given signature is a <i>JavaTypeSignature</i>.
 227       * @return signature the given signature, remapped with the {@link SignatureVisitor} returned by
 228       *     {@link #createSignatureRemapper(SignatureVisitor)}.
 229       */
 230     public String mapSignature(final String signature, final boolean typeSignature) {
 231         if (signature == null) {
 232             return null;
 233         }
 234         SignatureReader signatureReader = new SignatureReader(signature);
 235         SignatureWriter signatureWriter = new SignatureWriter();
 236         SignatureVisitor signatureRemapper = createSignatureRemapper(signatureWriter);
 237         if (typeSignature) {
 238             signatureReader.acceptType(signatureRemapper);
 239         } else {
 240             signatureReader.accept(signatureRemapper);
 241         }
 242         return signatureWriter.toString();
 243     }
 244 
 245     /**
 246       * Constructs a new remapper for signatures. The default implementation of this method returns a
 247       * new {@link SignatureRemapper}.
 248       *
 249       * @param signatureVisitor the SignatureVisitor the remapper must delegate to.
 250       * @return the newly created remapper.
 251       * @deprecated use {@link #createSignatureRemapper} instead.
 252       */
 253     @Deprecated
 254     protected SignatureVisitor createRemappingSignatureAdapter(
 255             final SignatureVisitor signatureVisitor) {
 256         return createSignatureRemapper(signatureVisitor);
 257     }
 258 
 259     /**
 260       * Constructs a new remapper for signatures. The default implementation of this method returns a
 261       * new {@link SignatureRemapper}.
 262       *
 263       * @param signatureVisitor the SignatureVisitor the remapper must delegate to.
 264       * @return the newly created remapper.
 265       */
 266     protected SignatureVisitor createSignatureRemapper(final SignatureVisitor signatureVisitor) {
 267         return new SignatureRemapper(signatureVisitor, this);
 268     }
 269 
 270     /**
 271       * Maps an inner class name to its new name. The default implementation of this method provides a
 272       * strategy that will work for inner classes produced by Java, but not necessarily other
 273       * languages. Subclasses can override.
 274       *
 275       * @param name the fully-qualified internal name of the inner class.
 276       * @param ownerName the internal name of the owner class of the inner class.
 277       * @param innerName the internal name of the inner class.
 278       * @return the new inner name of the inner class.
 279       */
 280     public String mapInnerClassName(
 281             final String name, final String ownerName, final String innerName) {
 282         final String remappedInnerName = this.mapType(name);
 283         if (remappedInnerName.contains("$")) {
 284             return remappedInnerName.substring(remappedInnerName.lastIndexOf('$') + 1);
 285         } else {
 286             return innerName;
 287         }
 288     }
 289 
 290     /**
 291       * Maps a method name to its new name. The default implementation of this method returns the given
 292       * name, unchanged. Subclasses can override.
 293       *
 294       * @param owner the internal name of the owner class of the method.
 295       * @param name the name of the method.
 296       * @param descriptor the descriptor of the method.
 297       * @return the new name of the method.
 298       */
 299     public String mapMethodName(final String owner, final String name, final String descriptor) {
 300         return name;
 301     }
 302 
 303     /**
 304       * Maps an invokedynamic or a constant dynamic method name to its new name. The default
 305       * implementation of this method returns the given name, unchanged. Subclasses can override.
 306       *
 307       * @param name the name of the method.
 308       * @param descriptor the descriptor of the method.
 309       * @return the new name of the method.
 310       */
 311     public String mapInvokeDynamicMethodName(final String name, final String descriptor) {
 312         return name;
 313     }
 314 
 315     /**
 316       * Maps a field name to its new name. The default implementation of this method returns the given
 317       * name, unchanged. Subclasses can override.
 318       *
 319       * @param owner the internal name of the owner class of the field.
 320       * @param name the name of the field.
 321       * @param descriptor the descriptor of the field.
 322       * @return the new name of the field.
 323       */
 324     public String mapFieldName(final String owner, final String name, final String descriptor) {
 325         return name;
 326     }
 327 
 328     /**
 329       * Maps a package name to its new name. The default implementation of this method returns the
 330       * given name, unchanged. Subclasses can override.
 331       *
 332       * @param name the fully qualified name of the package (using dots).
 333       * @return the new name of the package.
 334       */
 335     public String mapPackageName(final String name) {
 336         return name;
 337     }
 338 
 339     /**
 340       * Maps a module name to its new name. The default implementation of this method returns the given
 341       * name, unchanged. Subclasses can override.
 342       *
 343       * @param name the fully qualified name (using dots) of a module.
 344       * @return the new name of the module.
 345       */
 346     public String mapModuleName(final String name) {
 347         return name;
 348     }
 349 
 350     /**
 351       * Maps the internal name of a class to its new name. The default implementation of this method
 352       * returns the given name, unchanged. Subclasses can override.
 353       *
 354       * @param internalName the internal name of a class.
 355       * @return the new internal name.
 356       */
 357     public String map(final String internalName) {
 358         return internalName;
 359     }
 360 }