use of org.springframework.roo.classpath.details.ConstructorMetadataBuilder in project spring-roo by spring-projects.
the class IdentifierMetadata method getNoArgConstructor.
/**
* Locates the no-arg constructor for this class, if available.
* <p>
* If a class defines a no-arg constructor, it is returned (irrespective of
* access modifiers).
* <p>
* If a class does not define a no-arg constructor, one might be created. It
* will only be created if the {@link RooIdentifier#noArgConstructor} is
* true AND there is at least one other constructor declared in the source
* file. If a constructor is created, it will have a private access
* modifier.
*
* @return the constructor (may return null if no constructor is to be
* produced)
*/
private ConstructorMetadataBuilder getNoArgConstructor() {
// Search for an existing constructor
final List<JavaType> parameterTypes = new ArrayList<JavaType>();
final ConstructorMetadata result = governorTypeDetails.getDeclaredConstructor(parameterTypes);
if (result != null) {
// Found an existing no-arg constructor on this class
return null;
}
// Create the constructor
final InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
bodyBuilder.appendFormalLine("super();");
final ConstructorMetadataBuilder constructorBuilder = new ConstructorMetadataBuilder(getId());
constructorBuilder.setModifier(publicNoArgConstructor ? Modifier.PUBLIC : Modifier.PRIVATE);
constructorBuilder.setParameterTypes(AnnotatedJavaType.convertFromJavaTypes(parameterTypes));
constructorBuilder.setBodyBuilder(bodyBuilder);
return constructorBuilder;
}
use of org.springframework.roo.classpath.details.ConstructorMetadataBuilder in project spring-roo by spring-projects.
the class EntityDeserializerMetadata method getConstructor.
public static ConstructorMetadata getConstructor(String declaredByMetadataId, FieldMetadata serviceField, FieldMetadata conversionServiceField) {
InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
// Generating constructor
ConstructorMetadataBuilder constructor = new ConstructorMetadataBuilder(declaredByMetadataId);
constructor.setModifier(Modifier.PUBLIC);
constructor.addAnnotation(new AnnotationMetadataBuilder(SpringJavaType.AUTOWIRED));
// add Service to constructor
String serviceFieldName = serviceField.getFieldName().getSymbolName();
AnnotatedJavaType serviceParameter = new AnnotatedJavaType(serviceField.getFieldType(), new AnnotationMetadataBuilder(SpringJavaType.LAZY).build());
constructor.addParameterName(serviceField.getFieldName());
constructor.addParameterType(serviceParameter);
// Generating body
bodyBuilder.appendFormalLine("this.%1$s = %1$s;", serviceFieldName);
// add Conversion service to constructor
String conversionServiceFieldName = conversionServiceField.getFieldName().getSymbolName();
constructor.addParameter(conversionServiceFieldName, conversionServiceField.getFieldType());
// Generating body
bodyBuilder.appendFormalLine("this.%1$s = %1$s;", conversionServiceFieldName);
// Adding body
constructor.setBodyBuilder(bodyBuilder);
return constructor.build();
}
use of org.springframework.roo.classpath.details.ConstructorMetadataBuilder 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.ConstructorMetadataBuilder 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.ConstructorMetadataBuilder in project spring-roo by spring-projects.
the class ServiceImplMetadata method getConstructor.
/**
* Method that generates Service implementation constructor. If exists a
* repository, it will be included as constructor parameter
*
* @param autowired add or not autowired annotation
*
* @return
*/
private ConstructorMetadataBuilder getConstructor(boolean autowired) {
ConstructorMetadataBuilder constructorBuilder = new ConstructorMetadataBuilder(getId());
InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
// Append repository parameter if needed
if (repositoryFieldMetadata != null) {
addFieldToConstructor(bodyBuilder, constructorBuilder, repositoryFieldMetadata, false);
}
for (FieldMetadata requiredService : requiredServiceFieldByEntity.values()) {
addFieldToConstructor(bodyBuilder, constructorBuilder, requiredService, true);
}
constructorBuilder.setBodyBuilder(bodyBuilder);
constructorBuilder.setModifier(Modifier.PUBLIC);
if (autowired) {
// Adding @Autowired annotation
constructorBuilder.addAnnotation(new AnnotationMetadataBuilder(SpringJavaType.AUTOWIRED));
}
return constructorBuilder;
}
Aggregations