Search in sources :

Example 1 with ACC_SUPER

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

the class PluginGenerator method createModel.

private byte[] createModel(final JarOutputStream outputStream, String packageName) throws IOException {
    final String className = packageName + "/AModel.class";
    outputStream.putNextEntry(new ZipEntry(className));
    final ClassWriter writer = new ClassWriter(COMPUTE_FRAMES);
    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);
    // no real content (fields/methods) for now
    writer.visitEnd();
    return writer.toByteArray();
}
Also used : ZipEntry(java.util.zip.ZipEntry) ClassWriter(org.apache.xbean.asm6.ClassWriter)

Example 2 with ACC_SUPER

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

the class ProxyGenerator method generateProxy.

public Class<?> generateProxy(final ClassLoader loader, final Class<?> classToProxy, final String plugin, final String key) {
    final ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
    final String proxyClassName = fixPreservedPackages((classToProxy.getSigners() != null ? getSignedClassProxyName(classToProxy) : classToProxy.getName()) + "$$TalendServiceProxy");
    final String classFileName = proxyClassName.replace('.', '/');
    final String[] interfaceNames = { Type.getInternalName(Serializable.class) };
    final String superClassName = Type.getInternalName(classToProxy);
    cw.visit(findJavaVersion(classToProxy), ACC_PUBLIC + ACC_SUPER + ACC_SYNTHETIC, classFileName, null, superClassName, interfaceNames);
    cw.visitSource(classFileName + ".java", null);
    if (!Serializable.class.isAssignableFrom(classToProxy)) {
        try {
            classToProxy.getMethod("writeReplace");
        } catch (final NoSuchMethodException e) {
            createSerialisation(cw, plugin, key);
        }
    }
    final boolean hasInterceptors = hasInterceptors(classToProxy);
    if (hasInterceptors) {
        cw.visitField(ACC_PRIVATE, FIELD_INTERCEPTOR_HANDLER, Type.getDescriptor(InterceptorHandler.class), null, null).visitEnd();
        cw.visitField(ACC_PRIVATE | ACC_STATIC, FIELD_INTERCEPTED_METHODS, Type.getDescriptor(Method[].class), null, null).visitEnd();
    }
    createConstructor(cw, classToProxy, superClassName, classFileName, Stream.of(classToProxy.getDeclaredConstructors()).filter(c -> {
        final int modifiers = c.getModifiers();
        return Modifier.isPublic(modifiers) || Modifier.isProtected(modifiers);
    }).sorted((o1, o2) -> {
        // prefer public constructor and then the smallest parameter count
        final int mod1 = o1.getModifiers();
        final int mod2 = o2.getModifiers();
        if (Modifier.isProtected(mod1) && !Modifier.isPublic(mod2)) {
            return 1;
        }
        if (Modifier.isProtected(mod2) && !Modifier.isPublic(mod1)) {
            return -1;
        }
        return o1.getParameterCount() - o2.getParameterCount();
    }).findFirst().orElseThrow(() -> new IllegalArgumentException(classToProxy + " has no default constructor, put at least a protected one")), hasInterceptors);
    final Method[] interceptedMethods;
    if (hasInterceptors) {
        final Collection<Annotation> globalInterceptors = Stream.of(classToProxy.getAnnotations()).filter(this::isInterceptor).collect(toList());
        final AtomicInteger methodIndex = new AtomicInteger();
        interceptedMethods = Stream.of(classToProxy.getMethods()).filter(m -> !"<init>".equals(m.getName()) && (!globalInterceptors.isEmpty() || Stream.of(m.getAnnotations()).anyMatch(this::isInterceptor))).peek(method -> delegateMethod(cw, method, classFileName, methodIndex.getAndIncrement())).toArray(Method[]::new);
    } else {
        interceptedMethods = null;
    }
    final Class<Object> objectClass = Unsafes.defineAndLoadClass(loader, proxyClassName, cw.toByteArray());
    if (hasInterceptors) {
        try {
            final Field interceptedMethodsField = objectClass.getDeclaredField(FIELD_INTERCEPTED_METHODS);
            interceptedMethodsField.setAccessible(true);
            interceptedMethodsField.set(null, interceptedMethods);
        } catch (final Exception e) {
            throw new IllegalStateException(e);
        }
    }
    return objectClass;
}
Also used : ILOAD(org.apache.xbean.asm6.Opcodes.ILOAD) IRETURN(org.apache.xbean.asm6.Opcodes.IRETURN) Intercepts(org.talend.sdk.component.api.service.interceptor.Intercepts) ClassReader(org.apache.xbean.asm6.ClassReader) LRETURN(org.apache.xbean.asm6.Opcodes.LRETURN) V1_8(org.apache.xbean.asm6.Opcodes.V1_8) Type(org.apache.xbean.asm6.Type) ACC_PUBLIC(org.apache.xbean.asm6.Opcodes.ACC_PUBLIC) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ACC_PRIVATE(org.apache.xbean.asm6.Opcodes.ACC_PRIVATE) ALOAD(org.apache.xbean.asm6.Opcodes.ALOAD) ACC_SUPER(org.apache.xbean.asm6.Opcodes.ACC_SUPER) Method(java.lang.reflect.Method) IFEQ(org.apache.xbean.asm6.Opcodes.IFEQ) PUTFIELD(org.apache.xbean.asm6.Opcodes.PUTFIELD) GETFIELD(org.apache.xbean.asm6.Opcodes.GETFIELD) BIPUSH(org.apache.xbean.asm6.Opcodes.BIPUSH) ACC_SYNTHETIC(org.apache.xbean.asm6.Opcodes.ACC_SYNTHETIC) Collection(java.util.Collection) NEW(org.apache.xbean.asm6.Opcodes.NEW) LLOAD(org.apache.xbean.asm6.Opcodes.LLOAD) Serializable(java.io.Serializable) AASTORE(org.apache.xbean.asm6.Opcodes.AASTORE) ACC_VARARGS(org.apache.xbean.asm6.Opcodes.ACC_VARARGS) Stream(java.util.stream.Stream) EmptyVisitor(org.apache.xbean.asm6.shade.commons.EmptyVisitor) INVOKEVIRTUAL(org.apache.xbean.asm6.Opcodes.INVOKEVIRTUAL) Modifier(java.lang.reflect.Modifier) SIPUSH(org.apache.xbean.asm6.Opcodes.SIPUSH) Annotation(java.lang.annotation.Annotation) FRETURN(org.apache.xbean.asm6.Opcodes.FRETURN) CHECKCAST(org.apache.xbean.asm6.Opcodes.CHECKCAST) ASTORE(org.apache.xbean.asm6.Opcodes.ASTORE) InterceptorHandler(org.talend.sdk.component.api.service.interceptor.InterceptorHandler) DLOAD(org.apache.xbean.asm6.Opcodes.DLOAD) INVOKESTATIC(org.apache.xbean.asm6.Opcodes.INVOKESTATIC) Label(org.apache.xbean.asm6.Label) ATHROW(org.apache.xbean.asm6.Opcodes.ATHROW) Constructor(java.lang.reflect.Constructor) ANEWARRAY(org.apache.xbean.asm6.Opcodes.ANEWARRAY) AALOAD(org.apache.xbean.asm6.Opcodes.AALOAD) ACC_PROTECTED(org.apache.xbean.asm6.Opcodes.ACC_PROTECTED) POP(org.apache.xbean.asm6.Opcodes.POP) ICONST_0(org.apache.xbean.asm6.Opcodes.ICONST_0) ICONST_2(org.apache.xbean.asm6.Opcodes.ICONST_2) SKIP_DEBUG(org.apache.xbean.asm6.ClassReader.SKIP_DEBUG) ICONST_1(org.apache.xbean.asm6.Opcodes.ICONST_1) INVOKEINTERFACE(org.apache.xbean.asm6.Opcodes.INVOKEINTERFACE) INVOKESPECIAL(org.apache.xbean.asm6.Opcodes.INVOKESPECIAL) ICONST_4(org.apache.xbean.asm6.Opcodes.ICONST_4) ICONST_3(org.apache.xbean.asm6.Opcodes.ICONST_3) FLOAD(org.apache.xbean.asm6.Opcodes.FLOAD) ACONST_NULL(org.apache.xbean.asm6.Opcodes.ACONST_NULL) RETURN(org.apache.xbean.asm6.Opcodes.RETURN) Field(java.lang.reflect.Field) MethodVisitor(org.apache.xbean.asm6.MethodVisitor) ACC_STATIC(org.apache.xbean.asm6.Opcodes.ACC_STATIC) ObjectStreamException(java.io.ObjectStreamException) Collectors.toList(java.util.stream.Collectors.toList) DRETURN(org.apache.xbean.asm6.Opcodes.DRETURN) GETSTATIC(org.apache.xbean.asm6.Opcodes.GETSTATIC) DUP(org.apache.xbean.asm6.Opcodes.DUP) ClassWriter(org.apache.xbean.asm6.ClassWriter) ICONST_5(org.apache.xbean.asm6.Opcodes.ICONST_5) ARETURN(org.apache.xbean.asm6.Opcodes.ARETURN) AllArgsConstructor(lombok.AllArgsConstructor) InputStream(java.io.InputStream) Serializable(java.io.Serializable) Method(java.lang.reflect.Method) ClassWriter(org.apache.xbean.asm6.ClassWriter) Annotation(java.lang.annotation.Annotation) ObjectStreamException(java.io.ObjectStreamException) Field(java.lang.reflect.Field) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Example 3 with ACC_SUPER

