Search in sources :

Example 16 with Method

use of org.objectweb.asm.commons.Method in project cdap by caskdata.

the class FieldAccessorGenerator method generateConstructor.

private void generateConstructor(Field field) {
    if (isPrivate) {
        classWriter.visitField(Opcodes.ACC_PRIVATE + Opcodes.ACC_FINAL, "field", Type.getDescriptor(Field.class), null, null).visitEnd();
    }
    // Constructor(Type classType)
    Method constructor = getMethod(void.class, "<init>", java.lang.reflect.Type.class);
    GeneratorAdapter mg = new GeneratorAdapter(Opcodes.ACC_PUBLIC, constructor, null, new Type[0], classWriter);
    mg.loadThis();
    mg.loadArg(0);
    mg.invokeConstructor(Type.getType(AbstractFieldAccessor.class), constructor);
    if (isPrivate) {
        initializeReflectionField(mg, field);
    }
    mg.returnValue();
    mg.endMethod();
}
Also used : GeneratorAdapter(org.objectweb.asm.commons.GeneratorAdapter) Method(org.objectweb.asm.commons.Method)

Example 17 with Method

use of org.objectweb.asm.commons.Method in project cdap by caskdata.

the class SparkClassRewriter method rewriteClient.

/**
   * Defines the org.apache.spark.deploy.yarn.Client class with rewriting of the createConfArchive method to
   * workaround the SPARK-13441 bug.
   */
@Nullable
private byte[] rewriteClient(InputStream byteCodeStream) throws IOException {
    // We only need to rewrite if listing either HADOOP_CONF_DIR or YARN_CONF_DIR return null.
    boolean needRewrite = false;
    for (String env : ImmutableList.of("HADOOP_CONF_DIR", "YARN_CONF_DIR")) {
        String value = System.getenv(env);
        if (value != null) {
            File path = new File(value);
            if (path.isDirectory() && path.listFiles() == null) {
                needRewrite = true;
                break;
            }
        }
    }
    // If rewrite is not needed
    if (!needRewrite) {
        return null;
    }
    ClassReader cr = new ClassReader(byteCodeStream);
    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
    cr.accept(new ClassVisitor(Opcodes.ASM5, cw) {

        @Override
        public MethodVisitor visitMethod(final int access, final String name, final String desc, String signature, String[] exceptions) {
            MethodVisitor mv = super.visitMethod(access, name, desc, signature, exceptions);
            // Only rewrite the createConfArchive method
            if (!"createConfArchive".equals(name)) {
                return mv;
            }
            // Check if it's a recognizable return type.
            // Spark 1.5+ return type is File
            boolean isReturnFile = Type.getReturnType(desc).equals(Type.getType(File.class));
            Type optionType = Type.getObjectType("scala/Option");
            if (!isReturnFile) {
                // Spark 1.4 return type is Option<File>
                if (!Type.getReturnType(desc).equals(optionType)) {
                    // Unknown type. Not going to modify the code.
                    return mv;
                }
            }
            // Generate this for Spark 1.5+
            // return SparkRuntimeUtils.createConfArchive(this.sparkConf, SPARK_CONF_FILE,
            //                                            LOCALIZED_CONF_DIR, LOCALIZED_CONF_DIR_ZIP);
            // Generate this for Spark 1.4
            // return Option.apply(SparkRuntimeUtils.createConfArchive(this.sparkConf, SPARK_CONF_FILE,
            //                                                         LOCALIZED_CONF_DIR, LOCALIZED_CONF_DIR_ZIP));
            GeneratorAdapter mg = new GeneratorAdapter(mv, access, name, desc);
            // load this.sparkConf to the stack
            mg.loadThis();
            mg.getField(Type.getObjectType("org/apache/spark/deploy/yarn/Client"), "sparkConf", SPARK_CONF_TYPE);
            // push three constants to the stack
            mg.visitLdcInsn(SPARK_CONF_FILE);
            mg.visitLdcInsn(LOCALIZED_CONF_DIR);
            mg.visitLdcInsn(LOCALIZED_CONF_DIR_ZIP);
            // call SparkRuntimeUtils.createConfArchive, return a File and leave it in stack
            Type stringType = Type.getType(String.class);
            mg.invokeStatic(SPARK_RUNTIME_UTILS_TYPE, new Method("createConfArchive", Type.getType(File.class), new Type[] { SPARK_CONF_TYPE, stringType, stringType, stringType }));
            if (isReturnFile) {
                // Spark 1.5+ return type is File, hence just return the File from the stack
                mg.returnValue();
                mg.endMethod();
            } else {
                // Spark 1.4 return type is Option<File>
                // return Option.apply(<file from stack>);
                // where the file is actually just popped from the stack
                mg.invokeStatic(optionType, new Method("apply", optionType, new Type[] { Type.getType(Object.class) }));
                mg.checkCast(optionType);
                mg.returnValue();
                mg.endMethod();
            }
            return null;
        }
    }, ClassReader.EXPAND_FRAMES);
    return cw.toByteArray();
}
Also used : ClassVisitor(org.objectweb.asm.ClassVisitor) Method(org.objectweb.asm.commons.Method) ClassWriter(org.objectweb.asm.ClassWriter) MethodVisitor(org.objectweb.asm.MethodVisitor) Type(org.objectweb.asm.Type) ClassReader(org.objectweb.asm.ClassReader) GeneratorAdapter(org.objectweb.asm.commons.GeneratorAdapter) File(java.io.File) Nullable(javax.annotation.Nullable)

