• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015 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 
17 package com.android.tools.layoutlib.create;
18 
19 import com.android.tools.layoutlib.create.ICreateInfo.InjectMethodRunnable;
20 
21 import org.objectweb.asm.ClassVisitor;
22 import org.objectweb.asm.MethodVisitor;
23 
24 import static org.objectweb.asm.Opcodes.ACC_PUBLIC;
25 import static org.objectweb.asm.Opcodes.ALOAD;
26 import static org.objectweb.asm.Opcodes.ARETURN;
27 import static org.objectweb.asm.Opcodes.INVOKEVIRTUAL;
28 
29 public class InjectMethodRunnables {
30     public static final ICreateInfo.InjectMethodRunnable CONTEXT_GET_FRAMEWORK_CLASS_LOADER
31             = new InjectMethodRunnable() {
32         @Override
33         public void generateMethods(Object classVisitor) {
34             assert classVisitor instanceof ClassVisitor;
35             ClassVisitor cv = (ClassVisitor) classVisitor;
36             // generated by compiling the class:
37             // class foo { public ClassLoader getFrameworkClassLoader() { return getClass().getClassLoader(); } }
38             // and then running ASMifier on it:
39             // java -classpath asm-debug-all-5.0.2.jar:. org.objectweb.asm.util.ASMifier foo
40             MethodVisitor mv = cv.visitMethod(ACC_PUBLIC, "getFrameworkClassLoader",
41                     "()Ljava/lang/ClassLoader;", null, null);
42             mv.visitCode();
43             mv.visitVarInsn(ALOAD, 0);
44             mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Object", "getClass",
45                     "()Ljava/lang/Class;", false);
46             mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Class", "getClassLoader",
47                     "()Ljava/lang/ClassLoader;", false);
48             mv.visitInsn(ARETURN);
49             mv.visitMaxs(1, 1);
50             mv.visitEnd();
51             // generated code ends.
52         }
53     };
54 }
55