ReplaceMethodCallsAdapter.java revision e1960cc0b541cda93db94de5bef42dff922b9ec3
11160e6d2f7018117b0c29a7e2adba9ece36faec1Deepanshu Gupta/*
21160e6d2f7018117b0c29a7e2adba9ece36faec1Deepanshu Gupta * Copyright (C) 2014 The Android Open Source Project
31160e6d2f7018117b0c29a7e2adba9ece36faec1Deepanshu Gupta *
41160e6d2f7018117b0c29a7e2adba9ece36faec1Deepanshu Gupta * Licensed under the Apache License, Version 2.0 (the "License");
51160e6d2f7018117b0c29a7e2adba9ece36faec1Deepanshu Gupta * you may not use this file except in compliance with the License.
61160e6d2f7018117b0c29a7e2adba9ece36faec1Deepanshu Gupta * You may obtain a copy of the License at
71160e6d2f7018117b0c29a7e2adba9ece36faec1Deepanshu Gupta *
81160e6d2f7018117b0c29a7e2adba9ece36faec1Deepanshu Gupta *      http://www.apache.org/licenses/LICENSE-2.0
91160e6d2f7018117b0c29a7e2adba9ece36faec1Deepanshu Gupta *
101160e6d2f7018117b0c29a7e2adba9ece36faec1Deepanshu Gupta * Unless required by applicable law or agreed to in writing, software
111160e6d2f7018117b0c29a7e2adba9ece36faec1Deepanshu Gupta * distributed under the License is distributed on an "AS IS" BASIS,
121160e6d2f7018117b0c29a7e2adba9ece36faec1Deepanshu Gupta * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
131160e6d2f7018117b0c29a7e2adba9ece36faec1Deepanshu Gupta * See the License for the specific language governing permissions and
141160e6d2f7018117b0c29a7e2adba9ece36faec1Deepanshu Gupta * limitations under the License.
151160e6d2f7018117b0c29a7e2adba9ece36faec1Deepanshu Gupta */
161160e6d2f7018117b0c29a7e2adba9ece36faec1Deepanshu Gupta
171160e6d2f7018117b0c29a7e2adba9ece36faec1Deepanshu Guptapackage com.android.tools.layoutlib.create;
181160e6d2f7018117b0c29a7e2adba9ece36faec1Deepanshu Gupta
191160e6d2f7018117b0c29a7e2adba9ece36faec1Deepanshu Guptaimport org.objectweb.asm.ClassVisitor;
201160e6d2f7018117b0c29a7e2adba9ece36faec1Deepanshu Guptaimport org.objectweb.asm.MethodVisitor;
211160e6d2f7018117b0c29a7e2adba9ece36faec1Deepanshu Guptaimport org.objectweb.asm.Opcodes;
221160e6d2f7018117b0c29a7e2adba9ece36faec1Deepanshu Gupta
23e1960cc0b541cda93db94de5bef42dff922b9ec3Deepanshu Guptaimport java.util.Arrays;
24e1960cc0b541cda93db94de5bef42dff922b9ec3Deepanshu Guptaimport java.util.HashSet;
25e1960cc0b541cda93db94de5bef42dff922b9ec3Deepanshu Guptaimport java.util.Set;
26e1960cc0b541cda93db94de5bef42dff922b9ec3Deepanshu Gupta
271160e6d2f7018117b0c29a7e2adba9ece36faec1Deepanshu Gupta/**
281160e6d2f7018117b0c29a7e2adba9ece36faec1Deepanshu Gupta * Replaces calls to certain methods that do not exist in the Desktop VM.
291160e6d2f7018117b0c29a7e2adba9ece36faec1Deepanshu Gupta */
301160e6d2f7018117b0c29a7e2adba9ece36faec1Deepanshu Guptapublic class ReplaceMethodCallsAdapter extends ClassVisitor {
31e1960cc0b541cda93db94de5bef42dff922b9ec3Deepanshu Gupta
32e1960cc0b541cda93db94de5bef42dff922b9ec3Deepanshu Gupta    /**
33e1960cc0b541cda93db94de5bef42dff922b9ec3Deepanshu Gupta     * Descriptors for specialized versions {@link System#arraycopy} that are not present on the
34e1960cc0b541cda93db94de5bef42dff922b9ec3Deepanshu Gupta     * Desktop VM.
35e1960cc0b541cda93db94de5bef42dff922b9ec3Deepanshu Gupta     */
36e1960cc0b541cda93db94de5bef42dff922b9ec3Deepanshu Gupta    private static Set<String> ARRAYCOPY_DESCRIPTORS = new HashSet<String>(Arrays.asList(
37e1960cc0b541cda93db94de5bef42dff922b9ec3Deepanshu Gupta            "([CI[CII)V", "([BI[BII)V", "([SI[SII)V", "([II[III)V",
38e1960cc0b541cda93db94de5bef42dff922b9ec3Deepanshu Gupta            "([JI[JII)V", "([FI[FII)V", "([DI[DII)V", "([ZI[ZII)V"));
39e1960cc0b541cda93db94de5bef42dff922b9ec3Deepanshu Gupta
401160e6d2f7018117b0c29a7e2adba9ece36faec1Deepanshu Gupta    public ReplaceMethodCallsAdapter(ClassVisitor cv) {
411160e6d2f7018117b0c29a7e2adba9ece36faec1Deepanshu Gupta        super(Opcodes.ASM4, cv);
421160e6d2f7018117b0c29a7e2adba9ece36faec1Deepanshu Gupta    }
431160e6d2f7018117b0c29a7e2adba9ece36faec1Deepanshu Gupta
441160e6d2f7018117b0c29a7e2adba9ece36faec1Deepanshu Gupta    @Override
451160e6d2f7018117b0c29a7e2adba9ece36faec1Deepanshu Gupta    public MethodVisitor visitMethod(int access, String name, String desc, String signature,
461160e6d2f7018117b0c29a7e2adba9ece36faec1Deepanshu Gupta            String[] exceptions) {
471160e6d2f7018117b0c29a7e2adba9ece36faec1Deepanshu Gupta        return new MyMethodVisitor(super.visitMethod(access, name, desc, signature, exceptions));
481160e6d2f7018117b0c29a7e2adba9ece36faec1Deepanshu Gupta    }
491160e6d2f7018117b0c29a7e2adba9ece36faec1Deepanshu Gupta
501160e6d2f7018117b0c29a7e2adba9ece36faec1Deepanshu Gupta    private class MyMethodVisitor extends MethodVisitor {
511160e6d2f7018117b0c29a7e2adba9ece36faec1Deepanshu Gupta
521160e6d2f7018117b0c29a7e2adba9ece36faec1Deepanshu Gupta        public MyMethodVisitor(MethodVisitor mv) {
531160e6d2f7018117b0c29a7e2adba9ece36faec1Deepanshu Gupta            super(Opcodes.ASM4, mv);
541160e6d2f7018117b0c29a7e2adba9ece36faec1Deepanshu Gupta        }
551160e6d2f7018117b0c29a7e2adba9ece36faec1Deepanshu Gupta
561160e6d2f7018117b0c29a7e2adba9ece36faec1Deepanshu Gupta        @Override
571160e6d2f7018117b0c29a7e2adba9ece36faec1Deepanshu Gupta        public void visitMethodInsn(int opcode, String owner, String name, String desc) {
58e1960cc0b541cda93db94de5bef42dff922b9ec3Deepanshu Gupta            // Check if method is a specialized version of java.lang.System.arrayCopy
59e1960cc0b541cda93db94de5bef42dff922b9ec3Deepanshu Gupta            if (owner.equals("java/lang/System") && name.equals("arraycopy")) {
60e1960cc0b541cda93db94de5bef42dff922b9ec3Deepanshu Gupta
61e1960cc0b541cda93db94de5bef42dff922b9ec3Deepanshu Gupta                if (ARRAYCOPY_DESCRIPTORS.contains(desc)) {
62e1960cc0b541cda93db94de5bef42dff922b9ec3Deepanshu Gupta                    desc = "(Ljava/lang/Object;ILjava/lang/Object;II)V";
63e1960cc0b541cda93db94de5bef42dff922b9ec3Deepanshu Gupta                }
641160e6d2f7018117b0c29a7e2adba9ece36faec1Deepanshu Gupta            }
651160e6d2f7018117b0c29a7e2adba9ece36faec1Deepanshu Gupta            super.visitMethodInsn(opcode, owner, name, desc);
661160e6d2f7018117b0c29a7e2adba9ece36faec1Deepanshu Gupta        }
671160e6d2f7018117b0c29a7e2adba9ece36faec1Deepanshu Gupta    }
681160e6d2f7018117b0c29a7e2adba9ece36faec1Deepanshu Gupta}
69