Example 18 with Method

use of org.objectweb.asm.commons.Method in project eclipse.themes.darker by jinmingjian.

the class DarkerWeavingHook method transform.

public void transform(WovenClass wovenClass) {
    byte[] bytes = wovenClass.getBytes();
    ClassReader cr = new ClassReader(bytes);
    ClassWriter cw = new ClassWriter(cr, ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES);
    cr.accept(cw, 0);
    Method md = Method.getMethod("org.eclipse.swt.graphics.Color getForeground ()");
    MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, md.getName(), md.getDescriptor(), null, null);
    final GeneratorAdapter mg = new GeneratorAdapter(ACC_PUBLIC, md, mv);
    final Label LABEL_SKIP_CALL_TO_SUPER = new Label();
    final Label LABEL_LOOP_CMP = new Label();
    final Label LABEL_LOOP_START = new Label();
    mg.newInstance(Type.getType(Exception.class));
    mg.dup();
    mg.invokeConstructor(Type.getType(Exception.class), Method.getMethod("void <init> ()"));
    mg.invokeVirtual(Type.getType(Exception.class), Method.getMethod("StackTraceElement[] getStackTrace ()"));
    mg.storeLocal(1, Type.getType(StackTraceElement[].class));
    mg.push(10);
    mg.storeLocal(2, Type.getType(int.class));
    mg.goTo(LABEL_LOOP_CMP);
    mg.mark(LABEL_LOOP_START);
    mg.loadLocal(1);
    mg.loadLocal(2);
    mg.arrayLoad(Type.getType(StackTraceElement.class));
    mg.invokeVirtual(Type.getType(StackTraceElement.class), Method.getMethod("String getClassName ()"));
    mg.push("Dialog");
    mg.invokeVirtual(Type.getType(String.class), Method.getMethod("int indexOf (String)"));
    mg.push(-1);
    mg.ifICmp(GeneratorAdapter.EQ, LABEL_SKIP_CALL_TO_SUPER);
    //    //XXX: DEBUG
    //    mg.getStatic(Type.getType(System.class), "out", Type.getType(java.io.PrintStream.class));
    //    mg.push("2");
    //    mg.invokeVirtual(Type.getType(java.io.PrintStream.class), Method.getMethod("void println (String)"));
    //    //XXX: DEBUG
    mg.loadThis();
    mg.invokeConstructor(Type.getType("Lorg/eclipse/swt/widgets/Control;"), Method.getMethod("org.eclipse.swt.graphics.Color getForeground ()"));
    mg.returnValue();
    mg.mark(LABEL_SKIP_CALL_TO_SUPER);
    mg.iinc(2, 1);
    mg.mark(LABEL_LOOP_CMP);
    mg.loadLocal(2);
    mg.loadLocal(1);
    mg.arrayLength();
    mg.ifICmp(GeneratorAdapter.LT, LABEL_LOOP_START);
    mg.loadThis();
    mg.invokeVirtual(Type.getType("Lorg/eclipse/swt/custom/CLabel;"), Method.getMethod("org.eclipse.swt.widgets.Display getDisplay ()"));
    mg.push(1);
    mg.invokeVirtual(Type.getType("Lorg/eclipse/swt/widgets/Display;"), Method.getMethod("org.eclipse.swt.graphics.Color getSystemColor (int)"));
    mg.returnValue();
    mg.endMethod();
    cw.visitEnd();
    wovenClass.setBytes(cw.toByteArray());
}
Also used : Label(org.objectweb.asm.Label) ClassReader(org.objectweb.asm.ClassReader) GeneratorAdapter(org.objectweb.asm.commons.GeneratorAdapter) Method(org.objectweb.asm.commons.Method) ClassWriter(org.objectweb.asm.ClassWriter) IOException(java.io.IOException) MethodVisitor(org.objectweb.asm.MethodVisitor)

Example 19 with Method

use of org.objectweb.asm.commons.Method in project MinecraftForge by MinecraftForge.

the class ASMTransformerWrapper method makeWrapper.

