Search in sources :

Example 81 with MethodMetadataBuilder

use of org.springframework.roo.classpath.details.MethodMetadataBuilder in project spring-roo by spring-projects.

the class RepositoryJpaCustomMetadata method getCustomFinder.

/**
 * Method that generates finder methods whose return types are projections.
 *
 * @param finderReturnType
 * @param finderName
 * @param parameterType
 *
 * @return
 */
private MethodMetadata getCustomFinder(JavaType finderReturnType, JavaSymbolName finderName, JavaType parameterType) {
    // Define method parameter types and parameter names
    List<AnnotatedJavaType> parameterTypes = new ArrayList<AnnotatedJavaType>();
    parameterTypes.add(AnnotatedJavaType.convertFromJavaType(parameterType));
    parameterTypes.add(GLOBAL_SEARCH_PARAMETER);
    parameterTypes.add(PAGEABLE_PARAMETER);
    List<JavaSymbolName> parameterNames = new ArrayList<JavaSymbolName>();
    parameterNames.add(new JavaSymbolName("formBean"));
    parameterNames.add(GOBAL_SEARCH_PARAMETER_NAME);
    parameterNames.add(PAGEABLE_PARAMETER_NAME);
    // Return type
    JavaType returnType = new JavaType(SpringJavaType.PAGE.getFullyQualifiedTypeName(), 0, DataType.TYPE, null, Arrays.asList(finderReturnType));
    // Use the MethodMetadataBuilder for easy creation of MethodMetadata
    MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(getId(), Modifier.PUBLIC + Modifier.ABSTRACT, finderName, returnType, parameterTypes, parameterNames, null);
    // Build and return a MethodMetadata
    return methodBuilder.build();
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) SpringJavaType(org.springframework.roo.model.SpringJavaType) SpringletsJavaType(org.springframework.roo.model.SpringletsJavaType) JavaType(org.springframework.roo.model.JavaType) MethodMetadataBuilder(org.springframework.roo.classpath.details.MethodMetadataBuilder) AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) ArrayList(java.util.ArrayList)

Example 82 with MethodMetadataBuilder

use of org.springframework.roo.classpath.details.MethodMetadataBuilder in project spring-roo by spring-projects.

the class JpaDataOnDemandMetadata method getInitMethod.

/**
 * Returns the DoD type's "void init()" method (existing or generated)
 *
 * @param persistMethod
 *            (required)
 * @return never `null`
 */
