Search in sources :

Example 1 with ClassReader

use of jdk.internal.org.objectweb.asm.ClassReader in project jdk8u_jdk by JetBrains.

the class CheckClassAdapter method main.

/**
     * Checks a given class.
     * <p>
     * Usage: CheckClassAdapter &lt;binary class name or class file name&gt;
     *
     * @param args
     *            the command line arguments.
     *
     * @throws Exception
     *             if the class cannot be found, or if an IO exception occurs.
     */
public static void main(final String[] args) throws Exception {
    if (args.length != 1) {
        System.err.println("Verifies the given class.");
        System.err.println("Usage: CheckClassAdapter " + "<fully qualified class name or class file name>");
        return;
    }
    ClassReader cr;
    if (args[0].endsWith(".class")) {
        cr = new ClassReader(new FileInputStream(args[0]));
    } else {
        cr = new ClassReader(args[0]);
    }
    verify(cr, false, new PrintWriter(System.err));
}
Also used : ClassReader(jdk.internal.org.objectweb.asm.ClassReader) FileInputStream(java.io.FileInputStream) PrintWriter(java.io.PrintWriter)

Example 2 with ClassReader

use of jdk.internal.org.objectweb.asm.ClassReader 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 3 with ClassReader

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

the class MultiClassAdapter method changeVersion.

public static void changeVersion() throws IOException {
    String path = "code.record.WaitClearCode";
    VisitClassLoader classLoader = new VisitClassLoader(Thread.currentThread().getContextClassLoader());
    ClassReader cr = new ClassReader(path);
    ClassWriter cw = new ClassWriter(cr, 0);
    // 如 果ClassReader 检测到这个 ClassVisitor 返回的 MethodVisitor 来自一个ClassWriter,
    ChangeVersionAdapter ca = new ChangeVersionAdapter(cw);
    // 这意味着这个方法的内容将不会被转换,事实上,应用程序甚至不会看到其内容。
    // 在 ClassReader 组 件 的 accept 方 法 参 数 中 传 送 了 ClassVisitor
    cr.accept(ca, 0);
    // 在这种情况下,ClassReader 组件不会分析这个方法的内容,不会生成相应事件,只
    // 是复制 ClassWriter 中表示这个方法的字节数组
    /*
        执行这一优化后,由于 ChangeVersionAdapter 没有转换任何方法,所以以上代码的
        速度可以达到之前代码的两倍。对于转换部分或全部方法的常见转换,这一速度提升幅度可能要
        小一些,但仍然是很可观的:实际上在 10%到 20%的量级。遗憾的是,这一优化需要将原类中
        定义的所有常量都复制到转换后的类中。对于那些增加字段、方法或指令的转换来说,这一点不
        成问题,但对于那些要移除或重命名许多类成员的转换来说,这一优化将导致类文件大于未优化
        时的情况。因此,建议仅对“增加性”转换应用这一优化。
*/
    byte[] b = cw.toByteArray();
    VisitClassLoader visitClassLoader = new VisitClassLoader();
    Class<?> c = visitClassLoader.defineClass("code.record.WaitClearCode", b);
    System.out.println(c.getTypeName());
    ResourceUtil.write("ClassCode.class", b);
}
Also used : ClassReader(jdk.internal.org.objectweb.asm.ClassReader) ClassWriter(jdk.internal.org.objectweb.asm.ClassWriter)

Example 4 with ClassReader

use of jdk.internal.org.objectweb.asm.ClassReader 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 5 with ClassReader

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

the class DDRInteractiveClassLoader method sniffClassFile.

private DTFJPluginSnifferVisitor sniffClassFile(InputStream in) throws IOException {
    DTFJPluginSnifferVisitor sniffer = new DTFJPluginSnifferVisitor();
    ClassReader cr = new ClassReader(in);
    cr.accept(sniffer, 0);
    return sniffer;
}
Also used : ClassReader(jdk.internal.org.objectweb.asm.ClassReader)

Aggregations

ClassReader (jdk.internal.org.objectweb.asm.ClassReader)20 ClassWriter (jdk.internal.org.objectweb.asm.ClassWriter)9 PrintWriter (java.io.PrintWriter)7 FileInputStream (java.io.FileInputStream)6 ClassVisitor (jdk.internal.org.objectweb.asm.ClassVisitor)5 ClassNode (jdk.internal.org.objectweb.asm.tree.ClassNode)3 Attribute (jdk.internal.org.objectweb.asm.Attribute)2 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 StringWriter (java.io.StringWriter)1 URL (java.net.URL)1 ReadableByteChannel (java.nio.channels.ReadableByteChannel)1 ArrayList (java.util.ArrayList)1 JarEntry (java.util.jar.JarEntry)1 JarFile (java.util.jar.JarFile)1 BuiltinClassLoader (jdk.internal.loader.BuiltinClassLoader)1 AnnotationVisitor (jdk.internal.org.objectweb.asm.AnnotationVisitor)1 FieldVisitor (jdk.internal.org.objectweb.asm.FieldVisitor)1 MethodVisitor (jdk.internal.org.objectweb.asm.MethodVisitor)1