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             // FIXME: support Q-type
 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             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             int index = remappedInnerName.lastIndexOf('$') + 1;
285             while (index < remappedInnerName.length()
286                     && Character.isDigit(remappedInnerName.charAt(index))) {
287                 index++;
288             }
289             return remappedInnerName.substring(index);
290         } else {
291             return innerName;
292         }
293     }
294 
295     /**
296       * Maps a method name to its new name. The default implementation of this method returns the given
297       * name, unchanged. Subclasses can override.
298       *
299       * @param owner the internal name of the owner class of the method.
300       * @param name the name of the method.
301       * @param descriptor the descriptor of the method.
302       * @return the new name of the method.
303       */
304     public String mapMethodName(final String owner, final String name, final String descriptor) {
305         return name;
306     }
307 
308     /**
309       * Maps an invokedynamic or a constant dynamic method name to its new name. The default
310       * implementation of this method returns the given name, unchanged. Subclasses can override.
311       *
312       * @param name the name of the method.
313       * @param descriptor the descriptor of the method.
314       * @return the new name of the method.
315       */
316     public String mapInvokeDynamicMethodName(final String name, final String descriptor) {
317         return name;
318     }
319 
320     /**
321       * Maps a record component name to its new name. The default implementation of this method returns
322       * the given name, unchanged. Subclasses can override.
323       *
324       * @param owner the internal name of the owner class of the field.
325       * @param name the name of the field.
326       * @param descriptor the descriptor of the field.
327       * @return the new name of the field.
328       */
329     public String mapRecordComponentName(
330             final String owner, final String name, final String descriptor) {
331         return name;
332     }
333 
334     /**
335       * Maps a field name to its new name. The default implementation of this method returns the given
336       * 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 mapFieldName(final String owner, final String name, final String descriptor) {
344         return name;
345     }
346 
347     /**
348       * Maps a package name to its new name. The default implementation of this method returns the
349       * given name, unchanged. Subclasses can override.
350       *
351       * @param name the fully qualified name of the package (using dots).
352       * @return the new name of the package.
353       */
354     public String mapPackageName(final String name) {
355         return name;
356     }
357 
358     /**
359       * Maps a module name to its new name. The default implementation of this method returns the given
360       * name, unchanged. Subclasses can override.
361       *
362       * @param name the fully qualified name (using dots) of a module.
363       * @return the new name of the module.
364       */
365     public String mapModuleName(final String name) {
366         return name;
367     }
368 
369     /**
370       * Maps the internal name of a class to its new name. The default implementation of this method
371       * returns the given name, unchanged. Subclasses can override.
372       *
373       * @param internalName the internal name of a class.
374       * @return the new internal name.
375       */
376     public String map(final String internalName) {
377         return internalName;
378     }
379 }