Search in sources :

Example 26 with InvocableMemberBodyBuilder

use of org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder in project spring-roo by spring-projects.

the class SeiImplMetadata method getConstructor.

/**
 * This method obtains the constructor method of the annotated endpoint
 *
 * @return constructor method for this endpoint
 */
private ConstructorMetadata getConstructor() {
    // Check if constructor has been generated before
    if (constructor != null) {
        return constructor;
    }
    // Creating new constructor
    ConstructorMetadataBuilder constructorMethod = new ConstructorMetadataBuilder(getId());
    constructorMethod.setModifier(Modifier.PUBLIC);
    constructorMethod.addParameter(getServiceField().getFieldName().getSymbolNameUnCapitalisedFirstLetter(), getServiceField().getFieldType());
    // Generating constructor body
    InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
    bodyBuilder.appendFormalLine("%s(%s);", getMutatorMethod(getServiceField()).getMethodName(), getServiceField().getFieldName().getSymbolNameUnCapitalisedFirstLetter());
    constructorMethod.setBodyBuilder(bodyBuilder);
    constructor = constructorMethod.build();
    return constructor;
}
Also used : ConstructorMetadataBuilder(org.springframework.roo.classpath.details.ConstructorMetadataBuilder) InvocableMemberBodyBuilder(org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder)

Example 27 with InvocableMemberBodyBuilder

use of org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder 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;
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) JavadocComment(org.springframework.roo.classpath.details.comments.JavadocComment) MethodMetadataBuilder(org.springframework.roo.classpath.details.MethodMetadataBuilder) InvocableMemberBodyBuilder(org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder) CommentStructure(org.springframework.roo.classpath.details.comments.CommentStructure)

Example 28 with InvocableMemberBodyBuilder

use of org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder 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;
}
Also used : SpringJavaType(org.springframework.roo.model.SpringJavaType) JavaType(org.springframework.roo.model.JavaType) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) MethodMetadataBuilder(org.springframework.roo.classpath.details.MethodMetadataBuilder) InvocableMemberBodyBuilder(org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)

Example 29 with InvocableMemberBodyBuilder

use of org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder 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;
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) MethodMetadataBuilder(org.springframework.roo.classpath.details.MethodMetadataBuilder) AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) ArrayList(java.util.ArrayList) InvocableMemberBodyBuilder(org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)

Example 30 with InvocableMemberBodyBuilder

use of org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder 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();
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) JdkJavaType(org.springframework.roo.model.JdkJavaType) SpringJavaType(org.springframework.roo.model.SpringJavaType) SpringletsJavaType(org.springframework.roo.model.SpringletsJavaType) JavaType(org.springframework.roo.model.JavaType) MethodMetadataBuilder(org.springframework.roo.classpath.details.MethodMetadataBuilder) AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) ArrayList(java.util.ArrayList) MethodMetadata(org.springframework.roo.classpath.details.MethodMetadata) InvocableMemberBodyBuilder(org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder)

Aggregations

InvocableMemberBodyBuilder (org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder)152 JavaSymbolName (org.springframework.roo.model.JavaSymbolName)121 MethodMetadataBuilder (org.springframework.roo.classpath.details.MethodMetadataBuilder)117 AnnotatedJavaType (org.springframework.roo.classpath.details.annotations.AnnotatedJavaType)105 MethodMetadata (org.springframework.roo.classpath.details.MethodMetadata)92 AnnotationMetadataBuilder (org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)90 ArrayList (java.util.ArrayList)85 JavaType (org.springframework.roo.model.JavaType)70 SpringJavaType (org.springframework.roo.model.SpringJavaType)46 JdkJavaType (org.springframework.roo.model.JdkJavaType)44 FieldMetadata (org.springframework.roo.classpath.details.FieldMetadata)40 SpringletsJavaType (org.springframework.roo.model.SpringletsJavaType)34 Jsr303JavaType (org.springframework.roo.model.Jsr303JavaType)27 CommentStructure (org.springframework.roo.classpath.details.comments.CommentStructure)16 JavadocComment (org.springframework.roo.classpath.details.comments.JavadocComment)16 ServiceMetadata (org.springframework.roo.addon.layers.service.addon.ServiceMetadata)15 ConstructorMetadataBuilder (org.springframework.roo.classpath.details.ConstructorMetadataBuilder)15 RelationInfo (org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata.RelationInfo)14 RelationInfoExtended (org.springframework.roo.addon.web.mvc.controller.addon.RelationInfoExtended)11 JpaJavaType (org.springframework.roo.model.JpaJavaType)9