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 
 60 package jdk.internal.org.objectweb.asm.commons;
 61 
 62 import jdk.internal.org.objectweb.asm.ConstantDynamic;
 63 import jdk.internal.org.objectweb.asm.Handle;
 64 import jdk.internal.org.objectweb.asm.Opcodes;
 65 import jdk.internal.org.objectweb.asm.Type;
 66 import jdk.internal.org.objectweb.asm.signature.SignatureReader;
 67 import jdk.internal.org.objectweb.asm.signature.SignatureVisitor;
 68 import jdk.internal.org.objectweb.asm.signature.SignatureWriter;
 69 
 70 /**
 71  * A class responsible for remapping types and names.
 72  *
 73  * @author Eugene Kuleshov
 74  */
 75 public abstract class Remapper {
 76 
 77     /**
 78       * Returns the given descriptor, remapped with {@link #map(String)}.
 79       *
 80       * @param descriptor a type descriptor.
 81       * @return the given descriptor, with its [array element type] internal name remapped with {@link
 82       *     #map(String)} (if the descriptor corresponds to an array or object type, otherwise the
 83       *     descriptor is returned as is).
 84       */
 85     public String mapDesc(final String descriptor) {
 86         return mapType(Type.getType(descriptor)).getDescriptor();
 87     }
 88 
 89     /**
 90       * Returns the given {@link Type}, remapped with {@link #map(String)} or {@link
 91       * #mapMethodDesc(String)}.
 92       *
 93       * @param type a type, which can be a method type.
 94       * @return the given type, with its [array element type] internal name remapped with {@link
 95       *     #map(String)} (if the type is an array or object type, otherwise the type is returned as
 96       *     is) or, of the type is a method type, with its descriptor remapped with {@link
 97       *     #mapMethodDesc(String)}.
 98       */
 99     private Type mapType(final Type type) {
100         switch (type.getSort()) {
101             case Type.ARRAY:
102                 StringBuilder remappedDescriptor = new StringBuilder();
103                 for (int i = 0; i < type.getDimensions(); ++i) {
104                     remappedDescriptor.append('[');
105                 }
106                 remappedDescriptor.append(mapType(type.getElementType()).getDescriptor());
107                 return Type.getType(remappedDescriptor.toString());
108             case Type.OBJECT:
109                 String remappedInternalName = map(type.getInternalName());
110                 return remappedInternalName != null ? Type.getObjectType(remappedInternalName) : type;
111             case Type.METHOD:
112                 return Type.getMethodType(mapMethodDesc(type.getDescriptor()));
113             default:
114                 return type;
115         }
116     }
117 
118     /**
119       * Returns the given internal name, remapped with {@link #map(String)}.
120       *
121       * @param internalName the internal name (or array type descriptor) of some (array) class.
122       * @return the given internal name, remapped with {@link #map(String)}.
123       */
124     public String mapType(final String internalName) {
125         if (internalName == null) {
126             return null;
127         }
128         return mapType(Type.getObjectType(internalName)).getInternalName();
129     }
130 
131     /**
132       * Returns the given internal names, remapped with {@link #map(String)}.
133       *
134       * @param internalNames the internal names (or array type descriptors) of some (array) classes.
135       * @return the given internal name, remapped with {@link #map(String)}.
136       */
137     public String[] mapTypes(final String[] internalNames) {
138         String[] remappedInternalNames = null;
139         for (int i = 0; i < internalNames.length; ++i) {
140             String internalName = internalNames[i];
141             String remappedInternalName = mapType(internalName);
142             if (remappedInternalName != null) {
143                 if (remappedInternalNames == null) {
144                     remappedInternalNames = internalNames.clone();
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             boolean isFieldHandle = handle.getTag() <= Opcodes.H_PUTSTATIC;
195 
196             return new Handle(
197                     handle.getTag(),
198                     mapType(handle.getOwner()),
199                     isFieldHandle
200                             ? mapFieldName(handle.getOwner(), handle.getName(), handle.getDesc())
201                             : mapMethodName(handle.getOwner(), handle.getName(), handle.getDesc()),
202                     isFieldHandle ? mapDesc(handle.getDesc()) : mapMethodDesc(handle.getDesc()),
203                     handle.isInterface());
204         }
205         if (value instanceof ConstantDynamic) {
206             ConstantDynamic constantDynamic = (ConstantDynamic) value;
207             int bootstrapMethodArgumentCount = constantDynamic.getBootstrapMethodArgumentCount();
208             Object[] remappedBootstrapMethodArguments = new Object[bootstrapMethodArgumentCount];
209             for (int i = 0; i < bootstrapMethodArgumentCount; ++i) {
210                 remappedBootstrapMethodArguments[i] =
211                         mapValue(constantDynamic.getBootstrapMethodArgument(i));
212             }
213             String descriptor = constantDynamic.getDescriptor();
214             return new ConstantDynamic(
215                     mapInvokeDynamicMethodName(constantDynamic.getName(), descriptor),
216                     mapDesc(descriptor),
217                     (Handle) mapValue(constantDynamic.getBootstrapMethod()),
218                     remappedBootstrapMethodArguments);
219         }
220         return value;
221     }
222 
223     /**
224       * Returns the given signature, remapped with the {@link SignatureVisitor} returned by {@link
225       * #createSignatureRemapper(SignatureVisitor)}.
226       *
227       * @param signature a <i>JavaTypeSignature</i>, <i>ClassSignature</i> or <i>MethodSignature</i>.
228       * @param typeSignature whether the given signature is a <i>JavaTypeSignature</i>.
229       * @return signature the given signature, remapped with the {@link SignatureVisitor} returned by
230       *     {@link #createSignatureRemapper(SignatureVisitor)}.
231       */
232     public String mapSignature(final String signature, final boolean typeSignature) {
233         if (signature == null) {
234             return null;
235         }
236         SignatureReader signatureReader = new SignatureReader(signature);
237         SignatureWriter signatureWriter = new SignatureWriter();
238         SignatureVisitor signatureRemapper = createSignatureRemapper(signatureWriter);
239         if (typeSignature) {
240             signatureReader.acceptType(signatureRemapper);
241         } else {
242             signatureReader.accept(signatureRemapper);
243         }
244         return signatureWriter.toString();
245     }
246 
247     /**
248       * Constructs a new remapper for signatures. The default implementation of this method returns a
249       * new {@link SignatureRemapper}.
250       *
251       * @param signatureVisitor the SignatureVisitor the remapper must delegate to.
252       * @return the newly created remapper.
253       * @deprecated use {@link #createSignatureRemapper} instead.
254       */
255     @Deprecated
256     protected SignatureVisitor createRemappingSignatureAdapter(
257             final SignatureVisitor signatureVisitor) {
258         return createSignatureRemapper(signatureVisitor);
259     }
260 
261     /**
262       * Constructs a new remapper for signatures. The default implementation of this method returns a
263       * new {@link SignatureRemapper}.
264       *
265       * @param signatureVisitor the SignatureVisitor the remapper must delegate to.
266       * @return the newly created remapper.
267       */
268     protected SignatureVisitor createSignatureRemapper(final SignatureVisitor signatureVisitor) {
269         return new SignatureRemapper(signatureVisitor, this);
270     }
271 
272     /**
273       * Maps an annotation attribute name. The default implementation of this method returns the given
274       * name, unchanged. Subclasses can override.
275       *
276       * @param descriptor the descriptor of the annotation class.
277       * @param name the name of the annotation attribute.
278       * @return the new name of the annotation attribute.
279       */
280     public String mapAnnotationAttributeName(final String descriptor, final String name) {
281         return name;
282     }
283 
284     /**
285       * Maps an inner class name to its new name. The default implementation of this method provides a
286       * strategy that will work for inner classes produced by Java, but not necessarily other
287       * languages. Subclasses can override.
288       *
289       * @param name the fully-qualified internal name of the inner class.
290       * @param ownerName the internal name of the owner class of the inner class.
291       * @param innerName the internal name of the inner class.
292       * @return the new inner name of the inner class.
293       */
294     public String mapInnerClassName(
295             final String name, final String ownerName, final String innerName) {
296         final String remappedInnerName = this.mapType(name);
297         if (remappedInnerName.contains("$")) {
298             int index = remappedInnerName.lastIndexOf('$') + 1;
299             while (index < remappedInnerName.length()
300                     && Character.isDigit(remappedInnerName.charAt(index))) {
301                 index++;
302             }
303             return remappedInnerName.substring(index);
304         } else {
305             return innerName;
306         }
307     }
308 
309     /**
310       * Maps a method name to its new name. The default implementation of this method returns the given
311       * name, unchanged. Subclasses can override.
312       *
313       * @param owner the internal name of the owner class of the method.
314       * @param name the name of the method.
315       * @param descriptor the descriptor of the method.
316       * @return the new name of the method.
317       */
318     public String mapMethodName(final String owner, final String name, final String descriptor) {
319         return name;
320     }
321 
322     /**
323       * Maps an invokedynamic or a constant dynamic method name to its new name. The default
324       * implementation of this method returns the given name, unchanged. Subclasses can override.
325       *
326       * @param name the name of the method.
327       * @param descriptor the descriptor of the method.
328       * @return the new name of the method.
329       */
330     public String mapInvokeDynamicMethodName(final String name, final String descriptor) {
331         return name;
332     }
333 
334     /**
335       * Maps a record component name to its new name. The default implementation of this method returns
336       * the given name, unchanged. Subclasses can override.
337       *
338       * @param owner the internal name of the owner class of the field.
339       * @param name the name of the field.
340       * @param descriptor the descriptor of the field.
341       * @return the new name of the field.
342       */
343     public String mapRecordComponentName(
344             final String owner, final String name, final String descriptor) {
345         return name;
346     }
347 
348     /**
349       * Maps a field name to its new name. The default implementation of this method returns the given
350       * name, unchanged. Subclasses can override.
351       *
352       * @param owner the internal name of the owner class of the field.
353       * @param name the name of the field.
354       * @param descriptor the descriptor of the field.
355       * @return the new name of the field.
356       */
357     public String mapFieldName(final String owner, final String name, final String descriptor) {
358         return name;
359     }
360 
361     /**
362       * Maps a package name to its new name. The default implementation of this method returns the
363       * given name, unchanged. Subclasses can override.
364       *
365       * @param name the fully qualified name of the package (using dots).
366       * @return the new name of the package.
367       */
368     public String mapPackageName(final String name) {
369         return name;
370     }
371 
372     /**
373       * Maps a module name to its new name. The default implementation of this method returns the given
374       * name, unchanged. Subclasses can override.
375       *
376       * @param name the fully qualified name (using dots) of a module.
377       * @return the new name of the module.
378       */
379     public String mapModuleName(final String name) {
380         return name;
381     }
382 
383     /**
384       * Maps the internal name of a class to its new name. The default implementation of this method
385       * returns the given name, unchanged. Subclasses can override.
386       *
387       * @param internalName the internal name of a class.
388       * @return the new internal name.
389       */
390     public String map(final String internalName) {
391         return internalName;
392     }
393 }
394