Package org.springframework.roo.classpath.details

Examples of org.springframework.roo.classpath.details.MethodMetadataBuilder


                parameterTypes);
        if (userMethod != null) {
            Validate.isTrue(userMethod.getReturnType().equals(returnType),
                    "Method '%s' on '%s' must return '%s'", methodName,
                    destination, returnType.getNameIncludingTypeParameters());
            return new MethodMetadataBuilder(userMethod);
        }

        // Create the method body
        builder.getImportRegistrationResolver().addImports(ARRAY_LIST,
                ITERATOR, CONSTRAINT_VIOLATION_EXCEPTION, CONSTRAINT_VIOLATION);

        findEntriesMethod.copyAdditionsTo(builder, governorTypeDetails);
        persistMethod.copyAdditionsTo(builder, governorTypeDetails);

        final InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
        final String dataField = getDataFieldName().getSymbolName();
        bodyBuilder.appendFormalLine("int from = 0;");
        bodyBuilder.appendFormalLine("int to = " + quantity + ";");
        bodyBuilder.appendFormalLine(dataField + " = "
                + findEntriesMethod.getMethodCall() + ";");
        bodyBuilder.appendFormalLine("if (" + dataField + " == null) {");
        bodyBuilder.indent();
        bodyBuilder
                .appendFormalLine("throw new IllegalStateException(\"Find entries implementation for '"
                        + entity.getSimpleTypeName()
                        + "' illegally returned null\");");
        bodyBuilder.indentRemove();
        bodyBuilder.appendFormalLine("}");
        bodyBuilder.appendFormalLine("if (!" + dataField + ".isEmpty()) {");
        bodyBuilder.indent();
        bodyBuilder.appendFormalLine("return;");
        bodyBuilder.indentRemove();
        bodyBuilder.appendFormalLine("}");
        bodyBuilder.appendFormalLine("");
        bodyBuilder.appendFormalLine(dataField + " = new ArrayList<"
                + entity.getSimpleTypeName() + ">();");
        bodyBuilder.appendFormalLine("for (int i = 0; i < " + quantity
                + "; i++) {");
        bodyBuilder.indent();
        bodyBuilder.appendFormalLine(entity.getSimpleTypeName() + " " + OBJ_VAR
                + " = "
                + newTransientEntityMethod.getMethodName().getSymbolName()
                + "(i);");
        bodyBuilder.appendFormalLine("try {");
        bodyBuilder.indent();
        bodyBuilder.appendFormalLine(persistMethod.getMethodCall() + ";");
        bodyBuilder.indentRemove();
        bodyBuilder
                .appendFormalLine("} catch (final ConstraintViolationException e) {");
        bodyBuilder.indent();
        bodyBuilder
                .appendFormalLine("final StringBuilder msg = new StringBuilder();");
        bodyBuilder
                .appendFormalLine("for (Iterator<ConstraintViolation<?>> iter = e.getConstraintViolations().iterator(); iter.hasNext();) {");
        bodyBuilder.indent();
        bodyBuilder
                .appendFormalLine("final ConstraintViolation<?> cv = iter.next();");
        bodyBuilder
                .appendFormalLine("msg.append(\"[\").append(cv.getRootBean().getClass().getName()).append(\".\").append(cv.getPropertyPath()).append(\": \").append(cv.getMessage()).append(\" (invalid value = \").append(cv.getInvalidValue()).append(\")\").append(\"]\");");
        bodyBuilder.indentRemove();
        bodyBuilder.appendFormalLine("}");
        bodyBuilder
                .appendFormalLine("throw new IllegalStateException(msg.toString(), e);");
        bodyBuilder.indentRemove();
        bodyBuilder.appendFormalLine("}");
        if (flushMethod != null) {
            bodyBuilder.appendFormalLine(flushMethod.getMethodCall() + ";");
            flushMethod.copyAdditionsTo(builder, governorTypeDetails);
        }
        bodyBuilder.appendFormalLine(dataField + ".add(" + OBJ_VAR + ");");
        bodyBuilder.indentRemove();
        bodyBuilder.appendFormalLine("}");

        // Create the method
        return new MethodMetadataBuilder(getId(), Modifier.PUBLIC, methodName,
                returnType,
                AnnotatedJavaType.convertFromJavaTypes(parameterTypes),
                parameterNames, bodyBuilder);
    }
