use of org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder in project spring-roo by spring-projects.
the class JpaEntityFactoryMetadata method getCreateMethod.
private MethodMetadata getCreateMethod() {
// Define methodName
final JavaSymbolName methodName = CREATE_FACTORY_METHOD_NAME;
List<JavaType> parameterTypes = new ArrayList<JavaType>();
parameterTypes.add(JavaType.INT_PRIMITIVE);
// Check if method exists
MethodMetadata existingMethod = getGovernorMethod(methodName, parameterTypes);
if (existingMethod != null) {
return existingMethod;
}
List<JavaSymbolName> parameterNames = new ArrayList<JavaSymbolName>();
parameterNames.add(INDEX_SYMBOL);
// Add body
InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
// Entity obj = new Entity();
bodyBuilder.appendFormalLine("%1$s %2$s = new %1$s();", getNameOfJavaType(this.entity), OBJ_SYMBOL);
// Set values for transient object
for (final Map.Entry<FieldMetadata, JpaEntityFactoryMetadata> entry : locatedFields.entrySet()) {
bodyBuilder.appendFormalLine("%s(%s, %s);", BeanInfoUtils.getMutatorMethodName(entry.getKey()), OBJ_SYMBOL, INDEX_SYMBOL);
}
// return obj;
bodyBuilder.appendFormalLine("return %s;", OBJ_SYMBOL);
// Create method
MethodMetadataBuilder method = new MethodMetadataBuilder(this.getId(), Modifier.PUBLIC, methodName, this.entity, AnnotatedJavaType.convertFromJavaTypes(parameterTypes), parameterNames, bodyBuilder);
CommentStructure commentStructure = new CommentStructure();
List<String> paramsInfo = new ArrayList<String>();
paramsInfo.add(String.format("%s position of the %s", INDEX_VAR, this.entity.getSimpleTypeName()));
JavadocComment comment = new JavadocComment(String.format("Creates a new {@link %s} with the given %s.", this.entity.getSimpleTypeName(), INDEX_VAR), paramsInfo, String.format("a new transient %s", this.entity.getSimpleTypeName()), null);
commentStructure.addComment(comment, CommentLocation.BEGINNING);
method.setCommentStructure(commentStructure);
return method.build();
}
use of org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder in project spring-roo by spring-projects.
the class JpaEntityFactoryMetadata method getFieldMutatorMethods.
private List<MethodMetadataBuilder> getFieldMutatorMethods() {
final List<MethodMetadataBuilder> fieldMutatorMethods = new ArrayList<MethodMetadataBuilder>();
final List<JavaSymbolName> parameterNames = Arrays.asList(OBJ_SYMBOL, INDEX_SYMBOL);
final JavaType[] parameterTypes = { entity, JavaType.INT_PRIMITIVE };
for (final Map.Entry<FieldMetadata, String> entry : fieldInitializers.entrySet()) {
final FieldMetadata field = entry.getKey();
final JavaSymbolName mutatorName = BeanInfoUtils.getMutatorMethodName(field.getFieldName());
// Locate user-defined method
if (governorHasMethod(mutatorName, parameterTypes)) {
// Method found in governor so do not create method in ITD
continue;
}
// Method not on governor so need to create it
final String initializer = entry.getValue();
if (!StringUtils.isBlank(initializer)) {
final InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
bodyBuilder.append(getFieldValidationBody(field, initializer, mutatorName, false));
fieldMutatorMethods.add(new MethodMetadataBuilder(getId(), Modifier.PUBLIC, mutatorName, JavaType.VOID_PRIMITIVE, AnnotatedJavaType.convertFromJavaTypes(parameterTypes), parameterNames, bodyBuilder));
}
}
return fieldMutatorMethods;
}
use of org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder in project spring-roo by spring-projects.
the class DtoMetadata method getConstructor.
/**
* Builds constructor for initializing <code>final</code> fields.
*
* @return ConstructorMetadataBuilder for adding constructor to ITD
*/
private ConstructorMetadataBuilder getConstructor() {
ConstructorMetadataBuilder constructorBuilder = new ConstructorMetadataBuilder(getId());
InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
if (annotationValues.getImmutable() == true) {
for (FieldMetadata field : fields) {
// Add all fields excluding Serializable field
if (!field.getFieldName().equals(serialField)) {
String fieldName = field.getFieldName().getSymbolName();
constructorBuilder.addParameter(fieldName, field.getFieldType());
bodyBuilder.appendFormalLine(String.format("this.%s = %s;", fieldName, fieldName));
}
}
}
constructorBuilder.setModifier(Modifier.PUBLIC);
constructorBuilder.setBodyBuilder(bodyBuilder);
return constructorBuilder;
}
use of org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder in project spring-roo by spring-projects.
the class JpaDataOnDemandMetadata method getConstructorWithEntityManager.
/**
* Builds constructor with EntityManager argument.
*
* @return ConstructorMetadataBuilder for adding constructor to ITD
*/
private ConstructorMetadataBuilder getConstructorWithEntityManager() {
ConstructorMetadataBuilder constructorBuilder = new ConstructorMetadataBuilder(getId());
InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
constructorBuilder.addParameter(ENTITY_MANAGER_VAR, JpaJavaType.ENTITY_MANAGER);
// this(entityManager, 10);
bodyBuilder.appendFormalLine(String.format("this(%1$s, 10);", ENTITY_MANAGER_VAR));
constructorBuilder.setModifier(Modifier.PUBLIC);
constructorBuilder.setBodyBuilder(bodyBuilder);
CommentStructure comment = new CommentStructure();
List<String> paramsInfo = new ArrayList<String>();
paramsInfo.add(ENTITY_MANAGER_VAR + " to persist entities");
JavadocComment javadocComment = new JavadocComment(String.format("Creates a new {@link %s}.", this.governorPhysicalTypeMetadata.getType().getSimpleTypeName()), paramsInfo, null, null);
comment.addComment(javadocComment, CommentLocation.BEGINNING);
constructorBuilder.setCommentStructure(comment);
return constructorBuilder;
}
use of org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder 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;
}
Aggregations