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.Handle; 63 import jdk.internal.org.objectweb.asm.Type; 64 import jdk.internal.org.objectweb.asm.signature.SignatureReader; 65 import jdk.internal.org.objectweb.asm.signature.SignatureVisitor; 66 import jdk.internal.org.objectweb.asm.signature.SignatureWriter; 67 68 /** 69 * A class responsible for remapping types and names. Subclasses can override 70 * the following methods: 71 * 72 * <ul> 73 * <li>{@link #map(String)} - map type</li> 74 * <li>{@link #mapFieldName(String, String, String)} - map field name</li> 75 * <li>{@link #mapMethodName(String, String, String)} - map method name</li> 76 * </ul> 77 * 78 * @author Eugene Kuleshov 79 */ 80 public abstract class Remapper { 81 82 public String mapDesc(String desc) { 83 Type t = Type.getType(desc); 84 switch (t.getSort()) { 85 case Type.ARRAY: 86 String s = mapDesc(t.getElementType().getDescriptor()); 87 for (int i = 0; i < t.getDimensions(); ++i) { 88 s = '[' + s; 89 } 90 return s; 91 case Type.OBJECT: 92 String newType = map(t.getInternalName()); 93 if (newType != null) { 94 return 'L' + newType + ';'; 95 } 96 } 97 return desc; 98 } 99 100 private Type mapType(Type t) { 101 switch (t.getSort()) { 102 case Type.ARRAY: 103 String s = mapDesc(t.getElementType().getDescriptor()); 104 for (int i = 0; i < t.getDimensions(); ++i) { 105 s = '[' + s; 106 } 107 return Type.getType(s); 108 case Type.OBJECT: 109 s = map(t.getInternalName()); 110 return s != null ? Type.getObjectType(s) : t; 111 case Type.METHOD: 112 return Type.getMethodType(mapMethodDesc(t.getDescriptor())); 113 } 114 return t; 115 } 116 117 public String mapType(String type) { 118 if (type == null) { 119 return null; 120 } 121 return mapType(Type.getObjectType(type)).getInternalName(); 122 } 123 124 public String[] mapTypes(String[] types) { 125 String[] newTypes = null; 126 boolean needMapping = false; 127 for (int i = 0; i < types.length; i++) { 128 String type = types[i]; 129 String newType = map(type); 130 if (newType != null && newTypes == null) { 131 newTypes = new String[types.length]; 132 if (i > 0) { 133 System.arraycopy(types, 0, newTypes, 0, i); 134 } 135 needMapping = true; 136 } 137 if (needMapping) { 138 newTypes[i] = newType == null ? type : newType; 139 } 140 } 141 return needMapping ? newTypes : types; 142 } 143 144 public String mapMethodDesc(String desc) { 145 if ("()V".equals(desc)) { 146 return desc; 147 } 148 149 Type[] args = Type.getArgumentTypes(desc); 150 StringBuilder sb = new StringBuilder("("); 151 for (int i = 0; i < args.length; i++) { 152 sb.append(mapDesc(args[i].getDescriptor())); 153 } 154 Type returnType = Type.getReturnType(desc); 155 if (returnType == Type.VOID_TYPE) { 156 sb.append(")V"); 157 return sb.toString(); 158 } 159 sb.append(')').append(mapDesc(returnType.getDescriptor())); 160 return sb.toString(); 161 } 162 163 public Object mapValue(Object value) { 164 if (value instanceof Type) { 165 return mapType((Type) value); 166 } 167 if (value instanceof Handle) { 168 Handle h = (Handle) value; 169 return new Handle(h.getTag(), mapType(h.getOwner()), mapMethodName( 170 h.getOwner(), h.getName(), h.getDesc()), 171 mapMethodDesc(h.getDesc()), h.isInterface()); 172 } 173 return value; 174 } 175 176 /** 177 * @param signature 178 * signature for mapper 179 * @param typeSignature 180 * true if signature is a FieldTypeSignature, such as the 181 * signature parameter of the ClassVisitor.visitField or 182 * MethodVisitor.visitLocalVariable methods 183 * @return signature rewritten as a string 184 */ 185 public String mapSignature(String signature, boolean typeSignature) { 186 if (signature == null) { 187 return null; 188 } 189 SignatureReader r = new SignatureReader(signature); 190 SignatureWriter w = new SignatureWriter(); 191 SignatureVisitor a = createSignatureRemapper(w); 192 if (typeSignature) { 193 r.acceptType(a); 194 } else { 195 r.accept(a); 196 } 197 return w.toString(); 198 } 199 200 /** 201 * @deprecated use {@link #createSignatureRemapper} instead. 202 */ 203 @Deprecated 204 protected SignatureVisitor createRemappingSignatureAdapter( 205 SignatureVisitor v) { 206 return new SignatureRemapper(v, this); 207 } 208 209 protected SignatureVisitor createSignatureRemapper( 210 SignatureVisitor v) { 211 return createRemappingSignatureAdapter(v); 212 } 213 214 /** 215 * Map method name to the new name. Subclasses can override. 216 * 217 * @param owner 218 * owner of the method. 219 * @param name 220 * name of the method. 221 * @param desc 222 * descriptor of the method. 223 * @return new name of the method 224 */ 225 public String mapMethodName(String owner, String name, String desc) { 226 return name; 227 } 228 229 /** 230 * Map invokedynamic method name to the new name. Subclasses can override. 231 * 232 * @param name 233 * name of the invokedynamic. 234 * @param desc 235 * descriptor of the invokedynamic. 236 * @return new invokdynamic name. 237 */ 238 public String mapInvokeDynamicMethodName(String name, String desc) { 239 return name; 240 } 241 242 /** 243 * Map field name to the new name. Subclasses can override. 244 * 245 * @param owner 246 * owner of the field. 247 * @param name 248 * name of the field 249 * @param desc 250 * descriptor of the field 251 * @return new name of the field. 252 */ 253 public String mapFieldName(String owner, String name, String desc) { 254 return name; 255 } 256 257 /** 258 * Map type name to the new name. Subclasses can override. 259 * 260 * @param typeName 261 * the type name 262 * @return new name, default implementation is the identity. 263 */ 264 public String map(String typeName) { 265 return typeName; 266 } 267 }