Search in sources :

Example 21 with Type

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

use of org.apache.xbean.asm9.Type 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

MethodVisitor (org.apache.xbean.asm9.MethodVisitor)11 Type (org.apache.xbean.asm9.Type)7 Method (java.lang.reflect.Method)4 Label (org.apache.xbean.asm9.Label)4 ArrayList (java.util.ArrayList)3 Test (org.junit.Test)3 MatchEntry (org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntry)3 MatchEntryBuilder (org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntryBuilder)3 Icmpv4TypeCaseBuilder (org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.Icmpv4TypeCaseBuilder)3 Icmpv6TypeCaseBuilder (org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.Icmpv6TypeCaseBuilder)3 TcpSrcCaseBuilder (org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.TcpSrcCaseBuilder)3 Icmpv4TypeBuilder (org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.icmpv4.type._case.Icmpv4TypeBuilder)3 Icmpv6TypeBuilder (org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.icmpv6.type._case.Icmpv6TypeBuilder)3 TcpSrcBuilder (org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.tcp.src._case.TcpSrcBuilder)3 Type (jodd.asm5.Type)2 AnnotationVisitor (org.apache.xbean.asm9.AnnotationVisitor)2 Dscp (org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Dscp)2 Ipv4Address (org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address)2 MacAddress (org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress)2 EtherType (org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.EtherType)2