private MethodMetadataBuilder getInitMethod(final int quantity) {
    // Method definition to find or build
    final JavaSymbolName methodName = new JavaSymbolName("init");
    final JavaType[] parameterTypes = {};
    final List<JavaSymbolName> parameterNames = Collections.<JavaSymbolName>emptyList();
    final JavaType returnType = JavaType.VOID_PRIMITIVE;
    // Locate user-defined method
    final MethodMetadata userMethod = getGovernorMethod(methodName, 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
    final InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
    bodyBuilder.appendFormalLine("int from = 0;");
    bodyBuilder.appendFormalLine("int to = " + quantity + ";");
    // CriteriaBuilder cb = entityManager.getCriteriaBuilder();
    bodyBuilder.newLine();
    bodyBuilder.appendFormalLine("%s cb = %s().getCriteriaBuilder();", getNameOfJavaType(JpaJavaType.CRITERIA_BUILDER), getAccessorMethod(getEntityManagerField().build()).getMethodName());
    // CriteriaQuery<Entity> cq = cb.createQuery(Entity.class);
    bodyBuilder.appendFormalLine("%1$s<%2$s> cq = cb.createQuery(%2$s.class);", getNameOfJavaType(JpaJavaType.CRITERIA_QUERY), getNameOfJavaType(this.entity));
    // Root<Entity> rootEntry = cq.from(Entity.class);
    bodyBuilder.appendFormalLine("%1$s<%2$s> rootEntry = cq.from(%2$s.class);", getNameOfJavaType(JpaJavaType.ROOT), getNameOfJavaType(this.entity));
    // CriteriaQuery<Owner> all = cq.select(rootEntry);
    bodyBuilder.appendFormalLine("%s<%s> all = cq.select(rootEntry);", getNameOfJavaType(JpaJavaType.CRITERIA_QUERY), getNameOfJavaType(this.entity));
    // TypedQuery<Owner> allQuery =
    bodyBuilder.appendFormalLine("%s<%s> allQuery = ", getNameOfJavaType(JpaJavaType.TYPED_QUERY), getNameOfJavaType(this.entity));
    // entityManager.createQuery(all).setFirstResult(from).setMaxResults(to);
    bodyBuilder.indent();
    bodyBuilder.appendFormalLine("%s().createQuery(all).setFirstResult(from).setMaxResults(to);", getAccessorMethod(getEntityManagerField().build()).getMethodName());
    // setData(allQuery.getResultList());
    bodyBuilder.indentRemove();
    bodyBuilder.appendFormalLine("%s(allQuery.getResultList());", getMutatorMethod(getDataField().build()).getMethodName());
    // if (getData() == null) {
    bodyBuilder.appendFormalLine("if (%s() == null) {", getAccessorMethod(getDataField().build()).getMethodName());
    // throw new IllegalStateException(
    bodyBuilder.indent();
    bodyBuilder.appendFormalLine("throw new IllegalStateException(");
    // "Find entries implementation for 'Owner' illegally returned null");
    bodyBuilder.indent();
    bodyBuilder.appendFormalLine("\"Find entries implementation for '%s' illegally returned null\");", getNameOfJavaType(this.entity));
    // }
    bodyBuilder.indentRemove();
    bodyBuilder.indentRemove();
    bodyBuilder.appendFormalLine("}");
    // if (!data.isEmpty()) {
    bodyBuilder.appendFormalLine("if (!%s().isEmpty()) {", getAccessorMethod(getDataField().build()).getMethodName());
    // return;
    bodyBuilder.indent();
    bodyBuilder.appendFormalLine("return;");
    // }
    bodyBuilder.indentRemove();
    bodyBuilder.appendFormalLine("}");
    // setData(new ArrayList<Entity>());
    bodyBuilder.newLine();
    bodyBuilder.appendFormalLine("%s(new %s<%s>());", getMutatorMethod(getDataField().build()).getMethodName(), getNameOfJavaType(JdkJavaType.ARRAY_LIST), getNameOfJavaType(this.entity));
    // for (int i = from; i < to; i++) {
    bodyBuilder.appendFormalLine("for (int i = from; i < to; i++) {");
    bodyBuilder.indent();
    // Entity obj = factory.create(i);
    bodyBuilder.appendFormalLine("%s %s = %s().%s(i);", getNameOfJavaType(this.entity), OBJ_VAR, getAccessorMethod(getEntityFactoryField().build()).getMethodName(), this.entityFactoryMetadata.getCreateFactoryMethodName());
    // try {
    bodyBuilder.appendFormalLine("try {");
    bodyBuilder.indent();
    // entityManager.persist(obj);
    bodyBuilder.appendFormalLine("%s().persist(%s);", getAccessorMethod(getEntityManagerField().build()).getMethodName(), OBJ_VAR);
    // } catch (final ConstraintViolationException e) {
    bodyBuilder.indentRemove();
    bodyBuilder.appendFormalLine("} catch (final %s e) {", getNameOfJavaType(Jsr303JavaType.CONSTRAINT_VIOLATION_EXCEPTION));
    bodyBuilder.indent();
    // final StringBuilder msg = new StringBuilder();
    bodyBuilder.appendFormalLine("final StringBuilder msg = new StringBuilder();");
    // for (Iterator<ConstraintViolation<?>> iter = e.getConstraintViolations().iterator(); iter
    bodyBuilder.appendFormalLine("for (%s<%s<?>> iter = e.getConstraintViolations().iterator(); iter", getNameOfJavaType(JdkJavaType.ITERATOR), getNameOfJavaType(Jsr303JavaType.CONSTRAINT_VIOLATION));
    // .hasNext();) {
    bodyBuilder.indent();
    bodyBuilder.appendFormalLine("  .hasNext();) {");
    // final ConstraintViolation<?> cv = iter.next();
    bodyBuilder.appendFormalLine("final %s<?> cv = iter.next();", getNameOfJavaType(Jsr303JavaType.CONSTRAINT_VIOLATION));
    // msg.append("[").append(cv.getRootBean().getClass().getName()).append(".")
    bodyBuilder.appendFormalLine("msg.append(\"[\").append(cv.getRootBean().getClass().getName()).append(\".\")");
    // .append(cv.getPropertyPath()).append(": ").append(cv.getMessage())
    bodyBuilder.appendFormalLine(".append(cv.getPropertyPath()).append(\": \").append(cv.getMessage())");
    // .append(" (invalid value = ").append(cv.getInvalidValue()).append(")").append("]");
    bodyBuilder.appendFormalLine(".append(\" (invalid value = \").append(cv.getInvalidValue()).append(\")\").append(\"]\");");
    // }
    bodyBuilder.indentRemove();
    bodyBuilder.appendFormalLine("}");
    // throw new IllegalStateException(msg.toString(), e);
    bodyBuilder.appendFormalLine("throw new IllegalStateException(msg.toString(), e);");
    // }
    bodyBuilder.indentRemove();
    bodyBuilder.appendFormalLine("}");
    // entityManager.flush();
    bodyBuilder.appendFormalLine("%s().%s();", getAccessorMethod(getEntityManagerField().build()).getMethodName(), FLUSH_METHOD_NAME);
    // data.add(obj);
    bodyBuilder.appendFormalLine("%s().add(%s);", getAccessorMethod(getDataField().build()).getMethodName(), OBJ_VAR);
    // }
    bodyBuilder.indentRemove();
    bodyBuilder.appendFormalLine("}");
    // Create the method
    MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(getId(), Modifier.PUBLIC, methodName, returnType, AnnotatedJavaType.convertFromJavaTypes(parameterTypes), parameterNames, bodyBuilder);
    CommentStructure comment = new CommentStructure();
    JavadocComment javadocComment = new JavadocComment("Creates the initial list of generated entities.");
    comment.addComment(javadocComment, CommentLocation.BEGINNING);
    methodBuilder.setCommentStructure(comment);
    return methodBuilder;
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) Jsr303JavaType(org.springframework.roo.model.Jsr303JavaType) JdkJavaType(org.springframework.roo.model.JdkJavaType) JpaJavaType(org.springframework.roo.model.JpaJavaType) JavaType(org.springframework.roo.model.JavaType) JavadocComment(org.springframework.roo.classpath.details.comments.JavadocComment) MethodMetadataBuilder(org.springframework.roo.classpath.details.MethodMetadataBuilder) MethodMetadata(org.springframework.roo.classpath.details.MethodMetadata) InvocableMemberBodyBuilder(org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder) CommentStructure(org.springframework.roo.classpath.details.comments.CommentStructure)

Example 83 with MethodMetadataBuilder

use of org.springframework.roo.classpath.details.MethodMetadataBuilder in project spring-roo by spring-projects.

the class JpaEntityFactoryMetadata method addEmbeddedClassFieldMutatorMethodsToBuilder.

private void addEmbeddedClassFieldMutatorMethodsToBuilder(final EmbeddedHolder embeddedHolder, final Set<ClassOrInterfaceTypeDetails> dataOnDemandClasses) {
    final JavaType embeddedFieldType = embeddedHolder.getEmbeddedField().getFieldType();
    final JavaType[] parameterTypes = { embeddedFieldType, JavaType.INT_PRIMITIVE };
    final List<JavaSymbolName> parameterNames = Arrays.asList(OBJ_SYMBOL, INDEX_SYMBOL);
    for (final FieldMetadata field : embeddedHolder.getFields()) {
        final String initializer = getFieldInitializer(field, null, dataOnDemandClasses);
        final JavaSymbolName fieldMutatorMethodName = BeanInfoUtils.getMutatorMethodName(field.getFieldName());
        final InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
        bodyBuilder.append(getFieldValidationBody(field, initializer, fieldMutatorMethodName, false));
        final JavaSymbolName embeddedClassMethodName = getEmbeddedFieldMutatorMethodName(embeddedHolder.getEmbeddedField().getFieldName(), field.getFieldName());
        if (governorHasMethod(embeddedClassMethodName, parameterTypes)) {
            // Method found in governor so do not create method in ITD
            continue;
        }
        builder.addMethod(new MethodMetadataBuilder(getId(), Modifier.PUBLIC, embeddedClassMethodName, JavaType.VOID_PRIMITIVE, AnnotatedJavaType.convertFromJavaTypes(parameterTypes), parameterNames, bodyBuilder));
    }
}
Also used : AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) JdkJavaType(org.springframework.roo.model.JdkJavaType) JavaType(org.springframework.roo.model.JavaType) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) FieldMetadata(org.springframework.roo.classpath.details.FieldMetadata) MethodMetadataBuilder(org.springframework.roo.classpath.details.MethodMetadataBuilder) InvocableMemberBodyBuilder(org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder)

