use of org.springframework.roo.classpath.details.MethodMetadataBuilder in project spring-roo by spring-projects.
the class JpaEntityMetadata method getAddValueMethod.
/**
* Create add method to handle referenced relation
*
* @param addMethodName
* @param field
* @param cardinality
* @param childType
* @param mappedBy
* @param removeMethodName
* @param importResolver
* @return method metadata or null if method already in class
*/
private MethodMetadata getAddValueMethod(final JavaSymbolName addMethodName, final FieldMetadata field, final Cardinality cardinality, final JavaType childType, final String mappedBy, final JavaSymbolName removeMethodName, ImportRegistrationResolver importResolver) {
// Identify parameters type and name
final List<JavaType> parameterTypes = new ArrayList<JavaType>(1);
final List<JavaSymbolName> parameterNames = new ArrayList<JavaSymbolName>(1);
if (cardinality == Cardinality.ONE_TO_ONE) {
parameterTypes.add(childType);
parameterNames.add(field.getFieldName());
} else {
parameterTypes.add(JavaType.iterableOf(childType));
parameterNames.add(new JavaSymbolName(field.getFieldName().getSymbolName() + ADD_PARAMETER_SUFFIX));
}
// See if the type itself declared the method
MethodMetadata existingMethod = getGovernorMethod(addMethodName, parameterTypes);
if (existingMethod != null) {
return existingMethod;
}
final InvocableMemberBodyBuilder builder = new InvocableMemberBodyBuilder();
if (cardinality == Cardinality.ONE_TO_ONE) {
buildAddOneToOneBody(field, mappedBy, parameterNames.get(0), childType, removeMethodName, builder);
} else if (cardinality == Cardinality.ONE_TO_MANY) {
buildAddOneToManyBody(field, mappedBy, parameterNames.get(0), childType, builder, importResolver);
} else {
buildAddManyToManyBody(field, mappedBy, parameterNames.get(0), childType, builder, importResolver);
}
return new MethodMetadataBuilder(getId(), Modifier.PUBLIC, addMethodName, JavaType.VOID_PRIMITIVE, AnnotatedJavaType.convertFromJavaTypes(parameterTypes), parameterNames, builder).build();
}
use of org.springframework.roo.classpath.details.MethodMetadataBuilder in project spring-roo by spring-projects.
the class JpaEntityMetadata method getRemoveMethod.
/**
* Create remove method to handle referenced relation
*
* @param removeMethodName
* @param field
* @param cardinality
* @param childType
* @param mappedBy
* @param importResolver
* @return method metadata or null if method already in class
*/
private MethodMetadata getRemoveMethod(final JavaSymbolName removeMethodName, final FieldMetadata field, final Cardinality cardinality, final JavaType childType, final String mappedBy, ImportRegistrationResolver importResolver) {
// Identify parameters types and names (if any)
final List<JavaType> parameterTypes = new ArrayList<JavaType>(1);
final List<JavaSymbolName> parameterNames = new ArrayList<JavaSymbolName>(1);
if (cardinality != Cardinality.ONE_TO_ONE) {
parameterTypes.add(JavaType.iterableOf(childType));
parameterNames.add(new JavaSymbolName(field.getFieldName().getSymbolName() + REMOVE_PARAMETER_SUFFIX));
}
// See if the type itself declared the method
MethodMetadata existingMethod = getGovernorMethod(removeMethodName, parameterTypes);
if (existingMethod != null) {
return existingMethod;
}
final InvocableMemberBodyBuilder builder = new InvocableMemberBodyBuilder();
if (cardinality == Cardinality.ONE_TO_ONE) {
buildRemoveOneToOneBody(field, mappedBy, builder);
} else if (cardinality == Cardinality.ONE_TO_MANY) {
buildRemoveOneToManyBody(field, mappedBy, parameterNames.get(0), childType, builder, importResolver);
} else {
// ManyToMany
buildRemoveManyToManyBody(field, mappedBy, parameterNames.get(0), childType, builder, importResolver);
}
return new MethodMetadataBuilder(getId(), Modifier.PUBLIC, removeMethodName, JavaType.VOID_PRIMITIVE, AnnotatedJavaType.convertFromJavaTypes(parameterTypes), parameterNames, builder).build();
}
use of org.springframework.roo.classpath.details.MethodMetadataBuilder in project spring-roo by spring-projects.
the class SeiMetadata method getSEIMethodFromServiceMethod.
/**
* This method obtains a SEI method from a provided service method.
*
* This method caches the generated methods
*
* @param serviceMethod defined in a service interface
*
* @return MethodMetadataBuilder that contains all the information about the new SEI method.
*/
private MethodMetadata getSEIMethodFromServiceMethod(MethodMetadata serviceMethod) {
// Check if already exists the method
if (seiMethodsFromServiceMethods.get(serviceMethod) != null) {
return seiMethodsFromServiceMethods.get(serviceMethod);
}
// If not exists, generate it and cache it.
// Obtain the necessary elements from service method
JavaSymbolName methodName = serviceMethod.getMethodName();
JavaType returnType = serviceMethod.getReturnType();
List<AnnotatedJavaType> parameterTypes = serviceMethod.getParameterTypes();
List<JavaSymbolName> parameterNames = serviceMethod.getParameterNames();
// Obtain parameterList
// Is necessary to change the method name to prevent errors
String paramList = "";
for (AnnotatedJavaType param : parameterTypes) {
paramList = paramList.concat(StringUtils.capitalize(param.getJavaType().getSimpleTypeName())).concat("And");
}
if (StringUtils.isNotBlank(paramList)) {
// Before to update, check if is a finder
if (methodName.toString().startsWith("findBy")) {
methodName = new JavaSymbolName("find");
} else if (methodName.toString().startsWith("countBy")) {
methodName = new JavaSymbolName("count");
}
paramList = paramList.substring(0, paramList.length() - "And".length());
methodName = new JavaSymbolName(methodName.toString().concat("By").concat(paramList));
}
// Annotate parameter types with @WebParam and @XmlJavaTypeAdapter if needed
List<AnnotatedJavaType> annotatedParameterTypes = new ArrayList<AnnotatedJavaType>();
for (int i = 0; i < parameterTypes.size(); i++) {
List<AnnotationMetadata> annotations = new ArrayList<AnnotationMetadata>();
// Getting parameter type and parameter name
AnnotatedJavaType paramType = parameterTypes.get(i);
JavaSymbolName paramName = parameterNames.get(i);
// Creating @WebParam annotation
AnnotationMetadataBuilder webParamAnnotation = new AnnotationMetadataBuilder(JavaType.WEB_PARAM);
webParamAnnotation.addStringAttribute("name", paramName.toString());
webParamAnnotation.addStringAttribute("targetNamespace", "");
annotations.add(webParamAnnotation.build());
// Creating @XmlJavaTypeAdapter annotation
AnnotationMetadataBuilder javaTypeAdapter = new AnnotationMetadataBuilder(JavaType.XML_JAVATYPE_ADAPTER);
if (paramType.getJavaType().getFullyQualifiedTypeName().equals(JavaType.ITERABLE.getFullyQualifiedTypeName())) {
javaTypeAdapter.addClassAttribute("value", SpringletsJavaType.SPRINGLETS_ITERABLE_ADAPTER);
annotations.add(javaTypeAdapter.build());
} else if (paramType.getJavaType().getFullyQualifiedTypeName().equals(SpringJavaType.PAGE.getFullyQualifiedTypeName())) {
javaTypeAdapter.addClassAttribute("value", SpringletsJavaType.SPRINGLETS_PAGE_ADAPTER);
annotations.add(javaTypeAdapter.build());
} else if (paramType.getJavaType().equals(SpringletsJavaType.SPRINGLETS_GLOBAL_SEARCH)) {
javaTypeAdapter.addClassAttribute("value", SpringletsJavaType.SPRINGLETS_GLOBAL_SEARCH_ADAPTER);
annotations.add(javaTypeAdapter.build());
} else if (paramType.getJavaType().equals(SpringJavaType.PAGEABLE)) {
javaTypeAdapter.addClassAttribute("value", SpringletsJavaType.SPRINGLETS_PAGEABLE_ADAPTER);
annotations.add(javaTypeAdapter.build());
}
// Creating new parameter type annotated with @WebParam
AnnotatedJavaType annotatedParam = new AnnotatedJavaType(paramType.getJavaType(), annotations);
annotatedParameterTypes.add(annotatedParam);
}
MethodMetadataBuilder seiMethod = new MethodMetadataBuilder(getId(), Modifier.PUBLIC + Modifier.ABSTRACT, methodName, returnType, annotatedParameterTypes, parameterNames, null);
// Include @XmlJavaTypeAdapter annotation if needed
AnnotationMetadataBuilder javaTypeAdapter = new AnnotationMetadataBuilder(JavaType.XML_JAVATYPE_ADAPTER);
if (returnType.getFullyQualifiedTypeName().equals(JavaType.ITERABLE.getFullyQualifiedTypeName())) {
javaTypeAdapter.addClassAttribute("value", SpringletsJavaType.SPRINGLETS_ITERABLE_ADAPTER);
seiMethod.addAnnotation(javaTypeAdapter);
} else if (returnType.getFullyQualifiedTypeName().equals(SpringJavaType.PAGE.getFullyQualifiedTypeName())) {
javaTypeAdapter.addClassAttribute("value", SpringletsJavaType.SPRINGLETS_PAGE_ADAPTER);
seiMethod.addAnnotation(javaTypeAdapter);
} else if (returnType.equals(SpringletsJavaType.SPRINGLETS_GLOBAL_SEARCH)) {
javaTypeAdapter.addClassAttribute("value", SpringletsJavaType.SPRINGLETS_GLOBAL_SEARCH_ADAPTER);
seiMethod.addAnnotation(javaTypeAdapter);
} else if (returnType.equals(SpringJavaType.PAGEABLE)) {
javaTypeAdapter.addClassAttribute("value", SpringletsJavaType.SPRINGLETS_PAGEABLE_ADAPTER);
seiMethod.addAnnotation(javaTypeAdapter);
}
// Include @RequestWrapper annotation
AnnotationMetadataBuilder requestWrapperAnnotation = new AnnotationMetadataBuilder(JavaType.REQUEST_WRAPPER);
requestWrapperAnnotation.addStringAttribute("className", String.format("%s.%sRequest", sei.getType().getPackage(), seiMethod.getMethodName().getSymbolNameCapitalisedFirstLetter()));
requestWrapperAnnotation.addStringAttribute("localName", String.format("%sRequest", seiMethod.getMethodName().getSymbolNameCapitalisedFirstLetter()));
requestWrapperAnnotation.addStringAttribute("targetNamespace", String.format("http://ws.%s/", StringUtils.reverseDelimited(projectTopLevelPackage.getFullyQualifiedPackageName(), '.')));
seiMethod.addAnnotation(requestWrapperAnnotation);
// Include @ResponseWrapper annotation
AnnotationMetadataBuilder responseWrapperAnnotation = new AnnotationMetadataBuilder(JavaType.RESPONSE_WRAPPER);
responseWrapperAnnotation.addStringAttribute("className", String.format("%s.%sResponse", sei.getType().getPackage(), seiMethod.getMethodName().getSymbolNameCapitalisedFirstLetter()));
responseWrapperAnnotation.addStringAttribute("localName", String.format("%sResponse", seiMethod.getMethodName().getSymbolNameCapitalisedFirstLetter()));
responseWrapperAnnotation.addStringAttribute("targetNamespace", String.format("http://ws.%s/", StringUtils.reverseDelimited(projectTopLevelPackage.getFullyQualifiedPackageName(), '.')));
seiMethod.addAnnotation(responseWrapperAnnotation);
// Include @WebMethod annotation
AnnotationMetadataBuilder webMethodAnnotation = new AnnotationMetadataBuilder(JavaType.WEB_METHOD);
webMethodAnnotation.addStringAttribute("action", String.format("urn:%s", seiMethod.getMethodName().getSymbolNameCapitalisedFirstLetter()));
seiMethod.addAnnotation(webMethodAnnotation);
// Include @WebResult annotation
AnnotationMetadataBuilder webResultAnnotation = new AnnotationMetadataBuilder(JavaType.WEB_RESULT);
webResultAnnotation.addStringAttribute("name", returnType.getBaseType().getSimpleTypeName().toLowerCase());
webResultAnnotation.addStringAttribute("targetNamespace", "");
seiMethod.addAnnotation(webResultAnnotation);
// Include @WSDLDocumentationCollection annotation
AnnotationMetadataBuilder wsdlDocumentationCollectionAnnotation = new AnnotationMetadataBuilder(new JavaType("org.apache.cxf.annotations.WSDLDocumentationCollection"));
// Create @WSDLDocumentation annotation
List<AnnotationAttributeValue<?>> documentations = new ArrayList<AnnotationAttributeValue<?>>();
AnnotationMetadataBuilder documentationAnnotation1 = new AnnotationMetadataBuilder(wsdlDocumentationType);
documentationAnnotation1.addStringAttribute("value", String.format("TODO Auto-generated documentation for %s", sei.getType().getSimpleTypeName()));
documentationAnnotation1.addEnumAttribute("placement", wsdlDocumentationType, new JavaSymbolName("Placement.DEFAULT"));
NestedAnnotationAttributeValue newDocumentation1 = new NestedAnnotationAttributeValue(new JavaSymbolName("value"), documentationAnnotation1.build());
documentations.add(newDocumentation1);
AnnotationMetadataBuilder documentationAnnotation2 = new AnnotationMetadataBuilder(wsdlDocumentationType);
documentationAnnotation2.addStringAttribute("value", String.format("TODO Auto-generated documentation for %s", sei.getType().getSimpleTypeName()));
documentationAnnotation2.addEnumAttribute("placement", wsdlDocumentationType, new JavaSymbolName("Placement.PORT_TYPE_OPERATION_OUTPUT"));
NestedAnnotationAttributeValue newDocumentation2 = new NestedAnnotationAttributeValue(new JavaSymbolName("value"), documentationAnnotation2.build());
documentations.add(newDocumentation2);
ArrayAttributeValue<AnnotationAttributeValue<?>> newDocumentations = new ArrayAttributeValue<AnnotationAttributeValue<?>>(new JavaSymbolName("value"), documentations);
wsdlDocumentationCollectionAnnotation.addAttribute(newDocumentations);
seiMethod.addAnnotation(wsdlDocumentationCollectionAnnotation);
seiMethodsFromServiceMethods.put(serviceMethod, seiMethod.build());
seiMethods.put(seiMethod.build(), serviceMethod);
return seiMethod.build();
}
use of org.springframework.roo.classpath.details.MethodMetadataBuilder in project spring-roo by spring-projects.
the class JaxbEntityMetadata method getXmlIdentityInfoMethod.
/**
* This method returns the getXmlIdentityInfo() method.
*
* @return MethodMetadata that contains the getXmlIdentityInfoMethod
*/
public MethodMetadata getXmlIdentityInfoMethod() {
// Check if already exists
if (xmlIdentityInfoMethod != null) {
return xmlIdentityInfoMethod;
}
// If not, generate a new one
InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
// return getClass().getName() + ":" + getId();
bodyBuilder.appendFormalLine(String.format("return getClass().getName() + \":\" + %s();", identifierAccessor.getMethodName()));
MethodMetadataBuilder method = new MethodMetadataBuilder(getId(), Modifier.PUBLIC, new JavaSymbolName("getXmlIdentityInfo"), JavaType.STRING, bodyBuilder);
method.addAnnotation(new AnnotationMetadataBuilder(JavaType.XML_ID));
AnnotationMetadataBuilder xmlAttributeAnnotation = new AnnotationMetadataBuilder(JavaType.XML_ATTRIBUTE);
xmlAttributeAnnotation.addStringAttribute("name", "id");
method.addAnnotation(xmlAttributeAnnotation);
CommentStructure comment = new CommentStructure();
comment.addComment(new JavadocComment("Must return an unique ID across all entities"), CommentLocation.BEGINNING);
method.setCommentStructure(comment);
xmlIdentityInfoMethod = method.build();
return xmlIdentityInfoMethod;
}
use of org.springframework.roo.classpath.details.MethodMetadataBuilder in project spring-roo by spring-projects.
the class WsEndpointsMetadata method getOpenEntityManagerInViewFilterMethod.
/**
* This method obtains the method that register the openEntityManagerInView
* filter
*
* @return MethodMetadata with the information about the new method
*/
private MethodMetadata getOpenEntityManagerInViewFilterMethod() {
// Check if already exists
if (openEntityManagerInViewFilterMethod != null) {
return openEntityManagerInViewFilterMethod;
}
JavaType filterRegistrationBeanType = new JavaType("org.springframework.boot.context.embedded.FilterRegistrationBean");
// Generating method body
InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
// FilterRegistrationBean filterRegBean = new FilterRegistrationBean();
bodyBuilder.appendFormalLine("%s filterRegBean = new FilterRegistrationBean();", getNameOfJavaType(filterRegistrationBeanType));
// filterRegBean.setFilter(new OpenEntityManagerInViewFilter());
bodyBuilder.appendFormalLine("filterRegBean.setFilter(new %s());", getNameOfJavaType(new JavaType("org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter")));
// List<String> urlPatterns = new ArrayList<String>();
bodyBuilder.appendFormalLine("%s<String> urlPatterns = new %s<String>();", getNameOfJavaType(JavaType.LIST), getNameOfJavaType(JavaType.ARRAY_LIST));
// urlPatterns.add(this.cxfServletPath + "/*");
bodyBuilder.appendFormalLine("urlPatterns.add(%s() + \"/*\");", getAccessorMethod(getServletField()).getMethodName());
// filterRegBean.setUrlPatterns(urlPatterns);
bodyBuilder.appendFormalLine("filterRegBean.setUrlPatterns(urlPatterns);");
// if (LOG.isDebugEnabled()) {
bodyBuilder.appendFormalLine("if (%s().isDebugEnabled()) {", getAccessorMethod(getLoggerField()).getMethodName());
// LOG.debug("Registering the 'OpenEntityManagerInViewFilter' filter for the '"
bodyBuilder.indent();
bodyBuilder.appendFormalLine("%s().debug(\"Registering the 'OpenEntityManagerInViewFilter' filter for the '\"", getAccessorMethod(getLoggerField()).getMethodName());
// .concat(this.cxfServletPath + "/*").concat("' URL."));
bodyBuilder.indent();
bodyBuilder.appendFormalLine(".concat(%s() + \"/*\").concat(\"' URL.\"));", getAccessorMethod(getServletField()).getMethodName());
bodyBuilder.indentRemove();
bodyBuilder.indentRemove();
// }
bodyBuilder.appendFormalLine("}");
// return filterRegBean;
bodyBuilder.appendFormalLine("return filterRegBean;");
MethodMetadataBuilder method = new MethodMetadataBuilder(getId(), Modifier.PUBLIC, new JavaSymbolName("openEntityManagerInViewFilter"), filterRegistrationBeanType, bodyBuilder);
method.addAnnotation(new AnnotationMetadataBuilder(SpringJavaType.BEAN));
openEntityManagerInViewFilterMethod = method.build();
return openEntityManagerInViewFilterMethod;
}
Aggregations