use of org.springframework.roo.classpath.details.MethodMetadataBuilder in project spring-roo by spring-projects.
the class JspOperationsImpl method getIndexMethod.
private MethodMetadataBuilder getIndexMethod(final String folderName, final String declaredByMetadataId) {
final List<AnnotationMetadataBuilder> indexMethodAnnotations = new ArrayList<AnnotationMetadataBuilder>();
indexMethodAnnotations.add(new AnnotationMetadataBuilder(REQUEST_MAPPING, new ArrayList<AnnotationAttributeValue<?>>()));
final InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
bodyBuilder.appendFormalLine("return \"" + folderName + "/index\";");
final MethodMetadataBuilder indexMethodBuilder = new MethodMetadataBuilder(declaredByMetadataId, Modifier.PUBLIC, new JavaSymbolName("index"), JavaType.STRING, new ArrayList<AnnotatedJavaType>(), new ArrayList<JavaSymbolName>(), bodyBuilder);
indexMethodBuilder.setAnnotations(indexMethodAnnotations);
return indexMethodBuilder;
}
use of org.springframework.roo.classpath.details.MethodMetadataBuilder in project spring-roo by spring-projects.
the class JspOperationsImpl method createManualController.
/**
* Creates a new Spring MVC controller.
* <p>
* Request mappings assigned by this method will always commence with "/"
* and end with "/**". You may present this prefix and/or this suffix if you
* wish, although it will automatically be added should it not be provided.
*
* @param controller the controller class to create (required)
* @param preferredMapping the mapping this controller should adopt
* (optional; if unspecified it will be based on the controller
* name)
*/
public void createManualController(final JavaType controller, final String preferredMapping, final LogicalPath webappPath) {
Validate.notNull(controller, "Controller Java Type required");
// Create annotation @RequestMapping("/myobject/**")
final ImmutablePair<String, String> folderAndMapping = getFolderAndMapping(preferredMapping, controller);
final String folderName = folderAndMapping.getKey();
final String resourceIdentifier = getPathResolver().getFocusedCanonicalPath(Path.SRC_MAIN_JAVA, controller);
final String declaredByMetadataId = PhysicalTypeIdentifier.createIdentifier(controller, getProjectOperations().getPathResolver().getPath(resourceIdentifier));
final List<MethodMetadataBuilder> methods = new ArrayList<MethodMetadataBuilder>();
// Add HTTP post method
methods.add(getHttpPostMethod(declaredByMetadataId));
// Add index method
methods.add(getIndexMethod(folderName, declaredByMetadataId));
// Create Type definition
final List<AnnotationMetadataBuilder> typeAnnotations = new ArrayList<AnnotationMetadataBuilder>();
final List<AnnotationAttributeValue<?>> requestMappingAttributes = new ArrayList<AnnotationAttributeValue<?>>();
requestMappingAttributes.add(new StringAttributeValue(new JavaSymbolName("value"), folderAndMapping.getValue()));
final AnnotationMetadataBuilder requestMapping = new AnnotationMetadataBuilder(REQUEST_MAPPING, requestMappingAttributes);
typeAnnotations.add(requestMapping);
// Create annotation @Controller
final List<AnnotationAttributeValue<?>> controllerAttributes = new ArrayList<AnnotationAttributeValue<?>>();
final AnnotationMetadataBuilder controllerAnnotation = new AnnotationMetadataBuilder(CONTROLLER, controllerAttributes);
typeAnnotations.add(controllerAnnotation);
final ClassOrInterfaceTypeDetailsBuilder cidBuilder = new ClassOrInterfaceTypeDetailsBuilder(declaredByMetadataId, Modifier.PUBLIC, controller, PhysicalTypeCategory.CLASS);
cidBuilder.setAnnotations(typeAnnotations);
cidBuilder.setDeclaredMethods(methods);
getTypeManagementService().createOrUpdateTypeOnDisk(cidBuilder.build());
installView(folderName, "/index", new JavaSymbolName(controller.getSimpleTypeName()).getReadableSymbolName() + " View", "Controller", null, false, webappPath);
}
use of org.springframework.roo.classpath.details.MethodMetadataBuilder in project spring-roo by spring-projects.
the class LinkFactoryMetadata method getToUriMethod.
/**
* Generates a `toUri` method which generates URI's for the *Collection*
* controller methods which are called from views.
*
* @param finderName
* @param serviceFinderMethod
* @return
*/
private MethodMetadata getToUriMethod() {
// Define methodName
final JavaSymbolName toUriMethodName = TO_URI_METHOD_NAME;
// Define method argument types
List<AnnotatedJavaType> parameterTypes = new ArrayList<AnnotatedJavaType>();
parameterTypes.add(stringArgument);
parameterTypes.add(objectArrayArgument);
parameterTypes.add(mapStringObjectArgument);
// Return method if already exists
MethodMetadata existingMethod = getGovernorMethod(toUriMethodName, AnnotatedJavaType.convertFromAnnotatedJavaTypes(parameterTypes));
if (existingMethod != null) {
return existingMethod;
}
// Define method argument names
final List<JavaSymbolName> parameterNames = new ArrayList<JavaSymbolName>();
parameterNames.add(METHOD_NAME_ARGUMENT_NAME);
parameterNames.add(PARAMETERS_ARGUMENT_NAME);
parameterNames.add(PATH_VARIABLES_ARGUMENT_NAME);
// Generate body
final InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
// Include a conditional for every method
for (MethodMetadata method : this.controllerMethods) {
// Getting methodName
String methodName = method.getMethodName().getSymbolName();
// Getting methodParams
List<String> methodParamsToNull = new ArrayList<String>();
for (AnnotatedJavaType paramType : method.getParameterTypes()) {
JavaType javaType = paramType.getJavaType();
if (javaType.isPrimitive() && !javaType.isArray()) {
if (JavaType.BOOLEAN_PRIMITIVE.equals(javaType)) {
methodParamsToNull.add("false");
} else if (JavaType.INT_PRIMITIVE.equals(javaType)) {
methodParamsToNull.add("0");
} else if (JavaType.LONG_PRIMITIVE.equals(javaType)) {
methodParamsToNull.add("0l");
} else if (JavaType.FLOAT_PRIMITIVE.equals(javaType)) {
methodParamsToNull.add("0.0f");
} else if (JavaType.DOUBLE_PRIMITIVE.equals(javaType)) {
methodParamsToNull.add("0.0");
}
} else {
// Include a null declaration for every parameter
methodParamsToNull.add("null");
}
}
// if (METHOD_NAME_ARGUMENT_NAME.equals(methodNameConstant)) {
bodyBuilder.appendFormalLine("if (%s.equals(%s)) {", METHOD_NAME_ARGUMENT_NAME, getConstantForMethodName(methodName).getFieldName());
bodyBuilder.indent();
// return MvcUriComponentsBuilder.fromMethodCall(MvcUriComponentsBuilder.on(getControllerClass()).list(null)).build();
bodyBuilder.appendFormalLine("return %1$s.fromMethodCall(%1$s.on(%2$s()).%3$s(%4$s)).buildAndExpand(%5$s);", getNameOfJavaType(SpringletsJavaType.SPRINGLETS_MVC_URI_COMPONENTS_BUILDER), this.getControllerClassMethod.getMethodName(), methodName, StringUtils.join(methodParamsToNull, ", "), PATH_VARIABLES_ARGUMENT_NAME);
bodyBuilder.indentRemove();
// }
bodyBuilder.appendFormalLine("}");
}
// throw new IllegalArgumentException("Invalid method name: " + METHOD_NAME_ARGUMENT_NAME);
bodyBuilder.appendFormalLine("throw new IllegalArgumentException(\"Invalid method name: \" + %s);", METHOD_NAME_ARGUMENT_NAME);
// Build method builder
MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(getId(), Modifier.PUBLIC, toUriMethodName, SpringJavaType.URI_COMPONENTS, parameterTypes, parameterNames, bodyBuilder);
return methodBuilder.build();
}
use of org.springframework.roo.classpath.details.MethodMetadataBuilder in project spring-roo by spring-projects.
the class LinkFactoryMetadata method getControllerClassMethod.
private MethodMetadata getControllerClassMethod() {
// Define methodName
final JavaSymbolName methodName = GET_CONTROLLER_CLASS_METHOD_NAME;
// Define method argument types
List<AnnotatedJavaType> parameterTypes = new ArrayList<AnnotatedJavaType>();
// Return method if already exists
MethodMetadata existingMethod = getGovernorMethod(methodName, AnnotatedJavaType.convertFromAnnotatedJavaTypes(parameterTypes));
if (existingMethod != null) {
return existingMethod;
}
InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
bodyBuilder.appendFormalLine("return %s.class;", getNameOfJavaType(this.controller));
// return CONTROLLER_CLASS;
MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(getId(), Modifier.PUBLIC, methodName, JavaType.wrapperOf(JavaType.CLASS, this.controller), null);
// Set method body
methodBuilder.setBodyBuilder(bodyBuilder);
return methodBuilder.build();
}
use of org.springframework.roo.classpath.details.MethodMetadataBuilder in project spring-roo by spring-projects.
the class ThymeleafMainControllerMetadata method getJavascriptTemplatesmethod.
/*
* =====================================================================================
*/
/**
* @return
*/
private MethodMetadata getJavascriptTemplatesmethod() {
// Define methodName
final JavaSymbolName methodName = new JavaSymbolName("javascriptTemplates");
List<AnnotatedJavaType> parameterTypes = new ArrayList<AnnotatedJavaType>();
// Create @PathVariable("templeate") String template parameter
AnnotationMetadataBuilder pathVarialbeAnnotation = new AnnotationMetadataBuilder(SpringJavaType.PATH_VARIABLE);
pathVarialbeAnnotation.addStringAttribute("value", "template");
AnnotatedJavaType templateParameter = new AnnotatedJavaType(JavaType.STRING, pathVarialbeAnnotation.build());
parameterTypes.add(templateParameter);
final List<JavaSymbolName> parameterNames = new ArrayList<JavaSymbolName>();
parameterNames.add(new JavaSymbolName("template"));
MethodMetadata existingMethod = getGovernorMethod(methodName, AnnotatedJavaType.convertFromAnnotatedJavaTypes(parameterTypes));
if (existingMethod != null) {
return existingMethod;
}
// Adding annotations
final List<AnnotationMetadataBuilder> annotations = new ArrayList<AnnotationMetadataBuilder>();
// Adding @RequestMapping annotation
AnnotationMetadataBuilder requestMapping = new AnnotationMetadataBuilder(SpringJavaType.REQUEST_MAPPING);
requestMapping.addStringAttribute("value", "/js/{template}.js");
getNameOfJavaType(SpringJavaType.REQUEST_METHOD);
requestMapping.addEnumAttribute("method", SpringJavaType.REQUEST_METHOD, "GET");
annotations.add(requestMapping);
// Generate body
InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
// if (StringUtils.hasLength(template)) {
// return template.concat(".js");
// }
bodyBuilder.appendFormalLine("if (%s.hasLength(template)) {", getNameOfJavaType(SpringJavaType.STRING_UTILS));
bodyBuilder.indent();
bodyBuilder.appendFormalLine("return template.concat(\".js\");");
bodyBuilder.indentRemove();
bodyBuilder.appendFormalLine("}");
// throw new NotFoundException("File not found")
bodyBuilder.appendFormalLine("throw new %s(\"File not found\");", getNameOfJavaType(SpringletsJavaType.SPRINGLETS_NOT_FOUND_EXCEPTION));
MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(getId(), Modifier.PUBLIC, methodName, JavaType.STRING, parameterTypes, parameterNames, bodyBuilder);
methodBuilder.setAnnotations(annotations);
return methodBuilder.build();
}
Aggregations