Search in sources :

Example 1 with Attribute

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

the class ModuleInfoExtender method toByteArray.

/**
 * Returns the bytes of the modified module-info.class.
 * Once this method has been called then the Extender object should
 * be discarded.
 */
public byte[] toByteArray() throws IOException {
    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS + ClassWriter.COMPUTE_FRAMES);
    AttributeAddingClassVisitor cv = new AttributeAddingClassVisitor(Opcodes.ASM5, cw);
    ClassReader cr = new ClassReader(in);
    if (packages != null)
        cv.addAttribute(new ModulePackagesAttribute(packages));
    if (mainClass != null)
        cv.addAttribute(new ModuleMainClassAttribute(mainClass));
    if (targetPlatform != null)
        cv.addAttribute(new ModuleTargetAttribute(targetPlatform));
    if (hashes != null)
        cv.addAttribute(new ModuleHashesAttribute(hashes));
    if (moduleResolution != null)
        cv.addAttribute(new ModuleResolutionAttribute(moduleResolution.value()));
    List<Attribute> attrs = new ArrayList<>();
    // prototypes of attributes that should be parsed
    attrs.add(new ModuleAttribute(version));
    attrs.add(new ModulePackagesAttribute());
    attrs.add(new ModuleMainClassAttribute());
    attrs.add(new ModuleTargetAttribute());
    attrs.add(new ModuleHashesAttribute());
    cr.accept(cv, attrs.toArray(new Attribute[0]), 0);
    // add any attributes that didn't replace previous attributes
    cv.finish();
    return cw.toByteArray();
}
Also used : Attribute(jdk.internal.org.objectweb.asm.Attribute) ArrayList(java.util.ArrayList) ClassReader(jdk.internal.org.objectweb.asm.ClassReader) ClassWriter(jdk.internal.org.objectweb.asm.ClassWriter)

Example 2 with Attribute

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

Attribute (jdk.internal.org.objectweb.asm.Attribute)2 ClassReader (jdk.internal.org.objectweb.asm.ClassReader)2 ClassWriter (jdk.internal.org.objectweb.asm.ClassWriter)2 ArrayList (java.util.ArrayList)1 BuiltinClassLoader (jdk.internal.loader.BuiltinClassLoader)1 ClassVisitor (jdk.internal.org.objectweb.asm.ClassVisitor)1