Example 84 with MethodMetadataBuilder

use of org.springframework.roo.classpath.details.MethodMetadataBuilder in project spring-roo by spring-projects.

the class IdentifierMetadata method getAccessors.

/**
 * Locates the accessor methods.
 * <p>
 * If {@link #getFieldBuilders()} returns fields created by this ITD, public
 * accessors will automatically be produced in the declaring class.
 *
 * @param fields
 * @return the accessors (never returns null)
 */
private List<MethodMetadataBuilder> getAccessors(final List<FieldMetadataBuilder> fields) {
    final List<MethodMetadataBuilder> accessors = new ArrayList<MethodMetadataBuilder>();
    // Compute the names of the accessors that will be produced
    for (final FieldMetadataBuilder field : fields) {
        final JavaSymbolName requiredAccessorName = BeanInfoUtils.getAccessorMethodName(field.getFieldName(), field.getFieldType());
        final MethodMetadata accessor = getGovernorMethod(requiredAccessorName);
        if (accessor == null) {
            accessors.add(getAccessorMethod(field.getFieldName(), field.getFieldType()));
        } else {
            Validate.isTrue(Modifier.isPublic(accessor.getModifier()), "User provided field but failed to provide a public '%s()' method in '%s'", requiredAccessorName.getSymbolName(), destination.getFullyQualifiedTypeName());
            accessors.add(new MethodMetadataBuilder(accessor));
        }
    }
    return accessors;
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) MethodMetadataBuilder(org.springframework.roo.classpath.details.MethodMetadataBuilder) ArrayList(java.util.ArrayList) MethodMetadata(org.springframework.roo.classpath.details.MethodMetadata) FieldMetadataBuilder(org.springframework.roo.classpath.details.FieldMetadataBuilder)