private static byte[] makeWrapper(String fileName) {
    if (!wrapperModMap.containsKey(fileName) || !wrapperParentMap.containsKey(fileName) || !fileName.endsWith(".class")) {
        throw new IllegalArgumentException("makeWrapper called with strange argument: " + fileName);
    }
    String name = fileName.substring(0, fileName.length() - ".class".length());
    try {
        Type wrapper = Type.getType(TransformerWrapper.class);
        ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
        writer.visit(Opcodes.V1_6, Opcodes.ACC_PUBLIC, name, null, wrapper.getInternalName(), null);
        Method m = Method.getMethod("void <init> ()");
        GeneratorAdapter mg = new GeneratorAdapter(Opcodes.ACC_PUBLIC, m, null, null, writer);
        mg.loadThis();
        mg.invokeConstructor(wrapper, m);
        mg.returnValue();
        mg.endMethod();
        m = Method.getMethod("java.lang.String getParentClass ()");
        mg = new GeneratorAdapter(Opcodes.ACC_PROTECTED, m, null, null, writer);
        mg.push(wrapperParentMap.get(fileName));
        mg.returnValue();
        mg.endMethod();
        m = Method.getMethod("java.lang.String getCoreMod ()");
        mg = new GeneratorAdapter(Opcodes.ACC_PROTECTED, m, null, null, writer);
        mg.push(wrapperModMap.get(fileName));
        mg.returnValue();
        mg.endMethod();
        writer.visitEnd();
        return writer.toByteArray();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : Type(org.objectweb.asm.Type) GeneratorAdapter(org.objectweb.asm.commons.GeneratorAdapter) Method(org.objectweb.asm.commons.Method) ClassWriter(org.objectweb.asm.ClassWriter) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException)

Example 20 with Method

use of org.objectweb.asm.commons.Method in project robolectric by robolectric.

the class ProxyMaker method createProxyFactory.

<T> Factory createProxyFactory(Class<T> targetClass) {
    Type targetType = Type.getType(targetClass);
    String targetName = targetType.getInternalName();
    ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
    writer.visit(V1_7, ACC_PUBLIC | ACC_SUPER | ACC_FINAL, PROXY_NAME, null, targetName, null);
    writer.visitField(ACC_PUBLIC, TARGET_FIELD, targetType.getDescriptor(), null, null);
    for (java.lang.reflect.Method method : targetClass.getMethods()) {
        if (!shouldProxy(method))
            continue;
        Method proxyMethod = Method.getMethod(method);
        GeneratorAdapter m = new GeneratorAdapter(ACC_PUBLIC, Method.getMethod(method), null, null, writer);
        m.loadThis();
        m.getField(PROXY_TYPE, TARGET_FIELD, targetType);
        m.loadArgs();
        String targetMethod = methodMapper.getName(targetClass.getName(), method.getName());
        // In Java 8 we could use invokespecial here but not in 7, from jvm spec:
        // If an invokespecial instruction names a method which is not an instance
        // initialization method, then the type of the target reference on the operand
        // stack must be assignment compatible with the current class (JLS ยง5.2).
        m.visitMethodInsn(INVOKEVIRTUAL, targetName, targetMethod, proxyMethod.getDescriptor(), false);
        m.returnValue();
        m.endMethod();
    }
    writer.visitEnd();
    final Class<?> proxyClass = UNSAFE.defineAnonymousClass(targetClass, writer.toByteArray(), null);
    try {
        final MethodHandle setter = LOOKUP.findSetter(proxyClass, TARGET_FIELD, targetClass);
        return new Factory() {

            @Override
            public <E> E createProxy(Class<E> targetClass, E target) {
                try {
                    Object proxy = UNSAFE.allocateInstance(proxyClass);
                    setter.invoke(proxy, target);
                    return targetClass.cast(proxy);
                } catch (Throwable t) {
                    throw new AssertionError(t);
                }
            }
        };
    } catch (IllegalAccessException | NoSuchFieldException e) {
        throw new AssertionError(e);
    }
}
Also used : Method(org.objectweb.asm.commons.Method) ClassWriter(org.objectweb.asm.ClassWriter) Type(org.objectweb.asm.Type) GeneratorAdapter(org.objectweb.asm.commons.GeneratorAdapter) MethodHandle(java.lang.invoke.MethodHandle)

Aggregations

Method (org.objectweb.asm.commons.Method)32 GeneratorAdapter (org.objectweb.asm.commons.GeneratorAdapter)24 Type (org.objectweb.asm.Type)16 Label (org.objectweb.asm.Label)8 ClassWriter (org.objectweb.asm.ClassWriter)7 MethodVisitor (org.objectweb.asm.MethodVisitor)7 IOException (java.io.IOException)6 ClassReader (org.objectweb.asm.ClassReader)5 Schema (co.cask.cdap.api.data.schema.Schema)4 Set (java.util.Set)3 ClassVisitor (org.objectweb.asm.ClassVisitor)3 TypeToken (com.google.common.reflect.TypeToken)2 InvocationHandler (java.lang.reflect.InvocationHandler)2 SchemaHash (co.cask.cdap.api.data.schema.SchemaHash)1 MetricsContext (co.cask.cdap.api.metrics.MetricsContext)1 Encoder (co.cask.cdap.common.io.Encoder)1 Sets (com.google.common.collect.Sets)1 JClassType (com.google.gwt.core.ext.typeinfo.JClassType)1 InterceptorType (com.navercorp.pinpoint.profiler.instrument.interceptor.InterceptorType)1 File (java.io.File)1