use of org.springframework.roo.classpath.details.ConstructorMetadata in project spring-roo by spring-projects.
the class ItdSourceFileComposer method appendConstructors.
private void appendConstructors() {
final List<? extends ConstructorMetadata> constructors = itdTypeDetails.getDeclaredConstructors();
if (constructors == null || constructors.isEmpty()) {
return;
}
content = true;
for (final ConstructorMetadata constructor : constructors) {
Validate.isTrue(constructor.getParameterTypes().size() == constructor.getParameterNames().size(), "Mismatched parameter names against parameter types");
// ROO-3447: Append comments if exists
CommentStructure commentStructure = constructor.getCommentStructure();
if (commentStructure != null && commentStructure.getBeginComments() != null) {
List<AbstractComment> constructorComments = commentStructure.getBeginComments();
String comment = "";
boolean missingComponentsAdded = false;
for (AbstractComment constructorComment : constructorComments) {
// Join all JavadocComment's
if (constructorComment instanceof JavadocComment) {
// Add JavaDoc missing components
if (!missingComponentsAdded) {
if (!constructor.getParameterNames().isEmpty() && ((JavadocComment) constructorComment).getParamsInfo() == null) {
List<String> paramsInfo = new ArrayList<String>();
for (JavaSymbolName name : constructor.getParameterNames()) {
paramsInfo.add(name.getSymbolName());
}
((JavadocComment) constructorComment).setParamsInfo(paramsInfo);
}
}
missingComponentsAdded = true;
comment = comment.concat(constructorComment.getComment()).concat(IOUtils.LINE_SEPARATOR);
} else {
// Not JavadocComment, write comment as it is, unchanged
appendFormalLine(constructorComment.getComment());
}
}
// Write JavadocComment's to ITD, in a single JavadocComment instance
String[] commentLines = comment.split(IOUtils.LINE_SEPARATOR);
for (String commentLine : commentLines) {
appendFormalLine(commentLine);
}
} else {
// ROO-3834: Append default Javadoc if not exists a comment structure,
// including constructor params
CommentStructure defaultCommentStructure = new CommentStructure();
List<String> parameterNames = new ArrayList<String>();
for (JavaSymbolName name : constructor.getParameterNames()) {
parameterNames.add(name.getSymbolName());
}
JavadocComment javadocComment = new JavadocComment("TODO Auto-generated constructor documentation", parameterNames, null, null);
defaultCommentStructure.addComment(javadocComment, CommentLocation.BEGINNING);
constructor.setCommentStructure(defaultCommentStructure);
// Now lines should be formatted, so write them
String[] comment = javadocComment.getComment().split(IOUtils.LINE_SEPARATOR);
for (String line : comment) {
appendFormalLine(line);
}
}
// Append annotations
for (final AnnotationMetadata annotation : constructor.getAnnotations()) {
appendIndent();
outputAnnotation(annotation);
this.newLine(false);
}
// Append "<modifier> <TargetOfIntroduction>.new" portion
appendIndent();
if (constructor.getModifier() != 0) {
append(Modifier.toString(constructor.getModifier()));
append(" ");
}
append(introductionTo.getSimpleTypeName());
append(".");
append("new");
// Append parameter types and names
append("(");
final List<AnnotatedJavaType> parameterTypes = constructor.getParameterTypes();
final List<JavaSymbolName> parameterNames = constructor.getParameterNames();
for (int i = 0; i < parameterTypes.size(); i++) {
final AnnotatedJavaType paramType = parameterTypes.get(i);
final JavaSymbolName paramName = parameterNames.get(i);
for (final AnnotationMetadata methodParameterAnnotation : paramType.getAnnotations()) {
append(AnnotationMetadataUtils.toSourceForm(methodParameterAnnotation, resolver));
append(" ");
}
append(paramType.getJavaType().getNameIncludingTypeParameters(false, resolver));
append(" ");
append(paramName.getSymbolName());
if (i < parameterTypes.size() - 1) {
append(", ");
}
}
append(") {");
this.newLine(false);
indent();
// Add body
append(constructor.getBody());
indentRemove();
appendFormalLine("}");
this.newLine(false);
}
}
use of org.springframework.roo.classpath.details.ConstructorMetadata 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());
}
Aggregations