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