use of org.springframework.roo.classpath.details.comments.CommentStructure in project spring-roo by spring-projects.
the class ThymeleafMetadata method getAddColumnToReportBuilderMethod.
/* Jasper Export Methods */
/**
* Generates a method to add columns to DynamicJasper report builder.
*
* @return MethodMetadata
*/
private MethodMetadata getAddColumnToReportBuilderMethod() {
JavaSymbolName methodName = ADD_COLUMN_TO_REPORT_BUILDER_METHOD_NAME;
// Including parameter types
List<AnnotatedJavaType> parameterTypes = new ArrayList<AnnotatedJavaType>();
parameterTypes.add(STRING_PARAM);
parameterTypes.add(new AnnotatedJavaType(FAST_REPORT_BUILDER));
parameterTypes.add(LOCALE_PARAM);
parameterTypes.add(STRING_PARAM);
// Check method existence
MethodMetadata existingMethod = getGovernorMethod(methodName, AnnotatedJavaType.convertFromAnnotatedJavaTypes(parameterTypes));
if (existingMethod != null) {
return existingMethod;
}
// Including parameter names
final List<JavaSymbolName> parameterNames = new ArrayList<JavaSymbolName>();
final JavaSymbolName columnName = new JavaSymbolName("columnName");
parameterNames.add(columnName);
final JavaSymbolName reportBuilder = new JavaSymbolName("builder");
parameterNames.add(reportBuilder);
parameterNames.add(LOCALE_PARAM_NAME);
parameterNames.add(FILE_NAME_PARAM_NAME);
InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
// try {
bodyBuilder.appendFormalLine("try {");
for (int i = 0; i < this.entityValidFields.size(); i++) {
FieldMetadata fieldMetadata = this.entityValidFields.get(i);
String fieldName = fieldMetadata.getFieldName().getSymbolName();
if (i == 0) {
// if (columnName.equals("FIELD")) {
bodyBuilder.appendFormalLine("if (columnName.equals(\"%s\")) {", fieldName);
} else {
// else if (columnName.equals("FIELD")) {
bodyBuilder.appendFormalLine("else if (columnName.equals(\"%s\")) {", fieldName);
}
bodyBuilder.indent();
JavaType fieldType = fieldMetadata.getFieldType();
if (fieldMetadata.getFieldName().equals(this.entityMetadata.getCurrentIndentifierField().getFieldName())) {
// 50);
if (fieldType.isPrimitive()) {
// Print SimpleTypeName of JavaType when it is a primitive
bodyBuilder.appendFormalLine("builder.addColumn(%s().getMessage(\"%s_%s\", null, \"%s\", locale), \"%s\", %s.class.getName(), 50);", getAccessorMethod(this.messageSourceField).getMethodName(), this.entityLabel, fieldName.toLowerCase(), getFieldDefaultLabelValue(fieldName), fieldName, fieldType.getSimpleTypeName());
// assure import of fielType is in aj
getNameOfJavaType(fieldType);
} else {
bodyBuilder.appendFormalLine("builder.addColumn(%s().getMessage(\"%s_%s\", null, \"%s\", locale), \"%s\", %s.class.getName(), 50);", getAccessorMethod(this.messageSourceField).getMethodName(), this.entityLabel, fieldName.toLowerCase(), getFieldDefaultLabelValue(fieldName), fieldName, getNameOfJavaType(fieldType.withoutParameters()));
}
} else {
// 100);
if (fieldType.isPrimitive()) {
bodyBuilder.appendFormalLine("builder.addColumn(%s().getMessage(\"%s_%s\", null, \"%s\", locale), \"%s\", %s.class.getName(), 100);", getAccessorMethod(this.messageSourceField).getMethodName(), this.entityLabel, fieldName.toLowerCase(), getFieldDefaultLabelValue(fieldName), fieldName, fieldType.getSimpleTypeName());
// assure import of fielType is in aj
getNameOfJavaType(fieldType);
} else {
bodyBuilder.appendFormalLine("builder.addColumn(%s().getMessage(\"%s_%s\", null, \"%s\", locale), \"%s\", %s.class.getName(), 100);", getAccessorMethod(this.messageSourceField).getMethodName(), this.entityLabel, fieldName.toLowerCase(), getFieldDefaultLabelValue(fieldName), fieldName, getNameOfJavaType(fieldType.withoutParameters()));
}
}
bodyBuilder.indentRemove();
bodyBuilder.appendFormalLine("}");
}
// }
bodyBuilder.appendFormalLine("}");
// Build catch blocks
buildExportCatchBlock(bodyBuilder, COLUMN_BUILDER_EXCEPTION);
buildExportCatchBlock(bodyBuilder, CLASS_NOT_FOUND_EXCEPTION);
// Build method
MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(getId(), Modifier.PUBLIC, methodName, JavaType.VOID_PRIMITIVE, parameterTypes, parameterNames, bodyBuilder);
// Add Javadoc to method
CommentStructure commentStructure = new CommentStructure();
String description = "This method contains all the entity fields that are able to be displayed in a ".concat(IOUtils.LINE_SEPARATOR).concat("report. The developer could add a new column to the report builder providing the ").concat(IOUtils.LINE_SEPARATOR).concat("field name and the builder where the new field will be added as column.");
List<String> paramInfo = new ArrayList<String>();
paramInfo.add("columnName the field name to show as column");
paramInfo.add("builder The builder where the new field will be added as column.");
commentStructure.addComment(new JavadocComment(description, paramInfo, null, null), CommentLocation.BEGINNING);
methodBuilder.setCommentStructure(commentStructure);
return methodBuilder.build();
}
use of org.springframework.roo.classpath.details.comments.CommentStructure in project spring-roo by spring-projects.
the class ThymeleafMetadata method buildExportTypeMethod.
/**
* Builds export (TYPE) method, which is similar in all export target types.
*
* @param exporterClassInstantiation
* the String with the instantiation of the JasperReports support
* class.
* @param fileName
* the String with the output file name.
* @param methodName
* the JavaSymbolName with the method name.
* @return MethodMetadata
*/
private MethodMetadata buildExportTypeMethod(final String exporterClassInstantiation, final String fileName, final JavaSymbolName methodName, final String getMappingAnnotatinName, final String getMappingAnnotationValue, final String fileType) {
// Including parameter types
List<AnnotatedJavaType> parameterTypes = new ArrayList<AnnotatedJavaType>();
parameterTypes.add(GLOBAL_SEARCH_PARAM);
AnnotationMetadataBuilder pageableDefaultAnnotation = new AnnotationMetadataBuilder(SpringJavaType.PAGEABLE_DEFAULT);
pageableDefaultAnnotation.addIntegerAttribute("size", Integer.MAX_VALUE);
parameterTypes.add(new AnnotatedJavaType(SpringJavaType.PAGEABLE, pageableDefaultAnnotation.build()));
AnnotationMetadataBuilder requestParamAnnotation = new AnnotationMetadataBuilder(SpringJavaType.REQUEST_PARAM);
requestParamAnnotation.addStringAttribute("value", DATATABLES_COLUMNS_PARAM_NAME.getSymbolName());
parameterTypes.add(new AnnotatedJavaType(JavaType.STRING_ARRAY, requestParamAnnotation.build()));
parameterTypes.add(new AnnotatedJavaType(new JavaType("javax.servlet.http.HttpServletResponse")));
parameterTypes.add(LOCALE_PARAM);
MethodMetadata existingMethod = getGovernorMethod(methodName, AnnotatedJavaType.convertFromAnnotatedJavaTypes(parameterTypes));
if (existingMethod != null) {
return existingMethod;
}
// Including parameter names
final List<JavaSymbolName> parameterNames = new ArrayList<JavaSymbolName>();
parameterNames.add(GLOBAL_SEARCH_PARAM_NAME);
parameterNames.add(PAGEABLE_PARAM_NAME);
parameterNames.add(DATATABLES_COLUMNS_PARAM_NAME);
parameterNames.add(RESPONSE_PARAM_NAME);
parameterNames.add(LOCALE_PARAM_NAME);
// Adding annotations
final List<AnnotationMetadataBuilder> annotations = new ArrayList<AnnotationMetadataBuilder>();
AnnotationMetadataBuilder getMappingBuilder = new AnnotationMetadataBuilder(GET_MAPPING);
getMappingBuilder.addStringAttribute("name", getMappingAnnotatinName);
getMappingBuilder.addStringAttribute("value", getMappingAnnotationValue);
annotations.add(getMappingBuilder);
annotations.add(RESPONSE_BODY_ANNOTATION);
// Add throws types
final List<JavaType> throwTypes = new ArrayList<JavaType>();
throwTypes.add(JR_EXCEPTION);
throwTypes.add(IO_EXCEPTION);
throwTypes.add(COLUMN_BUILDER_EXCEPTION);
throwTypes.add(CLASS_NOT_FOUND_EXCEPTION);
InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
// export(search, pageable, datatablesColumns, response, new
// JasperReportsCsvExporter(), "ENTITY-ITEM_report.csv");
bodyBuilder.appendFormalLine("export(%s, %s, %s, %s, %s, \"%s\", %s);", GLOBAL_SEARCH_PARAM_NAME, PAGEABLE_PARAM_NAME, DATATABLES_COLUMNS_PARAM_NAME, RESPONSE_PARAM_NAME, exporterClassInstantiation, fileName, LOCALE_PARAM_NAME.getSymbolName());
// return ResponseEntity.ok().build();
bodyBuilder.appendFormalLine("return %s.ok().build();", getNameOfJavaType(RESPONSE_ENTITY));
// Build method
MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(getId(), Modifier.PUBLIC, methodName, JavaType.wrapperWilcard(RESPONSE_ENTITY), parameterTypes, parameterNames, bodyBuilder);
methodBuilder.setAnnotations(annotations);
// Add JavaDoc
CommentStructure commentStructure = new CommentStructure();
String description = "It delegates in the `export` method providing the necessary information".concat(IOUtils.LINE_SEPARATOR).concat(String.format("to generate a %s report.", fileType));
// Add params info to commment block
List<String> paramsInfo = new ArrayList<String>();
paramsInfo.add("search The GlobalSearch that contains the filter provided by the Datatables component");
paramsInfo.add("pageable The Pageable that contains the Sort info provided by the Datatabes component");
paramsInfo.add("datatablesColumns The Columns displayed in the Datatables component");
paramsInfo.add("response The HttpServletResponse");
// Add JavadocComment to CommentStructure and to method
commentStructure.addComment(new JavadocComment(description, paramsInfo, null, null), CommentLocation.BEGINNING);
methodBuilder.setCommentStructure(commentStructure);
return methodBuilder.build();
}
use of org.springframework.roo.classpath.details.comments.CommentStructure in project spring-roo by spring-projects.
the class ThymeleafControllerIntegrationTestMetadata method getTestExampleMethod.
/**
* Builds and return a test example method.
*
* @return {@link MethodMetadata}
*/
private MethodMetadata getTestExampleMethod() {
JavaSymbolName methodName = new JavaSymbolName("testMethodExample");
// Check if method exists in governor
MethodMetadata method = getGovernorMethod(methodName);
if (method != null) {
return method;
}
// Build method body
InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
// Setup
bodyBuilder.appendFormalLine("// Setup");
bodyBuilder.appendFormalLine("// Previous tasks");
bodyBuilder.newLine();
// Exercise
bodyBuilder.appendFormalLine("// Exercise");
bodyBuilder.appendFormalLine("// Execute method to test");
bodyBuilder.newLine();
// Verify
bodyBuilder.appendFormalLine("// Verify");
bodyBuilder.appendFormalLine("// Check results with assertions");
MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(this.getId(), Modifier.PUBLIC, methodName, JavaType.VOID_PRIMITIVE, bodyBuilder);
// Add @Test
methodBuilder.addAnnotation(new AnnotationMetadataBuilder(TEST));
CommentStructure commentStructure = new CommentStructure();
commentStructure.addComment(new JavadocComment("Test method example. To be implemented by developer."), CommentLocation.BEGINNING);
methodBuilder.setCommentStructure(commentStructure);
return methodBuilder.build();
}
use of org.springframework.roo.classpath.details.comments.CommentStructure in project spring-roo by spring-projects.
the class JpaEntityFactoryMetadata method getCreateMethod.
private MethodMetadata getCreateMethod() {
// Define methodName
final JavaSymbolName methodName = CREATE_FACTORY_METHOD_NAME;
List<JavaType> parameterTypes = new ArrayList<JavaType>();
parameterTypes.add(JavaType.INT_PRIMITIVE);
// Check if method exists
MethodMetadata existingMethod = getGovernorMethod(methodName, parameterTypes);
if (existingMethod != null) {
return existingMethod;
}
List<JavaSymbolName> parameterNames = new ArrayList<JavaSymbolName>();
parameterNames.add(INDEX_SYMBOL);
// Add body
InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
// Entity obj = new Entity();
bodyBuilder.appendFormalLine("%1$s %2$s = new %1$s();", getNameOfJavaType(this.entity), OBJ_SYMBOL);
// Set values for transient object
for (final Map.Entry<FieldMetadata, JpaEntityFactoryMetadata> entry : locatedFields.entrySet()) {
bodyBuilder.appendFormalLine("%s(%s, %s);", BeanInfoUtils.getMutatorMethodName(entry.getKey()), OBJ_SYMBOL, INDEX_SYMBOL);
}
// return obj;
bodyBuilder.appendFormalLine("return %s;", OBJ_SYMBOL);
// Create method
MethodMetadataBuilder method = new MethodMetadataBuilder(this.getId(), Modifier.PUBLIC, methodName, this.entity, AnnotatedJavaType.convertFromJavaTypes(parameterTypes), parameterNames, bodyBuilder);
CommentStructure commentStructure = new CommentStructure();
List<String> paramsInfo = new ArrayList<String>();
paramsInfo.add(String.format("%s position of the %s", INDEX_VAR, this.entity.getSimpleTypeName()));
JavadocComment comment = new JavadocComment(String.format("Creates a new {@link %s} with the given %s.", this.entity.getSimpleTypeName(), INDEX_VAR), paramsInfo, String.format("a new transient %s", this.entity.getSimpleTypeName()), null);
commentStructure.addComment(comment, CommentLocation.BEGINNING);
method.setCommentStructure(commentStructure);
return method.build();
}
use of org.springframework.roo.classpath.details.comments.CommentStructure in project spring-roo by spring-projects.
the class ItdSourceFileComposer method writeMethods.
private void writeMethods(final List<? extends MethodMetadata> methods, final boolean defineTarget, final boolean isInterfaceMethod) {
for (final MethodMetadata method : methods) {
Validate.isTrue(method.getParameterTypes().size() == method.getParameterNames().size(), "Method %s has mismatched parameter names against parameter types", method.getMethodName().getSymbolName());
// ROO-3447: Append comments if exists
CommentStructure commentStructure = method.getCommentStructure();
if (commentStructure != null && commentStructure.getBeginComments() != null) {
List<AbstractComment> comments = commentStructure.getBeginComments();
String commentString = "";
boolean missingComponentsAdded = false;
for (AbstractComment comment : comments) {
// Join all JavadocComment's
if (comment instanceof JavadocComment) {
// Add JavaDoc missing components
if (!missingComponentsAdded) {
// Check params info
if (!method.getParameterNames().isEmpty() && ((JavadocComment) comment).getParamsInfo() == null) {
List<String> paramsInfo = new ArrayList<String>();
for (JavaSymbolName name : method.getParameterNames()) {
paramsInfo.add(name.getSymbolName());
}
((JavadocComment) comment).setParamsInfo(paramsInfo);
}
// Check return info
if (!method.getReturnType().equals(JavaType.VOID_OBJECT) && !method.getReturnType().equals(JavaType.VOID_PRIMITIVE) && ((JavadocComment) comment).getReturnInfo() == null) {
((JavadocComment) comment).setReturnInfo(method.getReturnType().getSimpleTypeName());
}
// Check throws info
if (!method.getThrowsTypes().isEmpty() && ((JavadocComment) comment).getThrowsInfo() == null) {
List<String> throwsInfo = new ArrayList<String>();
for (JavaType throwsType : method.getThrowsTypes()) {
throwsInfo.add(throwsType.getSimpleTypeName());
}
((JavadocComment) comment).setThrowsInfo(throwsInfo);
}
missingComponentsAdded = true;
}
commentString = commentString.concat(comment.getComment()).concat(IOUtils.LINE_SEPARATOR);
} else {
// Not JavadocComment, write comment as it is, unchanged
appendFormalLine(comment.getComment());
}
}
// Write JavadocComment's to ITD, in a single JavadocComment instance
String[] commentLines = commentString.split(IOUtils.LINE_SEPARATOR);
for (String commentLine : commentLines) {
appendFormalLine(commentLine);
}
} else {
// ROO-3834: Append default Javadoc if not exists a comment structure,
// including params, return and throws
CommentStructure defaultCommentStructure = new CommentStructure();
List<String> parameterNames = new ArrayList<String>();
for (JavaSymbolName name : method.getParameterNames()) {
parameterNames.add(name.getSymbolName());
}
List<String> throwsTypesNames = new ArrayList<String>();
for (JavaType type : method.getThrowsTypes()) {
throwsTypesNames.add(type.getSimpleTypeName());
}
String returnInfo = null;
JavaType returnType = method.getReturnType();
if (!returnType.equals(JavaType.VOID_OBJECT) && !returnType.equals(JavaType.VOID_PRIMITIVE)) {
returnInfo = returnType.getSimpleTypeName();
}
JavadocComment javadocComment = new JavadocComment("TODO Auto-generated method documentation", parameterNames, returnInfo, throwsTypesNames);
defaultCommentStructure.addComment(javadocComment, CommentLocation.BEGINNING);
method.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 : method.getAnnotations()) {
appendIndent();
outputAnnotation(annotation);
this.newLine(false);
}
// Append "<modifier> <genericDefinition> <returnType> <methodName>" portion
appendIndent();
// modifier
if (method.getModifier() != 0) {
append(Modifier.toString(method.getModifier()));
append(" ");
}
// ROO-3648: genericDefinition
if (method.getGenericDefinition() != null && !method.getGenericDefinition().trim().equals("")) {
append("<".concat(method.getGenericDefinition()).concat(">"));
append(" ");
}
// return type
append(method.getReturnType().getNameIncludingTypeParameters(false, resolver));
append(" ");
if (defineTarget) {
append(introductionTo.getSimpleTypeName());
append(".");
}
append(method.getMethodName().getSymbolName());
// Append parameter types and names
append("(");
final List<AnnotatedJavaType> parameterTypes = method.getParameterTypes();
final List<JavaSymbolName> parameterNames = method.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()) {
outputAnnotation(methodParameterAnnotation);
append(" ");
}
append(paramType.getJavaType().getNameIncludingTypeParameters(false, resolver));
append(" ");
append(paramName.getSymbolName());
if (i < parameterTypes.size() - 1) {
append(", ");
}
}
// Add exceptions to be thrown
final List<JavaType> throwsTypes = method.getThrowsTypes();
if (throwsTypes.size() > 0) {
append(") throws ");
for (int i = 0; i < throwsTypes.size(); i++) {
append(throwsTypes.get(i).getNameIncludingTypeParameters(false, resolver));
if (throwsTypes.size() > i + 1) {
append(", ");
}
}
} else {
append(")");
}
if (isInterfaceMethod) {
append(";");
this.newLine(false);
} else {
append(" {");
this.newLine(false);
// Add body
indent();
append(method.getBody());
indentRemove();
appendFormalLine("}");
}
this.newLine();
}
}
Aggregations