Search in sources :

Example 11 with NEW

use of org.apache.xbean.asm6.Opcodes.NEW in project component-runtime by Talend.

the class ProxyGenerator method findJavaVersion.

private int findJavaVersion(final Class<?> from) {
    final String resource = from.getName().replace('.', '/') + ".class";
    try (final InputStream stream = from.getClassLoader().getResourceAsStream(resource)) {
        if (stream == null) {
            return javaVersion;
        }
        final ClassReader reader = new ClassReader(stream);
        final VersionVisitor visitor = new VersionVisitor();
        reader.accept(visitor, SKIP_DEBUG);
        if (visitor.version != 0) {
            return visitor.version;
        }
    } catch (final Exception e) {
    // no-op
    }
    // version
    return javaVersion;
}
Also used : InputStream(java.io.InputStream) ClassReader(org.apache.xbean.asm6.ClassReader) ObjectStreamException(java.io.ObjectStreamException)

Example 12 with NEW

use of org.apache.xbean.asm6.Opcodes.NEW in project component-runtime by Talend.

the class ProxyGenerator method createSerialisation.

private void createSerialisation(final ClassWriter cw, final String pluginId, final String key) {
    final MethodVisitor mv = cw.visitMethod(Modifier.PUBLIC, "writeReplace", "()Ljava/lang/Object;", null, new String[] { Type.getType(ObjectStreamException.class).getInternalName() });
    mv.visitCode();
    mv.visitTypeInsn(NEW, "org/talend/sdk/component/runtime/serialization/SerializableService");
    mv.visitInsn(DUP);
    mv.visitLdcInsn(pluginId);
    mv.visitLdcInsn(key);
    mv.visitMethodInsn(INVOKESPECIAL, "org/talend/sdk/component/runtime/serialization/SerializableService", "<init>", "(Ljava/lang/String;Ljava/lang/String;)V", false);
    mv.visitInsn(ARETURN);
    mv.visitMaxs(-1, -1);
    mv.visitEnd();
}
Also used : MethodVisitor(org.apache.xbean.asm6.MethodVisitor)

Example 13 with NEW

use of org.apache.xbean.asm6.Opcodes.NEW in project component-runtime by Talend.

the class PluginGenerator method createService.

private byte[] createService(final JarOutputStream outputStream, final String packageName, final String name) throws IOException {
    final String className = packageName + "/AService.class";
    outputStream.putNextEntry(new ZipEntry(className));
    final ClassWriter writer = new ClassWriter(COMPUTE_FRAMES);
    writer.visitAnnotation(Type.getDescriptor(Service.class), true).visitEnd();
    writer.visit(V1_8, ACC_PUBLIC + ACC_SUPER, className.substring(0, className.length() - ".class".length()), null, Type.getInternalName(Object.class), null);
    writer.visitSource(className.replace(".class", ".java"), null);
    addConstructor(writer);
    final MethodVisitor action = writer.visitMethod(ACC_PUBLIC, "doAction", "(L" + packageName + "/AModel;)L" + packageName + "/AModel;", null, new String[0]);
    final AnnotationVisitor actionAnnotation = action.visitAnnotation(Type.getDescriptor(Action.class), true);
    actionAnnotation.visit("family", "proc");
    actionAnnotation.visit("value", name + "Action");
    actionAnnotation.visitEnd();
    action.visitCode();
    action.visitTypeInsn(NEW, packageName + "/AModel");
    action.visitInsn(DUP);
    action.visitMethodInsn(INVOKESPECIAL, packageName + "/AModel", "<init>", "()V", false);
    action.visitInsn(ARETURN);
    action.visitInsn(ARETURN);
    action.visitMaxs(1, 1);
    action.visitEnd();
    writer.visitEnd();
    return writer.toByteArray();
}
Also used : Action(org.talend.sdk.component.api.service.Action) ZipEntry(java.util.zip.ZipEntry) AnnotationVisitor(org.apache.xbean.asm6.AnnotationVisitor) ClassWriter(org.apache.xbean.asm6.ClassWriter) MethodVisitor(org.apache.xbean.asm6.MethodVisitor)

Example 14 with NEW

use of org.apache.xbean.asm6.Opcodes.NEW in project component-runtime by Talend.

the class PluginGenerator method createProcessor.

