Search in sources :

Example 1 with ClassVisitor

use of jdk.internal.org.objectweb.asm.ClassVisitor in project warn-report by saaavsaaa.

the class MultiClassAdapter method visit.

private static void visit() throws IOException {
    String path = "code.record.WaitClearCode";
    ClassReader cr = new ClassReader(path);
    ClassWriter cw = new ClassWriter(0);
    // cv 将所有事件转发给 cw
    ClassVisitor cv = new ClassVisitor(ASM5, cw) {
    };
    // ClassReader cr = new ClassReader(b1);
    cr.accept(cv, 0);
    byte[] b2 = cw.toByteArray();
}
Also used : ClassReader(jdk.internal.org.objectweb.asm.ClassReader) ClassVisitor(jdk.internal.org.objectweb.asm.ClassVisitor) ClassWriter(jdk.internal.org.objectweb.asm.ClassWriter)

Example 2 with ClassVisitor

use of jdk.internal.org.objectweb.asm.ClassVisitor in project warn-report by saaavsaaa.

the class DelegageClassAdapter method extendsRun.

// 与 ChangeVersionAdapter 的程序图进行对比,可以看出,ca 和 cw 之间的事
// 件发生在 cr 和 ca 之间的事件之后,而不是像正常类适配器一样同时进行。事实上,对于所有
// 基于树的转换都是如此,同时还解释了为什么它们受到的限制要少于基于事件的转换。
private void extendsRun() throws IOException {
    ClassWriter cw = new ClassWriter(0);
    ClassVisitor ca = new ExtendsClassAdapter(cw);
    ClassReader cr = new ClassReader("...");
    cr.accept(ca, 0);
    byte[] b = cw.toByteArray();
}
Also used : ClassReader(jdk.internal.org.objectweb.asm.ClassReader) ClassVisitor(jdk.internal.org.objectweb.asm.ClassVisitor) ClassWriter(jdk.internal.org.objectweb.asm.ClassWriter)

Example 3 with ClassVisitor

use of jdk.internal.org.objectweb.asm.ClassVisitor in project openj9 by eclipse.

the class Transformer method transform.

