use of org.springframework.roo.model.JavaType in project spring-roo by spring-projects.
the class JpaDataOnDemandCreator method getEntityFactory.
@Override
public JavaType getEntityFactory(JavaType entity) {
Set<ClassOrInterfaceTypeDetails> dataOnDemandCids = typeLocationService.findClassesOrInterfaceDetailsWithAnnotation(RooJavaType.ROO_JPA_ENTITY_FACTORY);
JavaType typeToReturn = null;
for (ClassOrInterfaceTypeDetails cid : dataOnDemandCids) {
if (entity.equals((JavaType) cid.getAnnotation(RooJavaType.ROO_JPA_ENTITY_FACTORY).getAttribute("entity").getValue())) {
typeToReturn = cid.getName();
break;
}
}
return typeToReturn;
}
use of org.springframework.roo.model.JavaType 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.JavaType 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.JavaType 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;
}
use of org.springframework.roo.model.JavaType in project spring-roo by spring-projects.
the class JavaBeanMetadata method getInterfaceMethod.
/**
* Obtains a valid MethodMetadataBuilder with necessary configuration
*
* @param method
* @return MethodMetadataBuilder
*/
private MethodMetadataBuilder getInterfaceMethod(final MethodMetadata method) {
// Compute the method name
final JavaSymbolName methodName = method.getMethodName();
// See if the type itself declared the accessor
if (governorHasMethod(methodName)) {
return null;
}
// Getting return type
JavaType returnType = method.getReturnType();
// Generating body
final InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
// If return type is not primitive, return null
if (returnType.isPrimitive()) {
JavaType baseType = returnType.getBaseType();
if (baseType.equals(JavaType.BOOLEAN_PRIMITIVE)) {
bodyBuilder.appendFormalLine("return false;");
} else if (baseType.equals(JavaType.BYTE_PRIMITIVE)) {
bodyBuilder.appendFormalLine("return 0;");
} else if (baseType.equals(JavaType.SHORT_PRIMITIVE)) {
bodyBuilder.appendFormalLine("return 0;");
} else if (baseType.equals(JavaType.INT_PRIMITIVE)) {
bodyBuilder.appendFormalLine("return 0;");
} else if (baseType.equals(JavaType.LONG_PRIMITIVE)) {
bodyBuilder.appendFormalLine("return 0;");
} else if (baseType.equals(JavaType.FLOAT_PRIMITIVE)) {
bodyBuilder.appendFormalLine("return 0;");
} else if (baseType.equals(JavaType.DOUBLE_PRIMITIVE)) {
bodyBuilder.appendFormalLine("return 0.00;");
} else if (baseType.equals(JavaType.CHAR_PRIMITIVE)) {
bodyBuilder.appendFormalLine("return '\0';");
}
} else {
bodyBuilder.appendFormalLine("return null;");
}
return new MethodMetadataBuilder(getId(), Modifier.PUBLIC, methodName, returnType, bodyBuilder);
}
Aggregations