use of org.springframework.roo.classpath.details.FieldMetadata in project spring-roo by spring-projects.
the class ClasspathOperationsImpl method createConstructor.
@Override
public void createConstructor(final JavaType name, final Set<String> fields) {
final ClassOrInterfaceTypeDetails javaTypeDetails = typeLocationService.getTypeDetails(name);
Validate.notNull(javaTypeDetails, "The type specified, '%s', doesn't exist", name.getFullyQualifiedTypeName());
final String declaredByMetadataId = PhysicalTypeIdentifier.createIdentifier(name, pathResolver.getFocusedPath(Path.SRC_MAIN_JAVA));
final List<FieldMetadata> constructorFields = new ArrayList<FieldMetadata>();
final List<? extends FieldMetadata> declaredFields = javaTypeDetails.getDeclaredFields();
if (fields != null) {
for (final String field : fields) {
declared: for (final FieldMetadata declaredField : declaredFields) {
if (field.equals(declaredField.getFieldName().getSymbolName())) {
constructorFields.add(declaredField);
break declared;
}
}
}
if (constructorFields.isEmpty()) {
// class, so return without creating any constructor
throw new IllegalArgumentException(String.format("The set of fields provided for the constructor does not exist in the class '%s'", name));
}
}
// Search for an existing constructor
final List<JavaType> parameterTypes = new ArrayList<JavaType>();
for (final FieldMetadata fieldMetadata : constructorFields) {
parameterTypes.add(fieldMetadata.getFieldType());
}
final ConstructorMetadata result = javaTypeDetails.getDeclaredConstructor(parameterTypes);
if (result != null) {
// Found an existing constructor on this class
throw new IllegalArgumentException(String.format("The class '%s' already has a constructor method with the same arguments and it cannot " + "be created. Use '--force' parameter to overrite it.", name));
}
final List<JavaSymbolName> parameterNames = new ArrayList<JavaSymbolName>();
final InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
bodyBuilder.appendFormalLine("super();");
for (final FieldMetadata field : constructorFields) {
final String fieldName = field.getFieldName().getSymbolName();
bodyBuilder.appendFormalLine("this." + fieldName + " = " + fieldName + ";");
parameterNames.add(field.getFieldName());
}
// Create the constructor
final ConstructorMetadataBuilder constructorBuilder = new ConstructorMetadataBuilder(declaredByMetadataId);
constructorBuilder.setModifier(Modifier.PUBLIC);
constructorBuilder.setParameterTypes(AnnotatedJavaType.convertFromJavaTypes(parameterTypes));
constructorBuilder.setParameterNames(parameterNames);
constructorBuilder.setBodyBuilder(bodyBuilder);
final ClassOrInterfaceTypeDetailsBuilder cidBuilder = new ClassOrInterfaceTypeDetailsBuilder(javaTypeDetails);
cidBuilder.addConstructor(constructorBuilder);
typeManagementService.createOrUpdateTypeOnDisk(cidBuilder.build());
}
use of org.springframework.roo.classpath.details.FieldMetadata in project spring-roo by spring-projects.
the class DtoOperationsImpl method buildFieldsFromString.
/**
* Returns the list of fields to include in Projection.
*
* @param fieldsString the fields provided by user.
* @param entity the associated entity to use for searching the fields.
* @return Map<String, FieldMetadata> with the field name to add in the Projection
* and its metadata.
*/
public Map<String, FieldMetadata> buildFieldsFromString(String fieldsString, JavaType entity) {
// Create Map for storing FieldMetadata and it's future new name
Map<String, FieldMetadata> fieldsToAdd = new LinkedHashMap<String, FieldMetadata>();
// Create array of field names from command String
fieldsString = fieldsString.trim();
String[] fields = fieldsString.split(",");
ClassOrInterfaceTypeDetails cid = typeLocationService.getTypeDetails(entity);
List<FieldMetadata> allFields = memberDetailsScanner.getMemberDetails(this.getClass().getName(), cid).getFields();
// Iterate over all specified fields
for (int i = 0; i < fields.length; i++) {
String fieldName = "";
boolean found = false;
// Iterate over all entity fields
for (FieldMetadata field : allFields) {
if (field.getFieldName().getSymbolName().equals(fields[i])) {
// If found, add field to returned map
fieldsToAdd.put(field.getFieldName().getSymbolName(), field);
found = true;
break;
}
}
if (!found) {
// The field isn't in the entity, should be in one of its relations
String[] splittedByDot = StringUtils.split(fields[i], ".");
JavaType currentEntity = entity;
if (fields[i].contains(".")) {
// Search a matching relation field
for (int t = 0; t < splittedByDot.length; t++) {
ClassOrInterfaceTypeDetails currentEntityCid = typeLocationService.getTypeDetails(currentEntity);
List<FieldMetadata> currentEntityFields = memberDetailsScanner.getMemberDetails(this.getClass().getName(), currentEntityCid).getFields();
boolean relationFieldFound = false;
// Iterate to build the field-levels of the relation field
for (FieldMetadata field : currentEntityFields) {
if (field.getFieldName().getSymbolName().equals(splittedByDot[t]) && t != splittedByDot.length - 1 && typeLocationService.getTypeDetails(field.getFieldType()) != null && typeLocationService.getTypeDetails(field.getFieldType()).getAnnotation(RooJavaType.ROO_JPA_ENTITY) != null) {
// Field is an entity and we should look into its fields
currentEntity = field.getFieldType();
found = true;
relationFieldFound = true;
if (t == 0) {
fieldName = fieldName.concat(field.getFieldName().getSymbolName());
} else {
fieldName = fieldName.concat(StringUtils.capitalize(field.getFieldName().getSymbolName()));
}
break;
} else if (field.getFieldName().getSymbolName().equals(splittedByDot[t])) {
// Add field to projection fields
fieldName = fieldName.concat(StringUtils.capitalize(field.getFieldName().getSymbolName()));
fieldsToAdd.put(fieldName, field);
found = true;
relationFieldFound = true;
break;
}
}
// If not found, relation field is bad written
if (!relationFieldFound) {
throw new IllegalArgumentException(String.format("Field %s couldn't be located in %s. Please, be sure that it is well written.", splittedByDot[t], currentEntity.getFullyQualifiedTypeName()));
}
}
} else {
// Not written as a relation field
throw new IllegalArgumentException(String.format("Field %s couldn't be located in entity %s", fields[i], entity.getFullyQualifiedTypeName()));
}
}
// If still not found, field is bad written
if (!found) {
throw new IllegalArgumentException(String.format("Field %s couldn't be located. Please, be sure that it is well written.", fields[i]));
}
}
return fieldsToAdd;
}
use of org.springframework.roo.classpath.details.FieldMetadata in project spring-roo by spring-projects.
the class DtoOperationsImpl method addFieldsToProjection.
/**
* Removes persistence annotations of provided fields and adds them to a
* ClassOrInterfaceTypeDetailsBuilder representing a Projection in construction.
* Also adds final modifier to fields if required.
*
* @param projectionBuilder the ClassOrInterfaceTypeDetailsBuilder for building the
* Projection class.
* @param fieldsToAdd the List<FieldMetadata> to add.
*/
private void addFieldsToProjection(ClassOrInterfaceTypeDetailsBuilder projectionBuilder, Map<String, FieldMetadata> fieldsToAdd) {
Iterator<Entry<String, FieldMetadata>> iterator = fieldsToAdd.entrySet().iterator();
while (iterator.hasNext()) {
Entry<String, FieldMetadata> entry = iterator.next();
FieldMetadata field = entry.getValue();
// List and Set types require special management
FieldMetadataBuilder fieldBuilder = null;
FieldDetails fieldDetails = null;
if (field.getFieldType().getFullyQualifiedTypeName().equals("java.util.Set")) {
JavaType fieldType = field.getFieldType().getParameters().get(0);
fieldDetails = new SetField(projectionBuilder.getDeclaredByMetadataId(), new JavaType(JdkJavaType.SET.getFullyQualifiedTypeName(), 0, DataType.TYPE, null, Arrays.asList(fieldType)), field.getFieldName(), fieldType, null, null, true);
fieldBuilder = new FieldMetadataBuilder(fieldDetails);
fieldBuilder.setModifier(field.getModifier());
fieldBuilder.setAnnotations(field.getAnnotations());
} else if (field.getFieldType().getFullyQualifiedTypeName().equals("java.util.List")) {
JavaType fieldType = field.getFieldType().getParameters().get(0);
fieldDetails = new ListField(projectionBuilder.getDeclaredByMetadataId(), new JavaType(JdkJavaType.LIST.getFullyQualifiedTypeName(), 0, DataType.TYPE, null, Arrays.asList(fieldType)), field.getFieldName(), fieldType, null, null, true);
fieldBuilder = new FieldMetadataBuilder(fieldDetails);
fieldBuilder.setModifier(field.getModifier());
fieldBuilder.setAnnotations(field.getAnnotations());
} else {
// Can't just copy FieldMetadata because field.declaredByMetadataId will be the same
fieldBuilder = new FieldMetadataBuilder(projectionBuilder.getDeclaredByMetadataId(), field);
}
// Add dependency between modules
typeLocationService.addModuleDependency(projectionBuilder.getName().getModule(), field.getFieldType());
// Set new fieldName
fieldBuilder.setFieldName(new JavaSymbolName(entry.getKey()));
// If it is a CollectionField it needs an initializer
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()";
}
fieldBuilder.setFieldInitializer(initializer);
// Remove persistence annotations
List<AnnotationMetadata> annotations = field.getAnnotations();
for (AnnotationMetadata annotation : annotations) {
if (annotation.getAnnotationType().getFullyQualifiedTypeName().contains("javax.persistence")) {
fieldBuilder.removeAnnotation(annotation.getAnnotationType());
} else if (annotation.getAnnotationType().getFullyQualifiedTypeName().startsWith("javax.validation")) {
// Add validation dependency
projectOperations.addDependency(projectionBuilder.getName().getModule(), new Dependency("javax.validation", "validation-api", null));
} else if (annotation.getAnnotationType().equals(RooJavaType.ROO_JPA_RELATION)) {
fieldBuilder.removeAnnotation(annotation.getAnnotationType());
}
}
fieldBuilder.setModifier(Modifier.PRIVATE);
// Build field
FieldMetadata projectionField = fieldBuilder.build();
// Add field to DTO
projectionBuilder.addField(projectionField);
}
}
use of org.springframework.roo.classpath.details.FieldMetadata in project spring-roo by spring-projects.
the class EntityProjectionMetadata 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();
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.details.FieldMetadata in project spring-roo by spring-projects.
the class DbreMetadata method hasField.
private boolean hasField(final JavaSymbolName fieldName, final List<AnnotationMetadata> annotations) {
// Check governor for field
if (governorTypeDetails.getField(fieldName) != null) {
return true;
}
// Check @Column and @JoinColumn annotations on fields in governor with
// the same 'name' as the generated field
final List<FieldMetadata> governorFields = governorTypeDetails.getFieldsWithAnnotation(COLUMN);
governorFields.addAll(governorTypeDetails.getFieldsWithAnnotation(JOIN_COLUMN));
for (final FieldMetadata governorField : governorFields) {
governorFieldAnnotations: for (final AnnotationMetadata governorFieldAnnotation : governorField.getAnnotations()) {
if (governorFieldAnnotation.getAnnotationType().equals(COLUMN) || governorFieldAnnotation.getAnnotationType().equals(JOIN_COLUMN)) {
final AnnotationAttributeValue<?> name = governorFieldAnnotation.getAttribute(new JavaSymbolName(NAME));
if (name == null) {
continue governorFieldAnnotations;
}
for (final AnnotationMetadata annotationMetadata : annotations) {
final AnnotationAttributeValue<?> columnName = annotationMetadata.getAttribute(new JavaSymbolName(NAME));
if (columnName != null && columnName.equals(name)) {
return true;
}
}
}
}
}
return false;
}
Aggregations