Search in sources :

Example 1 with AnnotationVisitor

use of org.apache.xbean.asm9.AnnotationVisitor 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 2 with AnnotationVisitor

use of org.apache.xbean.asm9.AnnotationVisitor 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 3 with AnnotationVisitor

use of org.apache.xbean.asm9.AnnotationVisitor in project tomee by apache.

the class ReturnValidationGenerator method generateMethods.

protected void generateMethods(final ClassWriter cw) {
    for (final MethodConstraints methodConstraints : constraints) {
        final Method method = methodConstraints.getMethod();
        final String name = method.getName();
        // Declare a method of return type JsonWebToken for use with
        // a call to BeanValidation's ExecutableValidator.validateReturnValue
        final Type returnType = Type.getReturnType(method);
        final Type[] parameterTypes = Type.getArgumentTypes(method);
        final String descriptor = Type.getMethodDescriptor(returnType, parameterTypes);
        final MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, name, descriptor, null, null);
        // Put the method name on the
        final AnnotationVisitor av = mv.visitAnnotation(Type.getDescriptor(Generated.class), true);
        av.visit("value", this.getClass().getName());
        av.visitEnd();
        // track the MethodVisitor
        // We will later copy over the annotations
        generatedMethods.put(method.getName() + Type.getMethodDescriptor(method), new ConstrainedMethodVisitor(mv, methodConstraints));
        if (method.getReturnType().equals(Void.TYPE)) {
            mv.visitCode();
            mv.visitInsn(RETURN);
            mv.visitMaxs(0, 1);
        } else if (method.getReturnType().equals(Long.TYPE)) {
            mv.visitCode();
            mv.visitInsn(LCONST_0);
            mv.visitInsn(LRETURN);
            mv.visitMaxs(2, 4);
            mv.visitEnd();
        } else if (method.getReturnType().equals(Float.TYPE)) {
            mv.visitCode();
            mv.visitInsn(FCONST_0);
            mv.visitInsn(FRETURN);
            mv.visitMaxs(1, 3);
            mv.visitEnd();
        } else if (method.getReturnType().equals(Double.TYPE)) {
            mv.visitCode();
            mv.visitInsn(DCONST_0);
            mv.visitInsn(DRETURN);
            mv.visitMaxs(2, 4);
            mv.visitEnd();
        } else if (method.getReturnType().isPrimitive()) {
            mv.visitCode();
            mv.visitInsn(ICONST_0);
            mv.visitInsn(IRETURN);
            mv.visitMaxs(1, 3);
            mv.visitEnd();
        } else {
            // The method will simply return null
            mv.visitCode();
            mv.visitInsn(ACONST_NULL);
            mv.visitInsn(ARETURN);
            mv.visitMaxs(1, 1);
        }
    }
}
Also used : Type(org.apache.xbean.asm9.Type) AnnotationVisitor(org.apache.xbean.asm9.AnnotationVisitor) Method(java.lang.reflect.Method) MethodVisitor(org.apache.xbean.asm9.MethodVisitor)

Example 4 with AnnotationVisitor

use of org.apache.xbean.asm9.AnnotationVisitor in project tomee by apache.

the class JwtValidationGenerator method generateMethods.

protected void generateMethods(final ClassWriter cw) {
    for (final MethodConstraints methodConstraints : constraints) {
        final Method method = methodConstraints.getMethod();
        final String name = method.getName();
        // Declare a method of return type JsonWebToken for use with
        // a call to BeanValidation's ExecutableValidator.validateReturnValue
        final Type returnType = Type.getType(JsonWebToken.class);
        final Type[] parameterTypes = Type.getArgumentTypes(method);
        final String descriptor = Type.getMethodDescriptor(returnType, parameterTypes);
        final int access = method.isVarArgs() ? ACC_PUBLIC + ACC_VARARGS : ACC_PUBLIC;
        final MethodVisitor mv = cw.visitMethod(access, name, descriptor, null, null);
        // Put the method name on the
        final AnnotationVisitor av = mv.visitAnnotation(Type.getDescriptor(Generated.class), true);
        av.visit("value", this.getClass().getName());
        av.visitEnd();
        // track the MethodVisitor
        // We will later copy over the annotations
        generatedMethods.put(method.getName() + Type.getMethodDescriptor(method), new ConstrainedMethodVisitor(mv, methodConstraints));
        // The method will simply return null
        mv.visitCode();
        mv.visitInsn(ACONST_NULL);
        mv.visitInsn(ARETURN);
        mv.visitMaxs(1, 1);
    }
}
Also used : Type(org.apache.xbean.asm9.Type) AnnotationVisitor(org.apache.xbean.asm9.AnnotationVisitor) Method(java.lang.reflect.Method) MethodVisitor(org.apache.xbean.asm9.MethodVisitor)

Aggregations

Method (java.lang.reflect.Method)2 ZipEntry (java.util.zip.ZipEntry)2 AnnotationVisitor (org.apache.xbean.asm6.AnnotationVisitor)2 ClassWriter (org.apache.xbean.asm6.ClassWriter)2 MethodVisitor (org.apache.xbean.asm6.MethodVisitor)2 AnnotationVisitor (org.apache.xbean.asm9.AnnotationVisitor)2 MethodVisitor (org.apache.xbean.asm9.MethodVisitor)2 Type (org.apache.xbean.asm9.Type)2 Processor (org.talend.sdk.component.api.processor.Processor)1 Action (org.talend.sdk.component.api.service.Action)1