use of org.springframework.roo.classpath.details.FieldMetadataBuilder in project spring-roo by spring-projects.
the class IdentifierMetadata method getAccessors.
/**
* Locates the accessor methods.
* <p>
* If {@link #getFieldBuilders()} returns fields created by this ITD, public
* accessors will automatically be produced in the declaring class.
*
* @param fields
* @return the accessors (never returns null)
*/
private List<MethodMetadataBuilder> getAccessors(final List<FieldMetadataBuilder> fields) {
final List<MethodMetadataBuilder> accessors = new ArrayList<MethodMetadataBuilder>();
// Compute the names of the accessors that will be produced
for (final FieldMetadataBuilder field : fields) {
final JavaSymbolName requiredAccessorName = BeanInfoUtils.getAccessorMethodName(field.getFieldName(), field.getFieldType());
final MethodMetadata accessor = getGovernorMethod(requiredAccessorName);
if (accessor == null) {
accessors.add(getAccessorMethod(field.getFieldName(), field.getFieldType()));
} else {
Validate.isTrue(Modifier.isPublic(accessor.getModifier()), "User provided field but failed to provide a public '%s()' method in '%s'", requiredAccessorName.getSymbolName(), destination.getFullyQualifiedTypeName());
accessors.add(new MethodMetadataBuilder(accessor));
}
}
return accessors;
}
use of org.springframework.roo.classpath.details.FieldMetadataBuilder in project spring-roo by spring-projects.
the class JmsOperationsImpl method addJmsSender.
@Override
public void addJmsSender(String destinationName, JavaType classSelected, String jndiConnectionFactory, String profile, boolean force) {
// Check that module included in destionationName is an application module
String module = "";
String destination = "";
if (getProjectOperations().isMultimoduleProject()) {
Collection<String> moduleNames = getTypeLocationService().getModuleNames(ModuleFeatureName.APPLICATION);
// if only have one module, select this, else check the parameter
if (moduleNames.size() > 1) {
// user select a module
if (destinationName.contains(":")) {
int charSeparation = destinationName.indexOf(":");
if (charSeparation > 0 && destinationName.length() > charSeparation) {
module = destinationName.substring(0, charSeparation);
destination = destinationName.substring(charSeparation + 1, destinationName.length());
if (!moduleNames.contains(module)) {
// error, is necessary select an application module
throw new IllegalArgumentException(String.format("Module '%s' must be of application type. Select one in --destinationName parameter", module));
}
} else {
// error, is necessary select an application module and destination
throw new IllegalArgumentException(String.format("--destinationName parameter must be composed by [application type module]:[destination] or focus module must be application type module and only write the destination name"));
}
} else {
// module not selected, check if focus module is application type
Pom focusedModule = getProjectOperations().getFocusedModule();
if (getTypeLocationService().hasModuleFeature(focusedModule, ModuleFeatureName.APPLICATION)) {
module = focusedModule.getModuleName();
destination = destinationName;
} else {
throw new IllegalArgumentException(String.format("--destinationName parameter must be composed by [application type module]:[destination] or focus module must be application type module and only write the destination name"));
}
}
} else {
if (moduleNames.isEmpty()) {
// error, is necessary select an application module
throw new IllegalArgumentException(String.format("Is necessary to have at least an application type module."));
} else {
module = moduleNames.iterator().next();
destination = destinationName;
}
}
} else {
destination = destinationName;
}
// Add jms springlets dependecies
getProjectOperations().addDependency(classSelected.getModule(), DEPENDENCY_SPRINGLETS_JMS);
// Include springlets-boot-starter-mail in module
getProjectOperations().addDependency(module, DEPENDENCY_SPRINGLETS_STARTER_JMS);
// Include property version
getProjectOperations().addProperty("", PROPERTY_SPRINGLETS_VERSION);
// Add instance of springlets service to service defined by parameter
// Add JavaMailSender to service
final ClassOrInterfaceTypeDetails serviceTypeDetails = getTypeLocationService().getTypeDetails(classSelected);
Validate.isTrue(serviceTypeDetails != null, "Cannot locate source for '%s'", classSelected.getFullyQualifiedTypeName());
final String declaredByMetadataId = serviceTypeDetails.getDeclaredByMetadataId();
final ClassOrInterfaceTypeDetailsBuilder cidBuilder = new ClassOrInterfaceTypeDetailsBuilder(serviceTypeDetails);
// Create service field
cidBuilder.addField(new FieldMetadataBuilder(declaredByMetadataId, PRIVATE, Arrays.asList(new AnnotationMetadataBuilder(AUTOWIRED)), new JavaSymbolName("jmsSendingService"), SpringletsJavaType.SPRINGLETS_JMS_SENDING_SERVICE));
// Set destionation property name
StringBuffer destinationNamePropertyName = new StringBuffer(JMS_PROPERTY_DESTINATION_NAME_PREFIX);
destinationNamePropertyName.append(destination.replaceAll("/", "."));
destinationNamePropertyName.append(JMS_PROPERTY_DESTINATION_NAME_SUFIX);
StringBuffer destionationNameVar = new StringBuffer(JMS_VAR_DESTINATION_NAME_PREFIX);
if (destination.contains("/")) {
// Delete char '/' and each word
String[] destinationNameArray = destination.split("/");
for (String destinationFragment : destinationNameArray) {
destionationNameVar.append(StringUtils.capitalize(destinationFragment));
}
} else {
destionationNameVar.append(StringUtils.capitalize(destination));
}
destionationNameVar.append(JMS_VAR_DESTINATION_NAME_SUFIX);
// Adding @Value annotation
AnnotationMetadataBuilder valueAnnotation = new AnnotationMetadataBuilder(SpringJavaType.VALUE);
valueAnnotation.addStringAttribute("value", "${".concat(destinationNamePropertyName.toString()).concat("}"));
// Add instance of destination name
cidBuilder.addField(new FieldMetadataBuilder(declaredByMetadataId, PRIVATE, Arrays.asList(valueAnnotation), new JavaSymbolName(destionationNameVar.toString()), JavaType.STRING));
// Write both, springlets service and destination instance
getTypeManagementService().createOrUpdateTypeOnDisk(cidBuilder.build());
// Set properties
setProperties(destination, destinationNamePropertyName.toString(), jndiConnectionFactory, module, profile, force);
}
use of org.springframework.roo.classpath.details.FieldMetadataBuilder in project spring-roo by spring-projects.
the class JspViewManager method getCreateDocument.
public Document getCreateDocument() {
final DocumentBuilder builder = XmlUtils.getDocumentBuilder();
final Document document = builder.newDocument();
// Add document namespaces
final Element div = (Element) document.appendChild(new XmlElementBuilder("div", document).addAttribute("xmlns:form", "urn:jsptagdir:/WEB-INF/tags/form").addAttribute("xmlns:field", "urn:jsptagdir:/WEB-INF/tags/form/fields").addAttribute("xmlns:jsp", "http://java.sun.com/JSP/Page").addAttribute("xmlns:c", "http://java.sun.com/jsp/jstl/core").addAttribute("xmlns:spring", "http://www.springframework.org/tags").addAttribute("version", "2.0").addChild(new XmlElementBuilder("jsp:directive.page", document).addAttribute("contentType", "text/html;charset=UTF-8").build()).addChild(new XmlElementBuilder("jsp:output", document).addAttribute("omit-xml-declaration", "yes").build()).build());
// Add form create element
final Element formCreate = new XmlElementBuilder("form:create", document).addAttribute("id", XmlUtils.convertId("fc:" + formBackingType.getFullyQualifiedTypeName())).addAttribute("modelAttribute", entityName).addAttribute("path", controllerPath).addAttribute("render", "${empty dependencies}").build();
if (!controllerPath.equalsIgnoreCase(formBackingType.getSimpleTypeName())) {
formCreate.setAttribute("path", controllerPath);
}
final List<FieldMetadata> formFields = new ArrayList<FieldMetadata>();
final List<FieldMetadata> fieldCopy = new ArrayList<FieldMetadata>(fields);
// Handle Roo identifiers
if (!formBackingTypePersistenceMetadata.getRooIdentifierFields().isEmpty()) {
final String identifierFieldName = formBackingTypePersistenceMetadata.getIdentifierField().getFieldName().getSymbolName();
formCreate.setAttribute("compositePkField", identifierFieldName);
for (final FieldMetadata embeddedField : formBackingTypePersistenceMetadata.getRooIdentifierFields()) {
final FieldMetadataBuilder fieldBuilder = new FieldMetadataBuilder(embeddedField);
fieldBuilder.setFieldName(new JavaSymbolName(identifierFieldName + "." + embeddedField.getFieldName().getSymbolName()));
for (int i = 0; i < fieldCopy.size(); i++) {
// Make sure form fields are not presented twice.
if (fieldCopy.get(i).getFieldName().equals(embeddedField.getFieldName())) {
fieldCopy.remove(i);
break;
}
}
formFields.add(fieldBuilder.build());
}
}
formFields.addAll(fieldCopy);
// If identifier manually assigned, show it in creation
if (formBackingTypePersistenceMetadata.getIdentifierField().getAnnotation(JpaJavaType.GENERATED_VALUE) == null) {
formFields.add(formBackingTypePersistenceMetadata.getIdentifierField());
}
createFieldsForCreateAndUpdate(formFields, document, formCreate, true);
formCreate.setAttribute("z", XmlRoundTripUtils.calculateUniqueKeyFor(formCreate));
final Element dependency = new XmlElementBuilder("form:dependency", document).addAttribute("id", XmlUtils.convertId("d:" + formBackingType.getFullyQualifiedTypeName())).addAttribute("render", "${not empty dependencies}").addAttribute("dependencies", "${dependencies}").build();
dependency.setAttribute("z", XmlRoundTripUtils.calculateUniqueKeyFor(dependency));
div.appendChild(formCreate);
div.appendChild(dependency);
return document;
}
use of org.springframework.roo.classpath.details.FieldMetadataBuilder in project spring-roo by spring-projects.
the class ExceptionsMetadata method getMessageSourceField.
private FieldMetadata getMessageSourceField(Boolean controller) {
// Return current MessageSource field if already exists
if (this.messageSourceField != null) {
return this.messageSourceField;
}
// Preparing @Autowired annotation
List<AnnotationMetadataBuilder> annotations = new ArrayList<AnnotationMetadataBuilder>();
if (controller) {
// MessageSource field must be annotated on controllers
annotations.add(new AnnotationMetadataBuilder(SpringJavaType.AUTOWIRED));
}
// Generating field
FieldMetadataBuilder field = new FieldMetadataBuilder(getId(), Modifier.PRIVATE, annotations, new JavaSymbolName("exceptionMessageSource"), SpringJavaType.MESSAGE_SOURCE);
this.messageSourceField = field.build();
return this.messageSourceField;
}
use of org.springframework.roo.classpath.details.FieldMetadataBuilder in project spring-roo by spring-projects.
the class JavaParserFieldMetadataBuilder method build.
@Override
public FieldMetadata build() {
final FieldMetadataBuilder fieldMetadataBuilder = new FieldMetadataBuilder(declaredByMetadataId);
fieldMetadataBuilder.setAnnotations(annotations);
fieldMetadataBuilder.setFieldInitializer(fieldInitializer);
fieldMetadataBuilder.setFieldName(fieldName);
fieldMetadataBuilder.setFieldType(fieldType);
fieldMetadataBuilder.setModifier(modifier);
return fieldMetadataBuilder.build();
}
Aggregations