Search in sources :

Example 16 with ClassReader

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

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

the class Textifier method main.

/**
 * Prints a disassembled view of the given class to the standard output.
 * <p>
 * Usage: Textifier [-debug] &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 {
    int i = 0;
    int flags = ClassReader.SKIP_DEBUG;
    boolean ok = true;
    if (args.length < 1 || args.length > 2) {
        ok = false;
    }
    if (ok && "-debug".equals(args[0])) {
        i = 1;
        flags = 0;
        if (args.length != 2) {
            ok = false;
        }
    }
    if (!ok) {
        System.err.println("Prints a disassembled view of the given class.");
        System.err.println("Usage: Textifier [-debug] " + "<fully qualified class name or class file name>");
        return;
    }
    ClassReader cr;
    if (args[i].endsWith(".class") || args[i].indexOf('\\') > -1 || args[i].indexOf('/') > -1) {
        cr = new ClassReader(new FileInputStream(args[i]));
    } else {
        cr = new ClassReader(args[i]);
    }
    cr.accept(new TraceClassVisitor(new PrintWriter(System.out)), flags);
}
Also used : ClassReader(jdk.internal.org.objectweb.asm.ClassReader) FileInputStream(java.io.FileInputStream) PrintWriter(java.io.PrintWriter)

Example 18 with ClassReader

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

Example 19 with ClassReader

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

the class PluginManagerImpl method scanClassFile.

/*[ENDIF] Sidecar19-SE */
private ClassInfo scanClassFile(InputStream file, URL url) throws IOException {
    ClassScanner scanner = new ClassScanner(url, listeners);
    ClassReader cr = new ClassReader(file);
    cr.accept(scanner, 0);
    return scanner.getClassInfo();
}
Also used : ClassReader(jdk.internal.org.objectweb.asm.ClassReader)

Example 20 with ClassReader

use of jdk.internal.org.objectweb.asm.ClassReader in project OSJR by zeruth.

the class JarLoader method loadJar.

private void loadJar() {
    try {
        if (Settings.DOWNLOAD_GAMEPACK) {
            long start = System.currentTimeMillis();
            ReadableByteChannel rbc = Channels.newChannel(new URL(gamepackUrl).openStream());
            FileOutputStream fos = new FileOutputStream("gamepack.jar");
            fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
            System.out.println("Gamepack download took " + (System.currentTimeMillis() - start) + "ms");
        } else {
        }
        JarFile jar = new JarFile(new File("./export/gamepack.jar"));
        Enumeration<JarEntry> en = jar.entries();
        while (en.hasMoreElements()) {
            JarEntry entry = en.nextElement();
            if (entry.getName().endsWith(".class")) {
                ClassReader cr = new ClassReader(jar.getInputStream(entry));
                ClassNode cn = new ClassNode();
                cr.accept(cn, ClassReader.SKIP_DEBUG | ClassReader.SKIP_FRAMES);
                classnodes.put(cn.name, cn);
            }
        }
        jar.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : ReadableByteChannel(java.nio.channels.ReadableByteChannel) ClassNode(jdk.internal.org.objectweb.asm.tree.ClassNode) FileOutputStream(java.io.FileOutputStream) ClassReader(jdk.internal.org.objectweb.asm.ClassReader) JarFile(java.util.jar.JarFile) JarEntry(java.util.jar.JarEntry) JarFile(java.util.jar.JarFile) File(java.io.File) URL(java.net.URL)

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