public byte[] transform(ClassLoader loader, String className, Class<?> classToTransform, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException {
    try {
        if (flag == MODIFY_EXISTING_METHOD) {
            // for fast HCR
            AgentMain.logger.debug("Instrumenting " + className + " by modifying existing method.");
            ClassReader reader = new ClassReader(classToTransform.getCanonicalName());
            ClassWriter cw = new ClassWriter(reader, ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
            ClassVisitor visitor = new CustomClassVisitor(cw, classToTransform.getCanonicalName());
            reader.accept(visitor, ClassReader.EXPAND_FRAMES);
            return cw.toByteArray();
        } else if (flag == ADD_NEW_METHOD) {
            // for extended HCR
            AgentMain.logger.debug("Instrumenting " + className + " by adding a new method.");
            ClassReader reader = new ClassReader(classToTransform.getCanonicalName());
            ClassWriter cw = new ClassWriter(reader, ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
            ClassVisitor cv = new MethodAddAdapter(new CheckClassAdapter(cw));
            reader.accept(cv, ClassReader.SKIP_FRAMES);
            return cw.toByteArray();
        } else if (flag == ADD_NEW_FIELD) {
            // for GCRetransform test
            AgentMain.logger.debug("Instrumenting " + className + " by adding a new field.");
            ClassReader reader = new ClassReader(classToTransform.getCanonicalName());
            ClassWriter cw = new ClassWriter(reader, ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
            FieldNode newField = new FieldNode(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, "new_static_int_array", "[I", null, null);
            ClassVisitor cv = new FieldAdder(new CheckClassAdapter(cw), newField, className, _methodName);
            reader.accept(cv, ClassReader.SKIP_FRAMES);
            return cw.toByteArray();
        }
        return null;
    } catch (IOException e) {
        return null;
    }
}
Also used : FieldNode(jdk.internal.org.objectweb.asm.tree.FieldNode) CheckClassAdapter(jdk.internal.org.objectweb.asm.util.CheckClassAdapter) ClassReader(jdk.internal.org.objectweb.asm.ClassReader) ClassVisitor(jdk.internal.org.objectweb.asm.ClassVisitor) IOException(java.io.IOException) ClassWriter(jdk.internal.org.objectweb.asm.ClassWriter)

Example 4 with ClassVisitor

use of jdk.internal.org.objectweb.asm.ClassVisitor in project openj9 by eclipse.

the class Cmvc196982 method addAnnotation.

/*
	 * Transform classToTransform by adding the given annotation to all already-annotated methods, fields and constructors in the class.
	 * Return the modified class file bytes.
	 */
private byte[] addAnnotation(Class<?> classToTransform, final Class<?> annotation) throws IOException {
    ClassWriter writer = new ClassWriter(0);
    new ClassReader(Type.getInternalName(classToTransform)).accept(new ClassVisitor(ASM4, writer) {

        @Override
        public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
            return new MethodVisitor(ASM4, cv.visitMethod(access, name, desc, signature, exceptions)) {

                @Override
                public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
                    mv.visitAnnotation(Type.getDescriptor(annotation), true).visitEnd();
                    return mv.visitAnnotation(desc, visible);
                }
            };
        }

        @Override
        public FieldVisitor visitField(int access, String name, String desc, String signature, Object value) {
            return new FieldVisitor(ASM4, cv.visitField(access, name, desc, signature, value)) {

                @Override
                public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
                    fv.visitAnnotation(Type.getDescriptor(annotation), true).visitEnd();
                    return fv.visitAnnotation(desc, visible);
                }
            };
        }
    }, 0);
    return writer.toByteArray();
}
Also used : AnnotationVisitor(jdk.internal.org.objectweb.asm.AnnotationVisitor) ClassReader(jdk.internal.org.objectweb.asm.ClassReader) ClassVisitor(jdk.internal.org.objectweb.asm.ClassVisitor) FieldVisitor(jdk.internal.org.objectweb.asm.FieldVisitor) ClassWriter(jdk.internal.org.objectweb.asm.ClassWriter) MethodVisitor(jdk.internal.org.objectweb.asm.MethodVisitor)

Example 5 with ClassVisitor

use of jdk.internal.org.objectweb.asm.ClassVisitor in project Bytecoder by mirkosertic.

the class Module method loadModuleInfoClass.

/**
 * Loads module-info.class as a package-private interface in a class loader
 * that is a child of this module's class loader.
 */
private Class<?> loadModuleInfoClass(InputStream in) throws IOException {
    final String MODULE_INFO = "module-info";
    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS + ClassWriter.COMPUTE_FRAMES);
    ClassVisitor cv = new ClassVisitor(Opcodes.ASM5, cw) {

        @Override
        public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
            cw.visit(version, Opcodes.ACC_INTERFACE + Opcodes.ACC_ABSTRACT + Opcodes.ACC_SYNTHETIC, MODULE_INFO, null, "java/lang/Object", null);
        }

        @Override
        public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
            // keep annotations
            return super.visitAnnotation(desc, visible);
        }

        @Override
        public void visitAttribute(Attribute attr) {
        // drop non-annotation attributes
        }
    };
    ClassReader cr = new ClassReader(in);
    cr.accept(cv, 0);
    byte[] bytes = cw.toByteArray();
    ClassLoader cl = new ClassLoader(loader) {

        @Override
        protected Class<?> findClass(String cn) throws ClassNotFoundException {
            if (cn.equals(MODULE_INFO)) {
                return super.defineClass(cn, bytes, 0, bytes.length);
            } else {
                throw new ClassNotFoundException(cn);
            }
        }
    };
    try {
        return cl.loadClass(MODULE_INFO);
    } catch (ClassNotFoundException e) {
        throw new InternalError(e);
    }
}
Also used : Attribute(jdk.internal.org.objectweb.asm.Attribute) ClassReader(jdk.internal.org.objectweb.asm.ClassReader) BuiltinClassLoader(jdk.internal.loader.BuiltinClassLoader) ClassVisitor(jdk.internal.org.objectweb.asm.ClassVisitor) ClassWriter(jdk.internal.org.objectweb.asm.ClassWriter)

Aggregations

ClassReader (jdk.internal.org.objectweb.asm.ClassReader)5 ClassVisitor (jdk.internal.org.objectweb.asm.ClassVisitor)5 ClassWriter (jdk.internal.org.objectweb.asm.ClassWriter)5 IOException (java.io.IOException)1 BuiltinClassLoader (jdk.internal.loader.BuiltinClassLoader)1 AnnotationVisitor (jdk.internal.org.objectweb.asm.AnnotationVisitor)1 Attribute (jdk.internal.org.objectweb.asm.Attribute)1 FieldVisitor (jdk.internal.org.objectweb.asm.FieldVisitor)1 MethodVisitor (jdk.internal.org.objectweb.asm.MethodVisitor)1 FieldNode (jdk.internal.org.objectweb.asm.tree.FieldNode)1 CheckClassAdapter (jdk.internal.org.objectweb.asm.util.CheckClassAdapter)1