use of org.springframework.roo.model.JavaSymbolName 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.model.JavaSymbolName 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();
}
use of org.springframework.roo.model.JavaSymbolName in project spring-roo by spring-projects.
the class JpaDataOnDemandMetadata method getSpecificEntityMethod.
private MethodMetadata getSpecificEntityMethod() {
// Method definition to find or build
final JavaSymbolName methodName = new JavaSymbolName(JpaEntityFactoryMetadata.SPECIFIC_METHOD_PREFIX + this.entity.getSimpleTypeName());
final JavaType parameterType = JavaType.INT_PRIMITIVE;
final List<JavaSymbolName> parameterNames = Arrays.asList(INDEX_SYMBOL);
// Locate user-defined method
final MethodMetadata userMethod = getGovernorMethod(methodName, parameterType);
if (userMethod != null) {
Validate.isTrue(userMethod.getReturnType().equals(this.entity), "Method '%s on '%s' must return '%s'", methodName, this.destination, this.entity.getSimpleTypeName());
this.specificEntityMethod = userMethod;
return userMethod;
}
// Create method
final InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
bodyBuilder.appendFormalLine("init();");
bodyBuilder.appendFormalLine("if (" + INDEX_VAR + " < 0) {");
bodyBuilder.indent();
bodyBuilder.appendFormalLine(INDEX_VAR + " = 0;");
bodyBuilder.indentRemove();
bodyBuilder.appendFormalLine("}");
bodyBuilder.appendFormalLine("if (" + INDEX_VAR + " > (" + getAccessorMethod(getDataField().build()).getMethodName() + "().size() - 1)) {");
bodyBuilder.indent();
bodyBuilder.appendFormalLine(INDEX_VAR + " = " + getAccessorMethod(getDataField().build()).getMethodName() + "().size() - 1;");
bodyBuilder.indentRemove();
bodyBuilder.appendFormalLine("}");
bodyBuilder.appendFormalLine("return %s().get(%s);", getAccessorMethod(getDataField().build()).getMethodName(), INDEX_VAR);
final MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(getId(), Modifier.PUBLIC, methodName, this.entity, AnnotatedJavaType.convertFromJavaTypes(parameterType), parameterNames, bodyBuilder);
CommentStructure comment = new CommentStructure();
List<String> paramsInfo = new ArrayList<String>();
paramsInfo.add(String.format("%s the position of the {@link %s} to return", INDEX_VAR, this.entity.getSimpleTypeName()));
JavadocComment javadocComment = new JavadocComment(String.format("Returns a generated and persisted {@link %s} in a given index.", this.entity.getSimpleTypeName()), paramsInfo, String.format("%1$s the specific {@link %1$s}", this.entity.getSimpleTypeName()), null);
comment.addComment(javadocComment, CommentLocation.BEGINNING);
methodBuilder.setCommentStructure(comment);
this.specificEntityMethod = methodBuilder.build();
return this.specificEntityMethod;
}
use of org.springframework.roo.model.JavaSymbolName in project spring-roo by spring-projects.
the class JavaBeanMetadata method generateGettersAndSetters.
protected void generateGettersAndSetters(final Map<FieldMetadata, JavaSymbolName> declaredFields, List<? extends MethodMetadata> interfaceMethods) {
// Add getters and setters
for (final Entry<FieldMetadata, JavaSymbolName> entry : declaredFields.entrySet()) {
final FieldMetadata field = entry.getKey();
final MethodMetadataBuilder accessorMethod = getDeclaredGetter(field);
final MethodMetadataBuilder mutatorMethod = getDeclaredSetter(field);
// Check to see if GAE is interested
if (entry.getValue() != null) {
JavaSymbolName hiddenIdFieldName;
if (field.getFieldType().isCommonCollectionType()) {
hiddenIdFieldName = governorTypeDetails.getUniqueFieldName(field.getFieldName().getSymbolName() + "Keys");
builder.getImportRegistrationResolver().addImport(GAE_DATASTORE_KEY_FACTORY);
builder.addField(getMultipleEntityIdField(hiddenIdFieldName));
} else {
hiddenIdFieldName = governorTypeDetails.getUniqueFieldName(field.getFieldName().getSymbolName() + "Id");
builder.addField(getSingularEntityIdField(hiddenIdFieldName));
}
processGaeAnnotations(field);
InvocableMemberBodyBuilder gaeAccessorBody = getGaeAccessorBody(field, hiddenIdFieldName);
accessorMethod.setBodyBuilder(gaeAccessorBody);
InvocableMemberBodyBuilder gaeMutatorBody = getGaeMutatorBody(field, hiddenIdFieldName);
mutatorMethod.setBodyBuilder(gaeMutatorBody);
}
// Add to mutators and accesors list and build
if (accessorMethod != null) {
this.accesorMethods.put(field.getFieldName(), accessorMethod.build());
builder.addMethod(accessorMethod);
}
if (mutatorMethod != null) {
this.mutatorMethods.put(field.getFieldName(), mutatorMethod.build());
builder.addMethod(mutatorMethod);
}
}
// Implements interface methods if exists
implementsInterfaceMethods(interfaceMethods);
}
use of org.springframework.roo.model.JavaSymbolName in project spring-roo by spring-projects.
the class JavaBeanMetadata method getDeclaredSetter.
/**
* Obtains the specific mutator method that is either contained within the
* normal Java compilation unit or will be introduced by this add-on via an
* ITD.
*
* @param field
* that already exists on the type either directly or via
* introduction (required; must be declared by this type to be
* located)
* @return the method corresponding to a mutator, or null if not found
*/
private MethodMetadataBuilder getDeclaredSetter(final FieldMetadata field) {
Validate.notNull(field, "Field required");
// Compute the mutator method name
final JavaSymbolName methodName = BeanInfoUtils.getMutatorMethodName(field);
// Compute the mutator method parameters
final JavaType parameterType = field.getFieldType();
// See if the type itself declared the mutator
if (governorHasMethod(methodName, parameterType)) {
return null;
}
// Compute the mutator method parameter names
final List<JavaSymbolName> parameterNames = Arrays.asList(field.getFieldName());
// final fields as per ROO-36)
if (annotationValues.isSettersByDefault() && !Modifier.isTransient(field.getModifier()) && !Modifier.isStatic(field.getModifier()) && !Modifier.isFinal(field.getModifier())) {
final InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
bodyBuilder.appendFormalLine("this." + field.getFieldName().getSymbolName() + " = " + field.getFieldName().getSymbolName() + ";");
return new MethodMetadataBuilder(getId(), Modifier.PUBLIC, methodName, JavaType.VOID_PRIMITIVE, AnnotatedJavaType.convertFromJavaTypes(parameterType), parameterNames, bodyBuilder);
}
return null;
}
Aggregations