private byte[] createProcessor(final JarOutputStream outputStream, final String packageName) throws IOException {
    final String className = packageName + "/AProcessor.class";
    outputStream.putNextEntry(new ZipEntry(className));
    final ClassWriter writer = new ClassWriter(COMPUTE_FRAMES);
    final AnnotationVisitor processorAnnotation = writer.visitAnnotation(Type.getDescriptor(Processor.class), true);
    processorAnnotation.visit("family", "comp");
    processorAnnotation.visit("name", "proc");
    processorAnnotation.visitEnd();
    writer.visit(V1_8, ACC_PUBLIC + ACC_SUPER, className.substring(0, className.length() - ".class".length()), null, Type.getInternalName(Object.class), new String[] { Serializable.class.getName().replace(".", "/") });
    writer.visitSource(className.replace(".class", ".java"), null);
    addConstructor(writer);
    // generate a processor
    final MethodVisitor emitMethod = writer.visitMethod(ACC_PUBLIC, "emit", "(L" + packageName + "/AModel;)L" + packageName + "/AModel;", null, new String[0]);
    emitMethod.visitAnnotation(Type.getDescriptor(ElementListener.class), true).visitEnd();
    emitMethod.visitCode();
    emitMethod.visitTypeInsn(NEW, packageName + "/AModel");
    emitMethod.visitInsn(DUP);
    emitMethod.visitMethodInsn(INVOKESPECIAL, packageName + "/AModel", "<init>", "()V", false);
    emitMethod.visitInsn(ARETURN);
    emitMethod.visitInsn(ARETURN);
    emitMethod.visitMaxs(1, 1);
    emitMethod.visitEnd();
    writer.visitEnd();
    return writer.toByteArray();
}
Also used : Processor(org.talend.sdk.component.api.processor.Processor) ZipEntry(java.util.zip.ZipEntry) AnnotationVisitor(org.apache.xbean.asm6.AnnotationVisitor) ClassWriter(org.apache.xbean.asm6.ClassWriter) MethodVisitor(org.apache.xbean.asm6.MethodVisitor)

Example 15 with NEW

use of org.apache.xbean.asm6.Opcodes.NEW in project component-runtime by Talend.

the class BeamIOTransformer method rewrite.

private byte[] rewrite(final ConfigurableClassLoader loader, final String className, final byte[] classfileBuffer, final ClassLoader tmpLoader) {
    final String plugin = loader.getId();
    final ClassReader reader = new ClassReader(classfileBuffer);
    final ComponentClassWriter writer = new ComponentClassWriter(className.replace('/', '.'), tmpLoader, reader, ClassWriter.COMPUTE_FRAMES);
    final ComponentClassVisitor visitor = new ComponentClassVisitor(writer, plugin);
    reader.accept(visitor, ClassReader.SKIP_FRAMES);
    return writer.toByteArray();
}
Also used : ClassReader(org.apache.xbean.asm6.ClassReader)

Aggregations

ClassReader (org.apache.xbean.asm6.ClassReader)10 ClassWriter (org.apache.xbean.asm6.ClassWriter)8 IOException (java.io.IOException)7 InputStream (java.io.InputStream)7 MethodVisitor (org.apache.xbean.asm6.MethodVisitor)7 Stream (java.util.stream.Stream)5 File (java.io.File)4 FileInputStream (java.io.FileInputStream)4 FileOutputStream (java.io.FileOutputStream)4 Optional.ofNullable (java.util.Optional.ofNullable)4 JarEntry (java.util.jar.JarEntry)4 JarOutputStream (java.util.jar.JarOutputStream)4 ZipEntry (java.util.zip.ZipEntry)4 EXPAND_FRAMES (org.apache.xbean.asm6.ClassReader.EXPAND_FRAMES)4 COMPUTE_FRAMES (org.apache.xbean.asm6.ClassWriter.COMPUTE_FRAMES)4 ClassRemapper (org.apache.xbean.asm6.commons.ClassRemapper)4 Remapper (org.apache.xbean.asm6.commons.Remapper)4 JarLocation.jarLocation (org.apache.ziplock.JarLocation.jarLocation)4 Assertions.fail (org.junit.jupiter.api.Assertions.fail)4 Constructor (java.lang.reflect.Constructor)3