View Full Code Here


        // Create method
        final InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
        bodyBuilder.appendFormalLine("return false;");

        final MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(
                getId(), Modifier.PUBLIC, methodName, returnType,
                AnnotatedJavaType.convertFromJavaTypes(parameterType),
                parameterNames, bodyBuilder);
        builder.addMethod(methodBuilder);
        modifyMethod = methodBuilder.build();
    }
View Full Code Here

                    + OBJ_VAR + ", " + INDEX_VAR + ");");
        }

        bodyBuilder.appendFormalLine("return " + OBJ_VAR + ";");

        final MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(
                getId(), Modifier.PUBLIC, methodName, entity,
                AnnotatedJavaType.convertFromJavaTypes(parameterType),
                parameterNames, bodyBuilder);
        builder.addMethod(methodBuilder);
        newTransientEntityMethod = methodBuilder.build();
    }
View Full Code Here

        bodyBuilder.appendFormalLine("return " + findMethod.getMethodCall()
                + ";");

        findMethod.copyAdditionsTo(builder, governorTypeDetails);

        final MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(
                getId(), Modifier.PUBLIC, methodName, entity, bodyBuilder);
        builder.addMethod(methodBuilder);
        randomPersistentEntityMethod = methodBuilder.build();
    }
View Full Code Here

        bodyBuilder.appendFormalLine("return " + findMethod.getMethodCall()
                + ";");

        findMethod.copyAdditionsTo(builder, governorTypeDetails);

        final MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(
                getId(), Modifier.PUBLIC, methodName, entity,
                AnnotatedJavaType.convertFromJavaTypes(parameterType),
                parameterNames, bodyBuilder);
        builder.addMethod(methodBuilder);
        specificPersistentEntityMethod = methodBuilder.build();
    }
View Full Code Here

    /**
     * @return the clear method (never returns null)
     */
    private MethodMetadataBuilder getClearMethod() {
        if (parent != null) {
            final MethodMetadataBuilder found = parent.getClearMethod();
            if (found != null) {
                return found;
            }
        }
        if ("".equals(crudAnnotationValues.getClearMethod())) {
View Full Code Here

            bodyBuilder.appendFormalLine("return " + ENTITY_MANAGER_METHOD_NAME
                    + "().createQuery(\"SELECT COUNT(o) FROM " + entityName
                    + " o\", Long.class).getSingleResult();");
        }

        final MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(
                getId(), Modifier.PUBLIC | Modifier.STATIC, methodName,
                COUNT_RETURN_TYPE,
                AnnotatedJavaType.convertFromJavaTypes(parameterTypes),
                parameterNames, bodyBuilder);
        methodBuilder.setAnnotations(annotations);
        return methodBuilder.build();
    }
