use of org.springframework.roo.classpath.details.FieldMetadataBuilder in project spring-roo by spring-projects.
the class JpaFieldCreatorProvider method insertField.
public void insertField(final FieldDetails fieldDetails, final boolean permitReservedWords, final boolean transientModifier, boolean evictCacheForTargetClass) {
String module = getFieldModule(fieldDetails);
final FieldMetadataBuilder fieldBuilder = generateFieldBuilder(fieldDetails, permitReservedWords, transientModifier);
typeManagementService.addField(fieldBuilder.build(), evictCacheForTargetClass);
if (module != null) {
projectOperations.addModuleDependency(module);
}
}
use of org.springframework.roo.classpath.details.FieldMetadataBuilder in project spring-roo by spring-projects.
the class JpaFieldCreatorProvider method generateFieldBuilder.
/**
* Generates Field builder based on fieldDetails
*
* @param fieldDetails
* @param permitReservedWords
* @param transientModifier
* @return
*/
private FieldMetadataBuilder generateFieldBuilder(final FieldDetails fieldDetails, final boolean permitReservedWords, final boolean transientModifier) {
if (!permitReservedWords) {
ReservedWords.verifyReservedWordsNotPresent(fieldDetails.getFieldName());
if (fieldDetails.getColumn() != null) {
ReservedWords.verifyReservedWordsNotPresent(fieldDetails.getColumn());
}
}
final List<AnnotationMetadataBuilder> annotations = fieldDetails.getInitedAnnotations();
fieldDetails.decorateAnnotationsList(annotations);
fieldDetails.setAnnotations(annotations);
String initializer = null;
if (fieldDetails instanceof CollectionField) {
final CollectionField collectionField = (CollectionField) fieldDetails;
initializer = "new " + collectionField.getInitializer() + "()";
} else if (fieldDetails instanceof DateField && fieldDetails.getFieldName().getSymbolName().equals("created")) {
initializer = "new Date()";
}
int modifier = Modifier.PRIVATE;
if (transientModifier) {
modifier += Modifier.TRANSIENT;
}
fieldDetails.setModifiers(modifier);
// Format the passed-in comment (if given)
formatFieldComment(fieldDetails);
final FieldMetadataBuilder fieldBuilder = new FieldMetadataBuilder(fieldDetails);
fieldBuilder.setFieldInitializer(initializer);
return fieldBuilder;
}
use of org.springframework.roo.classpath.details.FieldMetadataBuilder in project spring-roo by spring-projects.
the class JpaEntityMetadata method getIterableToAddCantBeNullConstant.
/**
* This method obtains the constant that contains the message
* "The given Iterable of items to add can't be null!".
*
* @return name of the generated constant
*/
private FieldMetadata getIterableToAddCantBeNullConstant() {
// Check if iterableCantBeNullConstant already exists
if (iterableToAddCantBeNullConstant != null) {
return iterableToAddCantBeNullConstant;
}
// If not exists, generate a new one and include into builder
FieldMetadataBuilder constant = new FieldMetadataBuilder(getId(), Modifier.PUBLIC + Modifier.STATIC + Modifier.FINAL, new JavaSymbolName("ITERABLE_TO_ADD_CANT_BE_NULL_MESSAGE"), JavaType.STRING, "\"The given Iterable of items to add can't be null!\"");
iterableToAddCantBeNullConstant = constant.build();
return iterableToAddCantBeNullConstant;
}
use of org.springframework.roo.classpath.details.FieldMetadataBuilder in project spring-roo by spring-projects.
the class JpaOperationsImpl method getIdentifierField.
/**
* This method generates the identifier field using the provided values.
*
* @param entity
* @param identifierField
* @param identifierType
* @param identifierColumn
* @param sequenceName
* @param identifierStrategy
* @param inheritanceType
* @return
*/
private FieldMetadata getIdentifierField(final JavaType entity, String identifierField, final JavaType identifierType, final String identifierColumn, final String sequenceName, IdentifierStrategy identifierStrategy, InheritanceType inheritanceType) {
final List<AnnotationMetadataBuilder> annotations = new ArrayList<AnnotationMetadataBuilder>();
final boolean hasIdClass = !(identifierType.isCoreType());
final JavaType annotationType = hasIdClass ? EMBEDDED_ID : ID;
annotations.add(new AnnotationMetadataBuilder(annotationType));
if (StringUtils.isEmpty(identifierField)) {
identifierField = "id";
}
// Compute the column name, as required
if (!hasIdClass) {
if (!"".equals(sequenceName)) {
// ROO-3719: Add SEQUENCE as @GeneratedValue strategy
// Check if provided identifierStrategy is valid
boolean isValidIdentifierStrategy = false;
if (identifierStrategy != null) {
for (IdentifierStrategy identifierStrategyType : IdentifierStrategy.values()) {
if (identifierStrategyType.name().equals(identifierStrategy.name())) {
isValidIdentifierStrategy = true;
break;
}
}
}
if (!isValidIdentifierStrategy) {
identifierStrategy = IdentifierStrategy.AUTO;
}
// InheritanceType.TABLE_PER_CLASS)
if (IdentifierStrategy.AUTO.name().equals(identifierStrategy.name())) {
if (inheritanceType != null) {
if ("TABLE_PER_CLASS".equals(inheritanceType.name())) {
identifierStrategy = IdentifierStrategy.TABLE;
}
}
}
final AnnotationMetadataBuilder generatedValueBuilder = new AnnotationMetadataBuilder(GENERATED_VALUE);
generatedValueBuilder.addEnumAttribute("strategy", new EnumDetails(GENERATION_TYPE, new JavaSymbolName(identifierStrategy.name())));
if (StringUtils.isNotBlank(sequenceName)) {
final String sequenceKey = StringUtils.uncapitalize(entity.getSimpleTypeName()) + "Gen";
generatedValueBuilder.addStringAttribute("generator", sequenceKey);
final AnnotationMetadataBuilder sequenceGeneratorBuilder = new AnnotationMetadataBuilder(SEQUENCE_GENERATOR);
sequenceGeneratorBuilder.addStringAttribute("name", sequenceKey);
sequenceGeneratorBuilder.addStringAttribute("sequenceName", sequenceName);
annotations.add(sequenceGeneratorBuilder);
}
annotations.add(generatedValueBuilder);
}
// User has specified alternative columnName
if (StringUtils.isNotBlank(identifierColumn)) {
final AnnotationMetadataBuilder columnBuilder = new AnnotationMetadataBuilder(COLUMN);
columnBuilder.addStringAttribute("name", identifierColumn);
annotations.add(columnBuilder);
}
}
FieldDetails identifierFieldDetails = new FieldDetails(getTypeLocationService().getPhysicalTypeIdentifier(entity), identifierType, new JavaSymbolName(identifierField));
identifierFieldDetails.setModifiers(Modifier.PRIVATE);
identifierFieldDetails.addAnnotations(annotations);
return new FieldMetadataBuilder(identifierFieldDetails).build();
}
use of org.springframework.roo.classpath.details.FieldMetadataBuilder in project spring-roo by spring-projects.
the class JpaEntityFactoryMetadata method addCollaboratingFieldsToBuilder.
private void addCollaboratingFieldsToBuilder(final Set<ClassOrInterfaceTypeDetails> entityFactoryClasses) {
final Set<JavaSymbolName> fields = new LinkedHashSet<JavaSymbolName>();
for (final JavaType entityNeedingCollaborator : this.requiredDataOnDemandCollaborators) {
final JavaType collaboratorType = getCollaboratingType(entityNeedingCollaborator, entityFactoryClasses);
if (collaboratorType != null) {
final String collaboratingFieldName = getCollaboratingFieldName(entityNeedingCollaborator, entityFactoryClasses).getSymbolName();
final JavaSymbolName fieldSymbolName = new JavaSymbolName(collaboratingFieldName);
final FieldMetadata candidate = governorTypeDetails.getField(fieldSymbolName);
if (candidate != null) {
// We really expect the field to be correct if we're going to
// rely on it
Validate.isTrue(candidate.getFieldType().equals(collaboratorType), "Field '%s' on '%s' must be of type '%s'", collaboratingFieldName, destination.getFullyQualifiedTypeName(), collaboratorType.getFullyQualifiedTypeName());
Validate.isTrue(Modifier.isPrivate(candidate.getModifier()), "Field '%s' on '%s' must be private", collaboratingFieldName, destination.getFullyQualifiedTypeName());
Validate.notNull(MemberFindingUtils.getAnnotationOfType(candidate.getAnnotations(), AUTOWIRED), "Field '%s' on '%s' must be @Autowired", collaboratingFieldName, destination.getFullyQualifiedTypeName());
// It's ok, so we can move onto the new field
continue;
}
// Create field and add it to the ITD, if it hasn't already been
if (!fields.contains(fieldSymbolName)) {
// Must make the field
final List<AnnotationMetadataBuilder> annotations = new ArrayList<AnnotationMetadataBuilder>();
annotations.add(new AnnotationMetadataBuilder(AUTOWIRED));
builder.addField(new FieldMetadataBuilder(getId(), 0, annotations, fieldSymbolName, collaboratorType));
fields.add(fieldSymbolName);
}
}
}
}
Aggregations