use of org.apache.xbean.asm6.Opcodes.ACC_SUPER 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 4 with ACC_SUPER

use of org.apache.xbean.asm6.Opcodes.ACC_SUPER 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)

Aggregations

ClassWriter (org.apache.xbean.asm6.ClassWriter)4 ZipEntry (java.util.zip.ZipEntry)3 MethodVisitor (org.apache.xbean.asm6.MethodVisitor)3 AnnotationVisitor (org.apache.xbean.asm6.AnnotationVisitor)2 InputStream (java.io.InputStream)1 ObjectStreamException (java.io.ObjectStreamException)1 Serializable (java.io.Serializable)1 Annotation (java.lang.annotation.Annotation)1 Constructor (java.lang.reflect.Constructor)1 Field (java.lang.reflect.Field)1 Method (java.lang.reflect.Method)1 Modifier (java.lang.reflect.Modifier)1 Collection (java.util.Collection)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Collectors.toList (java.util.stream.Collectors.toList)1 Stream (java.util.stream.Stream)1 AllArgsConstructor (lombok.AllArgsConstructor)1 ClassReader (org.apache.xbean.asm6.ClassReader)1 SKIP_DEBUG (org.apache.xbean.asm6.ClassReader.SKIP_DEBUG)1 Label (org.apache.xbean.asm6.Label)1