View Full Code Here

        // Locate user-defined method
        final MethodMetadata userMethod = getGovernorMethod(methodName,
                parameterTypes);
        if (userMethod != null) {
            return new MethodMetadataBuilder(userMethod);
        }

        // Create the method
        final List<AnnotationMetadataBuilder> annotations = new ArrayList<AnnotationMetadataBuilder>();

        final InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();

        // Address non-injected entity manager field
        final MethodMetadata entityManagerMethod = getEntityManagerMethod();
        Validate.notNull(entityManagerMethod,
                "Entity manager method should not have returned null");

        // Use the getEntityManager() method to acquire an entity manager (the
        // method will throw an exception if it cannot be acquired)
        final String entityManagerFieldName = getEntityManagerField()
                .getFieldName().getSymbolName();
        bodyBuilder.appendFormalLine("if (this." + entityManagerFieldName
                + " == null) this." + entityManagerFieldName + " = "
                + entityManagerMethod.getMethodName().getSymbolName() + "();");

        JavaType returnType = JavaType.VOID_PRIMITIVE;
        if ("flush".equals(methodDelegateName)) {
            addTransactionalAnnotation(annotations);
            bodyBuilder.appendFormalLine("this." + entityManagerFieldName
                    + ".flush();");
        }
        else if ("clear".equals(methodDelegateName)) {
            addTransactionalAnnotation(annotations);
            bodyBuilder.appendFormalLine("this." + entityManagerFieldName
                    + ".clear();");
        }
        else if ("merge".equals(methodDelegateName)) {
            addTransactionalAnnotation(annotations);
            returnType = new JavaType(destination.getSimpleTypeName());
            bodyBuilder.appendFormalLine(destination.getSimpleTypeName()
                    + " merged = this." + entityManagerFieldName
                    + ".merge(this);");
            bodyBuilder.appendFormalLine("this." + entityManagerFieldName
                    + ".flush();");
            bodyBuilder.appendFormalLine("return merged;");
        }
        else if ("remove".equals(methodDelegateName)) {
            addTransactionalAnnotation(annotations);
            bodyBuilder.appendFormalLine("if (this." + entityManagerFieldName
                    + ".contains(this)) {");
            bodyBuilder.indent();
            bodyBuilder.appendFormalLine("this." + entityManagerFieldName
                    + ".remove(this);");
            bodyBuilder.indentRemove();
            bodyBuilder.appendFormalLine("} else {");
            bodyBuilder.indent();
            bodyBuilder.appendFormalLine(destination.getSimpleTypeName()
                    + " attached = " + destination.getSimpleTypeName() + "."
                    + getFindMethod().getMethodName().getSymbolName()
                    + "(this." + identifierField.getFieldName().getSymbolName()
                    + ");");
            bodyBuilder.appendFormalLine("this." + entityManagerFieldName
                    + ".remove(attached);");
            bodyBuilder.indentRemove();
            bodyBuilder.appendFormalLine("}");
        }
        else {
            // Persist
            addTransactionalAnnotation(annotations, true);
            bodyBuilder.appendFormalLine("this." + entityManagerFieldName + "."
                    + methodDelegateName + "(this);");
        }

        final MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(
                getId(), Modifier.PUBLIC, methodName, returnType,
                AnnotatedJavaType.convertFromJavaTypes(parameterTypes),
                new ArrayList<JavaSymbolName>(), bodyBuilder);
        methodBuilder.setAnnotations(annotations);
        return methodBuilder;
    }
View Full Code Here

        final InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
        bodyBuilder.appendFormalLine("return " + ENTITY_MANAGER_METHOD_NAME
                + "().createQuery(\"SELECT o FROM " + entityName + " o\", "
                + destination.getSimpleTypeName() + ".class).getResultList();");

        final MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(
                getId(), Modifier.PUBLIC | Modifier.STATIC, methodName,
                returnType,
                AnnotatedJavaType.convertFromJavaTypes(parameterTypes),
                parameterNames, bodyBuilder);
        methodBuilder.setAnnotations(annotations);
        return methodBuilder.build();
    }
View Full Code Here

        bodyBuilder.indentRemove();
        bodyBuilder.appendFormalLine("}");
        bodyBuilder.appendFormalLine("return " + ENTITY_MANAGER_METHOD_NAME
            + "().createQuery(jpaQuery, " + destination.getSimpleTypeName() + ".class" + ").getResultList();");

        final MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(
                getId(), Modifier.PUBLIC | Modifier.STATIC, methodName,
                returnType,
                AnnotatedJavaType.convertFromJavaTypes(parameterTypes),
                parameterNames, bodyBuilder);
        methodBuilder.setAnnotations(annotations);
        return methodBuilder.build();
    }
View Full Code Here

TOP

Related Classes of org.springframework.roo.classpath.details.MethodMetadataBuilder

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.