use of org.springframework.roo.classpath.details.comments.CommentStructure in project spring-roo by spring-projects.
the class JpaDataOnDemandMetadata method getRandomPersistentEntityMethod.
/**
* @return the "getRandomEntity():Entity" method (never returns null)
*/
private MethodMetadata getRandomPersistentEntityMethod() {
// Method definition to find or build
final JavaSymbolName methodName = new JavaSymbolName("getRandom" + this.entity.getSimpleTypeName());
// Locate user-defined method
final MethodMetadata userMethod = getGovernorMethod(methodName);
if (userMethod != null) {
Validate.isTrue(userMethod.getReturnType().equals(this.entity), "Method '%s' on '%s' must return '%s'", methodName, this.destination, this.entity.getSimpleTypeName());
this.randomPersistentEntityMethod = userMethod;
return userMethod;
}
// Create method
final InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
// init();
bodyBuilder.appendFormalLine("init();");
// return data.get(rnd.nextInt(data.size()));
bodyBuilder.appendFormalLine("return %1$s().get(%2$s().nextInt(%1$s().size()));", getAccessorMethod(getDataField().build()).getMethodName(), getAccessorMethod(getRndField().build()).getMethodName());
final MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(getId(), Modifier.PUBLIC, methodName, this.entity, bodyBuilder);
CommentStructure comment = new CommentStructure();
JavadocComment javadocComment = new JavadocComment(String.format("Returns a generated and persisted {@link %s} in a random index.", this.entity.getSimpleTypeName()), null, String.format("%1$s a random {@link %1$s}", this.entity.getSimpleTypeName()), null);
comment.addComment(javadocComment, CommentLocation.BEGINNING);
methodBuilder.setCommentStructure(comment);
this.randomPersistentEntityMethod = methodBuilder.build();
return this.randomPersistentEntityMethod;
}
use of org.springframework.roo.classpath.details.comments.CommentStructure in project spring-roo by spring-projects.
the class JpaDataOnDemandMetadata method getConstructorWithEntityManagerAndSize.
/**
* Builds constructor with EntityManager and size arguments.
*
* @return {@link ConstructorMetadataBuilder} for adding constructor to ITD
*/
private ConstructorMetadataBuilder getConstructorWithEntityManagerAndSize() {
ConstructorMetadataBuilder constructorBuilder = new ConstructorMetadataBuilder(getId());
InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
constructorBuilder.addParameter(ENTITY_MANAGER_VAR, JpaJavaType.ENTITY_MANAGER);
constructorBuilder.addParameter(SIZE_VAR, JavaType.INT_PRIMITIVE);
bodyBuilder.appendFormalLine(String.format("%1$s(%2$s);", getMutatorMethod(getEntityManagerField().build()).getMethodName(), ENTITY_MANAGER_VAR));
bodyBuilder.appendFormalLine(String.format("%1$s(%2$s);", getMutatorMethod(getSizeField()).getMethodName(), SIZE_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");
paramsInfo.add(SIZE_VAR + " the number of entities to create and persist initially.");
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.details.comments.CommentStructure in project spring-roo by spring-projects.
the class JpaDataOnDemandMetadata method getNewRandomTransientEntityMethod.
private MethodMetadata getNewRandomTransientEntityMethod() {
// Method definition to find or build
final JavaSymbolName methodName = new JavaSymbolName("getNewRandomTransient" + this.entity.getSimpleTypeName());
// Locate user-defined method
final MethodMetadata userMethod = getGovernorMethod(methodName);
if (userMethod != null) {
Validate.isTrue(userMethod.getReturnType().equals(entity), "Method '%s' on '%s' must return '%s'", methodName, this.destination, this.entity.getSimpleTypeName());
this.newTransientEntityMethod = userMethod;
return userMethod;
}
// Create method
final InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
// int randomIndex = getSize() + rnd.nextInt(Integer.MAX_VALUE - getSize());
bodyBuilder.appendFormalLine("int randomIndex = %1$s() + %2$s().nextInt(Integer.MAX_VALUE - %1$s());", this.sizeAccesorName, getAccessorMethod(getRndField().build()).getMethodName());
// return factory.create(randomIndex);
bodyBuilder.appendFormalLine("return %s().%s(randomIndex);", getAccessorMethod(getEntityFactoryField().build()).getMethodName(), this.entityFactoryMetadata.getCreateFactoryMethodName());
final MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(getId(), Modifier.PUBLIC, methodName, this.entity, bodyBuilder);
CommentStructure comment = new CommentStructure();
JavadocComment javadocComment = new JavadocComment(String.format("Creates a new transient %s in a random index out of " + "the initial list of the created entities,".concat(IOUtils.LINE_SEPARATOR).concat("with an index greater than {@link %s#getSize()} - 1."), this.entity.getSimpleTypeName(), this.governorPhysicalTypeMetadata.getType().getSimpleTypeName()), null, String.format("%1$s the generated transient {@link %1$s}", this.entity.getSimpleTypeName()), null);
comment.addComment(javadocComment, CommentLocation.BEGINNING);
methodBuilder.setCommentStructure(comment);
this.newTransientEntityMethod = methodBuilder.build();
return this.newTransientEntityMethod;
}
use of org.springframework.roo.classpath.details.comments.CommentStructure in project spring-roo by spring-projects.
the class JpaDataOnDemandMetadata method getDataField.
/**
* @return the "data" field to use, which is either provided by the user or
* produced on demand (never returns null)
*/
private FieldMetadataBuilder getDataField() {
final List<JavaType> parameterTypes = Arrays.asList(entity);
final JavaType listType = new JavaType(LIST.getFullyQualifiedTypeName(), 0, DataType.TYPE, null, parameterTypes);
int index = -1;
while (true) {
// Compute the required field name
index++;
// The type parameters to be used by the field type
final JavaSymbolName fieldName = new JavaSymbolName("data" + StringUtils.repeat("_", index));
dataFieldName = fieldName;
final FieldMetadata candidate = governorTypeDetails.getField(fieldName);
if (candidate != null) {
// Verify if candidate is suitable
if (!Modifier.isPrivate(candidate.getModifier())) {
// next possible name)
continue;
}
if (!candidate.getFieldType().equals(listType)) {
// The equals method also verifies type params are present
continue;
}
// we assume the user knows what they're doing and have made one
return new FieldMetadataBuilder(candidate);
}
// Candidate not found, so let's create one
FieldMetadataBuilder fieldBuilder = new FieldMetadataBuilder(getId(), Modifier.PRIVATE, new ArrayList<AnnotationMetadataBuilder>(), fieldName, listType);
CommentStructure comment = new CommentStructure();
comment.addComment(new JavadocComment("List of created entities."), CommentLocation.BEGINNING);
fieldBuilder.setCommentStructure(comment);
return fieldBuilder;
}
}
use of org.springframework.roo.classpath.details.comments.CommentStructure in project spring-roo by spring-projects.
the class JpaDataOnDemandMetadata method getSizeField.
/**
* Creates size field.
*
* @return {@link FieldMetadataBuilder} for building field into ITD.
*/
private FieldMetadata getSizeField() {
// Create field
FieldMetadataBuilder sizeField = new FieldMetadataBuilder(getId(), Modifier.PRIVATE, new ArrayList<AnnotationMetadataBuilder>(), new JavaSymbolName(SIZE_VAR), JavaType.INT_PRIMITIVE);
CommentStructure comment = new CommentStructure();
comment.addComment(new JavadocComment("Number of elements to create and persist."), CommentLocation.BEGINNING);
sizeField.setCommentStructure(comment);
return sizeField.build();
}
Aggregations