Example 85 with MethodMetadataBuilder

use of org.springframework.roo.classpath.details.MethodMetadataBuilder in project spring-roo by spring-projects.

the class JpaUnitTestMetadata method getCleanMethod.

/**
 * Obtains a method annotated with @After for doing the test class teardown phase
 * after finishing each test.
 *
 * @return {@link MethodMetadataBuilder}
 */
private MethodMetadataBuilder getCleanMethod() {
    final InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
    bodyBuilder.newLine();
    bodyBuilder.appendFormalLine("// Clean needed after executing each test method");
    bodyBuilder.appendFormalLine("// To be implemented by developer");
    bodyBuilder.newLine();
    // Use the MethodMetadataBuilder for easy creation of MethodMetadata
    MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(getId(), Modifier.PUBLIC, new JavaSymbolName("clean"), JavaType.VOID_PRIMITIVE, bodyBuilder);
    // Add @After
    methodBuilder.addAnnotation(new AnnotationMetadataBuilder(AFTER).build());
    // Add comment
    CommentStructure comment = new CommentStructure();
    JavadocComment javaDocComment = new JavadocComment("This method will be automatically executed after each test method for freeing resources allocated with @Before annotated method.");
    comment.addComment(javaDocComment, CommentLocation.BEGINNING);
    methodBuilder.setCommentStructure(comment);
    return methodBuilder;
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) JavadocComment(org.springframework.roo.classpath.details.comments.JavadocComment) MethodMetadataBuilder(org.springframework.roo.classpath.details.MethodMetadataBuilder) InvocableMemberBodyBuilder(org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder) CommentStructure(org.springframework.roo.classpath.details.comments.CommentStructure)

Aggregations

MethodMetadataBuilder (org.springframework.roo.classpath.details.MethodMetadataBuilder)155 JavaSymbolName (org.springframework.roo.model.JavaSymbolName)143 AnnotatedJavaType (org.springframework.roo.classpath.details.annotations.AnnotatedJavaType)120 InvocableMemberBodyBuilder (org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder)117 MethodMetadata (org.springframework.roo.classpath.details.MethodMetadata)115 ArrayList (java.util.ArrayList)110 AnnotationMetadataBuilder (org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)87 JavaType (org.springframework.roo.model.JavaType)65 SpringJavaType (org.springframework.roo.model.SpringJavaType)48 SpringletsJavaType (org.springframework.roo.model.SpringletsJavaType)43 JdkJavaType (org.springframework.roo.model.JdkJavaType)34 FieldMetadata (org.springframework.roo.classpath.details.FieldMetadata)30 Jsr303JavaType (org.springframework.roo.model.Jsr303JavaType)27 ServiceMetadata (org.springframework.roo.addon.layers.service.addon.ServiceMetadata)15 CommentStructure (org.springframework.roo.classpath.details.comments.CommentStructure)14 JavadocComment (org.springframework.roo.classpath.details.comments.JavadocComment)14 RelationInfo (org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata.RelationInfo)12 RelationInfoExtended (org.springframework.roo.addon.web.mvc.controller.addon.RelationInfoExtended)11 JpaJavaType (org.springframework.roo.model.JpaJavaType)9 JpaEntityMetadata (org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata)7