DelegateClassAdapter.java revision caed59d90db8626462baaec351e66b2a3280dc34
1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.tools.layoutlib.create;
18
19import org.objectweb.asm.ClassAdapter;
20import org.objectweb.asm.ClassVisitor;
21import org.objectweb.asm.MethodVisitor;
22import org.objectweb.asm.Opcodes;
23
24import java.util.Set;
25
26/**
27 * A {@link DelegateClassAdapter} can transform some methods from a class into
28 * delegates that defer the call to an associated delegate class.
29 * <p/>
30 * This is used to override specific methods and or all native methods in classes.
31 */
32public class DelegateClassAdapter extends ClassAdapter {
33
34    /** Suffix added to original methods. */
35    private static final String ORIGINAL_SUFFIX = "_Original";
36    private static String CONSTRUCTOR = "<init>";
37    private static String CLASS_INIT = "<clinit>";
38
39    public final static String ALL_NATIVES = "<<all_natives>>";
40
41    private final String mClassName;
42    private final Set<String> mDelegateMethods;
43    private final Log mLog;
44
45    /**
46     * Creates a new {@link DelegateClassAdapter} that can transform some methods
47     * from a class into delegates that defer the call to an associated delegate class.
48     * <p/>
49     * This is used to override specific methods and or all native methods in classes.
50     *
51     * @param log The logger object. Must not be null.
52     * @param cv the class visitor to which this adapter must delegate calls.
53     * @param className The internal class name of the class to visit,
54     *          e.g. <code>com/android/SomeClass$InnerClass</code>.
55     * @param delegateMethods The set of method names to modify and/or the
56     *          special constant {@link #ALL_NATIVES} to convert all native methods.
57     */
58    public DelegateClassAdapter(Log log,
59            ClassVisitor cv,
60            String className,
61            Set<String> delegateMethods) {
62        super(cv);
63        mLog = log;
64        mClassName = className;
65        mDelegateMethods = delegateMethods;
66    }
67
68    //----------------------------------
69    // Methods from the ClassAdapter
70
71    @Override
72    public MethodVisitor visitMethod(int access, String name, String desc,
73            String signature, String[] exceptions) {
74
75        boolean isStatic = (access & Opcodes.ACC_STATIC) != 0;
76        boolean isNative = (access & Opcodes.ACC_NATIVE) != 0;
77
78        boolean useDelegate = (isNative && mDelegateMethods.contains(ALL_NATIVES)) ||
79                              mDelegateMethods.contains(name);
80
81        if (!useDelegate) {
82            // Not creating a delegate for this method, pass it as-is from the reader
83            // to the writer.
84            return super.visitMethod(access, name, desc, signature, exceptions);
85        }
86
87        if (useDelegate) {
88            if (CONSTRUCTOR.equals(name) || CLASS_INIT.equals(name)) {
89                // We don't currently support generating delegates for constructors.
90                throw new UnsupportedOperationException(
91                    String.format(
92                        "Delegate doesn't support overriding constructor %1$s:%2$s(%3$s)",  //$NON-NLS-1$
93                        mClassName, name, desc));
94            }
95        }
96
97        if (isNative) {
98            // Remove native flag
99            access = access & ~Opcodes.ACC_NATIVE;
100            MethodVisitor mwDelegate = super.visitMethod(access, name, desc, signature, exceptions);
101
102            DelegateMethodAdapter2 a = new DelegateMethodAdapter2(
103                    mLog, null /*mwOriginal*/, mwDelegate, mClassName, name, desc, isStatic);
104
105            // A native has no code to visit, so we need to generate it directly.
106            a.generateDelegateCode();
107
108            return mwDelegate;
109        }
110
111        // Given a non-native SomeClass.MethodName(), we want to generate 2 methods:
112        // - A copy of the original method named SomeClass.MethodName_Original().
113        //   The content is the original method as-is from the reader.
114        // - A brand new implementation of SomeClass.MethodName() which calls to a
115        //   non-existing method named SomeClass_Delegate.MethodName().
116        //   The implementation of this 'delegate' method is done in layoutlib_brigde.
117
118        int accessDelegate = access;
119        // change access to public for the original one
120        if (Main.sOptions.generatePublicAccess) {
121            access &= ~(Opcodes.ACC_PROTECTED | Opcodes.ACC_PRIVATE);
122            access |= Opcodes.ACC_PUBLIC;
123        }
124
125        MethodVisitor mwOriginal = super.visitMethod(access, name + ORIGINAL_SUFFIX,
126                                                     desc, signature, exceptions);
127        MethodVisitor mwDelegate = super.visitMethod(accessDelegate, name,
128                                                     desc, signature, exceptions);
129
130        DelegateMethodAdapter2 a = new DelegateMethodAdapter2(
131                mLog, mwOriginal, mwDelegate, mClassName, name, desc, isStatic);
132        return a;
133    }
134}
135