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 StringBuffer s = new StringBuffer("("); 151 for (int i = 0; i < args.length; i++) { 152 s.append(mapDesc(args[i].getDescriptor())); 153 } 154 Type returnType = Type.getReturnType(desc); 155 if (returnType == Type.VOID_TYPE) { 156 s.append(")V"); 157 return s.toString(); 158 } 159 s.append(')').append(mapDesc(returnType.getDescriptor())); 160 return s.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())); 172 } 173 return value; 174 } 175 176 /** 177 * 178 * @param typeSignature 179 * true if signature is a FieldTypeSignature, such as the 180 * signature parameter of the ClassVisitor.visitField or 181 * MethodVisitor.visitLocalVariable methods 182 */ 183 public String mapSignature(String signature, boolean typeSignature) { 184 if (signature == null) { 185 return null; 186 } 187 SignatureReader r = new SignatureReader(signature); 188 SignatureWriter w = new SignatureWriter(); 189 SignatureVisitor a = createRemappingSignatureAdapter(w); 190 if (typeSignature) { 191 r.acceptType(a); 192 } else { 193 r.accept(a); 194 } 195 return w.toString(); 196 } 197 198 protected SignatureVisitor createRemappingSignatureAdapter( 199 SignatureVisitor v) { 200 return new RemappingSignatureAdapter(v, this); 201 } 202 203 /** 204 * Map method name to the new name. Subclasses can override. 205 * 206 * @param owner 207 * owner of the method. 208 * @param name 209 * name of the method. 210 * @param desc 211 * descriptor of the method. 212 * @return new name of the method 213 */ 214 public String mapMethodName(String owner, String name, String desc) { 215 return name; 216 } 217 218 /** 219 * Map invokedynamic method name to the new name. Subclasses can override. 220 * 221 * @param name 222 * name of the invokedynamic. 223 * @param desc 224 * descriptor of the invokedynamic. 225 * @return new invokdynamic name. 226 */ 227 public String mapInvokeDynamicMethodName(String name, String desc) { 228 return name; 229 } 230 231 /** 232 * Map field name to the new name. Subclasses can override. 233 * 234 * @param owner 235 * owner of the field. 236 * @param name 237 * name of the field 238 * @param desc 239 * descriptor of the field 240 * @return new name of the field. 241 */ 242 public String mapFieldName(String owner, String name, String desc) { 243 return name; 244 } 245 246 /** 247 * Map type name to the new name. Subclasses can override. 248 */ 249 public String map(String typeName) { 250 return typeName; 251 } 252 }