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 = internalNames.clone();
144                 }
145                 remappedInternalNames[i] = remappedInternalName;
146             }
147         }
148         return remappedInternalNames != null ? remappedInternalNames : internalNames;
149     }
150 
151     /**
152       * Returns the given method descriptor, with its argument and return type descriptors remapped
153       * with {@link #mapDesc(String)}.
154       *
155       * @param methodDescriptor a method descriptor.
156       * @return the given method descriptor, with its argument and return type descriptors remapped
157       *     with {@link #mapDesc(String)}.
158       */
159     public String mapMethodDesc(final String methodDescriptor) {
160         if ("()V".equals(methodDescriptor)) {
161             return methodDescriptor;
162         }
163 
164         StringBuilder stringBuilder = new StringBuilder("(");
165         for (Type argumentType : Type.getArgumentTypes(methodDescriptor)) {
166             stringBuilder.append(mapType(argumentType).getDescriptor());
167         }
168         Type returnType = Type.getReturnType(methodDescriptor);
169         if (returnType == Type.VOID_TYPE) {
170             stringBuilder.append(")V");
171         } else {
172             stringBuilder.append(')').append(mapType(returnType).getDescriptor());
173         }
174         return stringBuilder.toString();
175     }
176 
177     /**
178       * Returns the given value, remapped with this remapper. Possible values are {@link Boolean},
179       * {@link Byte}, {@link Short}, {@link Character}, {@link Integer}, {@link Long}, {@link Double},
180       * {@link Float}, {@link String}, {@link Type}, {@link Handle}, {@link ConstantDynamic} or arrays
181       * of primitive types .
182       *
183       * @param value an object. Only {@link Type}, {@link Handle} and {@link ConstantDynamic} values
184       *     are remapped.
185       * @return the given value, remapped with this remapper.
186       */
187     public Object mapValue(final Object value) {
188         if (value instanceof Type) {
189             return mapType((Type) value);
190         }
191         if (value instanceof Handle) {
192             Handle handle = (Handle) value;
193             return new Handle(
194                     handle.getTag(),
195                     mapType(handle.getOwner()),
196                     mapMethodName(handle.getOwner(), handle.getName(), handle.getDesc()),
197                     handle.getTag() <= Opcodes.H_PUTSTATIC
198                             ? mapDesc(handle.getDesc())
199                             : mapMethodDesc(handle.getDesc()),
200                     handle.isInterface());
201         }
202         if (value instanceof ConstantDynamic) {
203             ConstantDynamic constantDynamic = (ConstantDynamic) value;
204             int bootstrapMethodArgumentCount = constantDynamic.getBootstrapMethodArgumentCount();
205             Object[] remappedBootstrapMethodArguments = new Object[bootstrapMethodArgumentCount];
206             for (int i = 0; i < bootstrapMethodArgumentCount; ++i) {
207                 remappedBootstrapMethodArguments[i] =
208                         mapValue(constantDynamic.getBootstrapMethodArgument(i));
209             }
210             String descriptor = constantDynamic.getDescriptor();
211             return new ConstantDynamic(
212                     mapInvokeDynamicMethodName(constantDynamic.getName(), descriptor),
213                     mapDesc(descriptor),
214                     (Handle) mapValue(constantDynamic.getBootstrapMethod()),
215                     remappedBootstrapMethodArguments);
216         }
217         return value;
218     }
219 
220     /**
221       * Returns the given signature, remapped with the {@link SignatureVisitor} returned by {@link
222       * #createSignatureRemapper(SignatureVisitor)}.
223       *
224       * @param signature a <i>JavaTypeSignature</i>, <i>ClassSignature</i> or <i>MethodSignature</i>.
225       * @param typeSignature whether the given signature is a <i>JavaTypeSignature</i>.
226       * @return signature the given signature, remapped with the {@link SignatureVisitor} returned by
227       *     {@link #createSignatureRemapper(SignatureVisitor)}.
228       */
229     public String mapSignature(final String signature, final boolean typeSignature) {
230         if (signature == null) {
231             return null;
232         }
233         SignatureReader signatureReader = new SignatureReader(signature);
234         SignatureWriter signatureWriter = new SignatureWriter();
235         SignatureVisitor signatureRemapper = createSignatureRemapper(signatureWriter);
236         if (typeSignature) {
237             signatureReader.acceptType(signatureRemapper);
238         } else {
239             signatureReader.accept(signatureRemapper);
240         }
241         return signatureWriter.toString();
242     }
243 
244     /**
245       * Constructs a new remapper for signatures. The default implementation of this method returns a
246       * new {@link SignatureRemapper}.
247       *
248       * @param signatureVisitor the SignatureVisitor the remapper must delegate to.
249       * @return the newly created remapper.
250       * @deprecated use {@link #createSignatureRemapper} instead.
251       */
252     @Deprecated
253     protected SignatureVisitor createRemappingSignatureAdapter(
254             final SignatureVisitor signatureVisitor) {
255         return createSignatureRemapper(signatureVisitor);
256     }
257 
258     /**
259       * Constructs a new remapper for signatures. The default implementation of this method returns a
260       * new {@link SignatureRemapper}.
261       *
262       * @param signatureVisitor the SignatureVisitor the remapper must delegate to.
263       * @return the newly created remapper.
264       */
265     protected SignatureVisitor createSignatureRemapper(final SignatureVisitor signatureVisitor) {
266         return new SignatureRemapper(signatureVisitor, this);
267     }
268 
269     /**
270       * Maps an inner class name to its new name. The default implementation of this method provides a
271       * strategy that will work for inner classes produced by Java, but not necessarily other
272       * languages. Subclasses can override.
273       *
274       * @param name the fully-qualified internal name of the inner class.
275       * @param ownerName the internal name of the owner class of the inner class.
276       * @param innerName the internal name of the inner class.
277       * @return the new inner name of the inner class.
278       */
279     public String mapInnerClassName(
280             final String name, final String ownerName, final String innerName) {
281         final String remappedInnerName = this.mapType(name);
282         if (remappedInnerName.contains("$")) {
283             int index = remappedInnerName.lastIndexOf('$') + 1;
284             while (index < remappedInnerName.length()
285                     && Character.isDigit(remappedInnerName.charAt(index))) {
286                 index++;
287             }
288             return remappedInnerName.substring(index);
289         } else {
290             return innerName;
291         }
292     }
293 
294     /**
295       * Maps a method name to its new name. The default implementation of this method returns the given
296       * name, unchanged. Subclasses can override.
297       *
298       * @param owner the internal name of the owner class of the method.
299       * @param name the name of the method.
300       * @param descriptor the descriptor of the method.
301       * @return the new name of the method.
302       */
303     public String mapMethodName(final String owner, final String name, final String descriptor) {
304         return name;
305     }
306 
307     /**
308       * Maps an invokedynamic or a constant dynamic method name to its new name. The default
309       * implementation of this method returns the given name, unchanged. Subclasses can override.
310       *
311       * @param name the name of the method.
312       * @param descriptor the descriptor of the method.
313       * @return the new name of the method.
314       */
315     public String mapInvokeDynamicMethodName(final String name, final String descriptor) {
316         return name;
317     }
318 
319     /**
320       * Maps a record component name to its new name. The default implementation of this method returns
321       * the given name, unchanged. Subclasses can override.
322       *
323       * @param owner the internal name of the owner class of the field.
324       * @param name the name of the field.
325       * @param descriptor the descriptor of the field.
326       * @return the new name of the field.
327       */
328     public String mapRecordComponentName(
329             final String owner, final String name, final String descriptor) {
330         return name;
331     }
332 
333     /**
334       * Maps a field name to its new name. The default implementation of this method returns the given
335       * name, unchanged. Subclasses can override.
336       *
337       * @param owner the internal name of the owner class of the field.
338       * @param name the name of the field.
339       * @param descriptor the descriptor of the field.
340       * @return the new name of the field.
341       */
342     public String mapFieldName(final String owner, final String name, final String descriptor) {
343         return name;
344     }
345 
346     /**
347       * Maps a package name to its new name. The default implementation of this method returns the
348       * given name, unchanged. Subclasses can override.
349       *
350       * @param name the fully qualified name of the package (using dots).
351       * @return the new name of the package.
352       */
353     public String mapPackageName(final String name) {
354         return name;
355     }
356 
357     /**
358       * Maps a module name to its new name. The default implementation of this method returns the given
359       * name, unchanged. Subclasses can override.
360       *
361       * @param name the fully qualified name (using dots) of a module.
362       * @return the new name of the module.
363       */
364     public String mapModuleName(final String name) {
365         return name;
366     }
367 
368     /**
369       * Maps the internal name of a class to its new name. The default implementation of this method
370       * returns the given name, unchanged. Subclasses can override.
371       *
372       * @param internalName the internal name of a class.
373       * @return the new internal name.
374       */
375     public String map(final String internalName) {
376         return internalName;
377     }
378 }