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.AnnotationVisitor;
  63 import jdk.internal.org.objectweb.asm.Handle;
  64 import jdk.internal.org.objectweb.asm.Label;
  65 import jdk.internal.org.objectweb.asm.MethodVisitor;
  66 import jdk.internal.org.objectweb.asm.Opcodes;
  67 
  68 /**
  69  * A {@link LocalVariablesSorter} for type mapping.
  70  *
  71  * @author Eugene Kuleshov
  72  */
  73 public class RemappingMethodAdapter extends LocalVariablesSorter {
  74 
  75     protected final Remapper remapper;
  76 
  77     public RemappingMethodAdapter(
  78         final int access,
  79         final String desc,
  80         final MethodVisitor mv,
  81         final Remapper remapper)
  82     {
  83         this(Opcodes.ASM4, access, desc, mv, remapper);
  84     }
  85 
  86     protected RemappingMethodAdapter(
  87         final int api,
  88         final int access,
  89         final String desc,
  90         final MethodVisitor mv,
  91         final Remapper remapper)
  92     {
  93         super(api, access, desc, mv);
  94         this.remapper = remapper;
  95     }
  96 
  97     @Override
  98     public AnnotationVisitor visitAnnotationDefault() {
  99         AnnotationVisitor av = mv.visitAnnotationDefault();
 100         return av == null ? av : new RemappingAnnotationAdapter(av, remapper);
 101     }
 102 
 103     @Override
 104     public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
 105         AnnotationVisitor av = mv.visitAnnotation(remapper.mapDesc(desc), visible);
 106         return av == null ? av : new RemappingAnnotationAdapter(av, remapper);
 107     }
 108 
 109     @Override
 110     public AnnotationVisitor visitParameterAnnotation(
 111         int parameter,
 112         String desc,
 113         boolean visible)
 114     {
 115         AnnotationVisitor av = mv.visitParameterAnnotation(parameter,
 116                 remapper.mapDesc(desc),
 117                 visible);
 118         return av == null ? av : new RemappingAnnotationAdapter(av, remapper);
 119     }
 120 
 121     @Override
 122     public void visitFrame(
 123         int type,
 124         int nLocal,
 125         Object[] local,
 126         int nStack,
 127         Object[] stack)
 128     {
 129         super.visitFrame(type, nLocal, remapEntries(nLocal, local), nStack, remapEntries(nStack, stack));
 130     }
 131 
 132     private Object[] remapEntries(int n, Object[] entries) {
 133         for (int i = 0; i < n; i++) {
 134             if (entries[i] instanceof String) {
 135                 Object[] newEntries = new Object[n];
 136                 if (i > 0) {
 137                     System.arraycopy(entries, 0, newEntries, 0, i);
 138                 }
 139                 do {
 140                     Object t = entries[i];
 141                     newEntries[i++] = t instanceof String
 142                             ? remapper.mapType((String) t)
 143                             : t;
 144                 } while (i < n);
 145                 return newEntries;
 146             }
 147         }
 148         return entries;
 149     }
 150 
 151     @Override
 152     public void visitFieldInsn(
 153         int opcode,
 154         String owner,
 155         String name,
 156         String desc)
 157     {
 158         super.visitFieldInsn(opcode,
 159                 remapper.mapType(owner),
 160                 remapper.mapFieldName(owner, name, desc),
 161                 remapper.mapDesc(desc));
 162     }
 163 
 164     @Override
 165     public void visitMethodInsn(
 166         int opcode,
 167         String owner,
 168         String name,
 169         String desc)
 170     {
 171         super.visitMethodInsn(opcode,
 172                 remapper.mapType(owner),
 173                 remapper.mapMethodName(owner, name, desc),
 174                 remapper.mapMethodDesc(desc));
 175     }
 176 
 177     @Override
 178     public void visitInvokeDynamicInsn(
 179         String name,
 180         String desc,
 181         Handle bsm,
 182         Object... bsmArgs)
 183     {
 184         for(int i=0; i<bsmArgs.length; i++) {
 185             bsmArgs[i] = remapper.mapValue(bsmArgs[i]);
 186         }
 187         super.visitInvokeDynamicInsn(
 188                 remapper.mapInvokeDynamicMethodName(name, desc),
 189                 remapper.mapMethodDesc(desc),
 190                 (Handle)remapper.mapValue(bsm),
 191                 bsmArgs);
 192     }
 193 
 194     @Override
 195     public void visitTypeInsn(int opcode, String type) {
 196         super.visitTypeInsn(opcode, remapper.mapType(type));
 197     }
 198 
 199     @Override
 200     public void visitLdcInsn(Object cst) {
 201         super.visitLdcInsn(remapper.mapValue(cst));
 202     }
 203 
 204     @Override
 205     public void visitMultiANewArrayInsn(String desc, int dims) {
 206         super.visitMultiANewArrayInsn(remapper.mapDesc(desc), dims);
 207     }
 208 
 209     @Override
 210     public void visitTryCatchBlock(
 211         Label start,
 212         Label end,
 213         Label handler,
 214         String type)
 215     {
 216         super.visitTryCatchBlock(start, end, handler,
 217                 type == null ? null : remapper.mapType(type));
 218     }
 219 
 220     @Override
 221     public void visitLocalVariable(
 222         String name,
 223         String desc,
 224         String signature,
 225         Label start,
 226         Label end,
 227         int index)
 228     {
 229         super.visitLocalVariable(name,
 230                 remapper.mapDesc(desc),
 231                 remapper.mapSignature(signature, true),
 232                 start,
 233                 end,
 234                 index);
 235     }
 236 }