use of org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder in project spring-roo by spring-projects.
the class WsEndpointsMetadata method getBusField.
/**
* This method obtains the bus field that will be used in some different methods
*
* @return FieldMetadata that contains all the necessary information
* about the bus field
*/
private FieldMetadata getBusField() {
// Check if already exists
if (this.busField != null) {
return this.busField;
}
// Create the field
FieldMetadataBuilder bus = new FieldMetadataBuilder(getId(), Modifier.PRIVATE, new JavaSymbolName("bus"), new JavaType("org.apache.cxf.Bus"), null);
bus.addAnnotation(new AnnotationMetadataBuilder(SpringJavaType.AUTOWIRED));
busField = bus.build();
return busField;
}
use of org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder in project spring-roo by spring-projects.
the class UpdateCompilationUnitTest method testSimpleClass3AddField.
@Test
public void testSimpleClass3AddField() throws Exception {
// Set up
final File file = getResource(SIMPLE_CLASS3_FILE_PATH);
final String fileContents = getResourceContents(file);
final ClassOrInterfaceTypeDetails simpleInterfaceDetails = typeParsingService.getTypeFromString(fileContents, SIMPLE_CLASS3_DECLARED_BY_MID, SIMPLE_CLASS3_TYPE);
final SetField fieldDetails = new SetField(SIMPLE_CLASS3_DECLARED_BY_MID, new JavaType(SET.getFullyQualifiedTypeName(), 0, DataType.TYPE, null, Arrays.asList(SIMPLE_CLASS3_TYPE)), new JavaSymbolName("children"), SIMPLE_CLASS3_TYPE, Cardinality.ONE_TO_MANY, new Cascade[] { Cascade.REMOVE }, false);
final FieldMetadataBuilder fieldBuilder = new FieldMetadataBuilder(fieldDetails.getPhysicalTypeIdentifier(), Modifier.PRIVATE, new ArrayList<AnnotationMetadataBuilder>(), fieldDetails.getFieldName(), fieldDetails.getFieldType());
fieldBuilder.setFieldInitializer("new HashSet<SimpleClass3>()");
final ClassOrInterfaceTypeDetails newClassDetails = addField(simpleInterfaceDetails, fieldBuilder.build());
// Invoke
final String result = typeParsingService.getCompilationUnitContents(newClassDetails);
saveResult(file, result, "-addField");
checkSimple3Class(result);
assertTrue(result.contains("private Set<SimpleClass3> children = new HashSet<SimpleClass3>();"));
// Add another
final ClassOrInterfaceTypeDetails simpleInterfaceDetails2 = typeParsingService.getTypeFromString(result, SIMPLE_CLASS3_DECLARED_BY_MID, SIMPLE_CLASS3_TYPE);
final ReferenceField fieldDetails2 = new ReferenceField(SIMPLE_CLASS3_DECLARED_BY_MID, SIMPLE_CLASS2_TYPE, new JavaSymbolName("referenceField"), Cardinality.MANY_TO_ONE, new Cascade[] { Cascade.REFRESH });
final FieldMetadataBuilder fieldBuilder2 = new FieldMetadataBuilder(fieldDetails2.getPhysicalTypeIdentifier(), Modifier.PRIVATE, new ArrayList<AnnotationMetadataBuilder>(), fieldDetails2.getFieldName(), fieldDetails2.getFieldType());
final ClassOrInterfaceTypeDetails newClassDetails2 = addField(simpleInterfaceDetails2, fieldBuilder2.build());
// Invoke
final String result2 = typeParsingService.getCompilationUnitContents(newClassDetails2);
// Save to file for debug
saveResult(file, result2, "-addField2");
checkSimple3Class(result2);
assertTrue(result.contains("private Set<SimpleClass3> children = new HashSet<SimpleClass3>();"));
assertTrue(result2.contains("private SimpleClass2 referenceField;"));
}
use of org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder 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.annotations.AnnotationMetadataBuilder 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.annotations.AnnotationMetadataBuilder 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