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();
}
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;
}
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));
}
}
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;
}
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;
}
Aggregations