Search in sources :

Example 31 with AnnotationMetadataBuilder

use of org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder in project spring-roo by spring-projects.

the class JpaEntityMetadata method getTableAnnotation.

/**
 * Generates the JPA @Table annotation to be applied to the entity
 *
 * @param annotationValues
 * @return
 */
private AnnotationMetadata getTableAnnotation() {
    final AnnotationMetadata tableAnnotation = getTypeAnnotation(TABLE);
    if (tableAnnotation == null) {
        return null;
    }
    final String catalog = annotationValues.getCatalog();
    final String schema = annotationValues.getSchema();
    final String table = annotationValues.getTable();
    if (StringUtils.isNotBlank(table) || StringUtils.isNotBlank(schema) || StringUtils.isNotBlank(catalog)) {
        final AnnotationMetadataBuilder tableBuilder = new AnnotationMetadataBuilder(tableAnnotation);
        if (StringUtils.isNotBlank(catalog)) {
            tableBuilder.addStringAttribute("catalog", catalog);
        }
        if (StringUtils.isNotBlank(schema)) {
            tableBuilder.addStringAttribute("schema", schema);
        }
        if (StringUtils.isNotBlank(table)) {
            tableBuilder.addStringAttribute("name", table);
        }
        return tableBuilder.build();
    }
    return null;
}
Also used : AnnotationMetadata(org.springframework.roo.classpath.details.annotations.AnnotationMetadata) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)

Example 32 with AnnotationMetadataBuilder

use of org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder in project spring-roo by spring-projects.

the class NewUpdateCompilationUnitTest method testSimpleClassAddAnnotation.

@Test
public void testSimpleClassAddAnnotation() throws Exception {
    // Set up
    final File file = getResource(SIMPLE_CLASS_FILE_PATH);
    final String fileContents = getResourceContents(file);
    final ClassOrInterfaceTypeDetails simpleInterfaceDetails = typeParsingService.getTypeFromString(fileContents, SIMPLE_CLASS_DECLARED_BY_MID, SIMPLE_CLASS_TYPE);
    final AnnotationMetadataBuilder annotationBuilder = new AnnotationMetadataBuilder(new JavaType("org.springframework.roo.addon.javabean.addon.RooToString"));
    final ClassOrInterfaceTypeDetails newSimpleInterfaceDetails = addAnnotation(simpleInterfaceDetails, annotationBuilder.build());
    // Invoke
    final String result = typeParsingService.updateAndGetCompilationUnitContents(file.getCanonicalPath(), newSimpleInterfaceDetails);
    saveResult(file, result, "-addedAnnotation");
    checkSimpleClass(result);
    assertTrue(result.contains("import org.springframework.roo.addon.javabean.addon.RooToString;"));
    assertTrue(result.contains("@RooToString"));
    // Invoke again
    final ClassOrInterfaceTypeDetails simpleInterfaceDetails2 = typeParsingService.getTypeFromString(result, SIMPLE_CLASS_DECLARED_BY_MID, SIMPLE_CLASS_TYPE);
    final String result2 = typeParsingService.updateAndGetCompilationUnitContents(file.getCanonicalPath(), simpleInterfaceDetails2);
    saveResult(file, result2, "-addedAnnotation2");
    checkSimpleClass(result2);
    assertTrue(result2.contains("import org.springframework.roo.addon.javabean.addon.RooToString;"));
    assertTrue(result2.contains("@RooToString"));
}
Also used : JavaType(org.springframework.roo.model.JavaType) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) File(java.io.File) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder) Test(org.junit.Test)

Example 33 with AnnotationMetadataBuilder

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

Example 34 with AnnotationMetadataBuilder

use of org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder in project spring-roo by spring-projects.

the class WsOperationsImpl method annotateClassIfNeeded.

/**
 * This method annotates the provided class with @RooJaxbEntity. If this class extends
 * other classes, and that classes annotates other classes, etc.
 * this method will annotate them.
 *
 * @param entityDetails
 */
private void annotateClassIfNeeded(ClassOrInterfaceTypeDetails entityDetails) {
    List<JavaType> extendsTypes = entityDetails.getExtendsTypes();
    for (JavaType extendsType : extendsTypes) {
        ClassOrInterfaceTypeDetails extendsTypeDetails = getTypeLocationService().getTypeDetails(extendsType);
        if (extendsTypeDetails != null && extendsTypeDetails.getAnnotation(RooJavaType.ROO_JPA_ENTITY) != null) {
            // If annotation has not been included before, add it.
            if (extendsTypeDetails.getAnnotation(RooJavaType.ROO_JAXB_ENTITY) == null) {
                ClassOrInterfaceTypeDetailsBuilder cidBuilder = new ClassOrInterfaceTypeDetailsBuilder(extendsTypeDetails);
                // Include @RooJaxbEntity annotation
                AnnotationMetadataBuilder jaxbEntityAnnotation = new AnnotationMetadataBuilder(RooJavaType.ROO_JAXB_ENTITY);
                cidBuilder.addAnnotation(jaxbEntityAnnotation);
                // Write entity class on disk
                getTypeManagementService().createOrUpdateTypeOnDisk(cidBuilder.build());
            }
            // have been annotated
            if (!extendsTypeDetails.getExtendsTypes().isEmpty()) {
                annotateClassIfNeeded(extendsTypeDetails);
            }
        }
    }
    // If annotation has not been included before, add it.
    if (entityDetails.getAnnotation(RooJavaType.ROO_JAXB_ENTITY) == null) {
        ClassOrInterfaceTypeDetailsBuilder cidBuilder = new ClassOrInterfaceTypeDetailsBuilder(entityDetails);
        // Include @RooJaxbEntity annotation
        AnnotationMetadataBuilder jaxbEntityAnnotation = new AnnotationMetadataBuilder(RooJavaType.ROO_JAXB_ENTITY);
        cidBuilder.addAnnotation(jaxbEntityAnnotation);
        // Write entity class on disk
        getTypeManagementService().createOrUpdateTypeOnDisk(cidBuilder.build());
    }
}
Also used : RooJavaType(org.springframework.roo.model.RooJavaType) SpringJavaType(org.springframework.roo.model.SpringJavaType) JpaJavaType(org.springframework.roo.model.JpaJavaType) JavaType(org.springframework.roo.model.JavaType) ClassOrInterfaceTypeDetailsBuilder(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)

Example 35 with AnnotationMetadataBuilder

use of org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder in project spring-roo by spring-projects.

the class WsOperationsImpl method getWsClientAnnotation.

/**
 * This method provides @RooWsClient annotation with all the necessary attributes
 *
 * @param endpoint
 * @param targetNamespace
 * @param bindingType
 * @return
 */
private AnnotationMetadataBuilder getWsClientAnnotation(final String endpoint, final String targetNamespace, final EnumDetails bindingType) {
    final List<AnnotationAttributeValue<?>> wsClientAttributes = new ArrayList<AnnotationAttributeValue<?>>();
    wsClientAttributes.add(new StringAttributeValue(new JavaSymbolName("endpoint"), endpoint));
    wsClientAttributes.add(new StringAttributeValue(new JavaSymbolName("targetNamespace"), targetNamespace));
    wsClientAttributes.add(new EnumAttributeValue(new JavaSymbolName("binding"), bindingType));
    return new AnnotationMetadataBuilder(RooJavaType.ROO_WS_CLIENT, wsClientAttributes);
}
Also used : AnnotationAttributeValue(org.springframework.roo.classpath.details.annotations.AnnotationAttributeValue) NestedAnnotationAttributeValue(org.springframework.roo.classpath.details.annotations.NestedAnnotationAttributeValue) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) ArrayList(java.util.ArrayList) EnumAttributeValue(org.springframework.roo.classpath.details.annotations.EnumAttributeValue) StringAttributeValue(org.springframework.roo.classpath.details.annotations.StringAttributeValue) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)

Aggregations

AnnotationMetadataBuilder (org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)232 JavaSymbolName (org.springframework.roo.model.JavaSymbolName)155 ArrayList (java.util.ArrayList)129 InvocableMemberBodyBuilder (org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder)90 MethodMetadataBuilder (org.springframework.roo.classpath.details.MethodMetadataBuilder)87 AnnotatedJavaType (org.springframework.roo.classpath.details.annotations.AnnotatedJavaType)81 JavaType (org.springframework.roo.model.JavaType)78 MethodMetadata (org.springframework.roo.classpath.details.MethodMetadata)73 ClassOrInterfaceTypeDetailsBuilder (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder)54 SpringJavaType (org.springframework.roo.model.SpringJavaType)47 ClassOrInterfaceTypeDetails (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails)42 FieldMetadataBuilder (org.springframework.roo.classpath.details.FieldMetadataBuilder)40 JdkJavaType (org.springframework.roo.model.JdkJavaType)39 AnnotationAttributeValue (org.springframework.roo.classpath.details.annotations.AnnotationAttributeValue)31 AnnotationMetadata (org.springframework.roo.classpath.details.annotations.AnnotationMetadata)30 RooJavaType (org.springframework.roo.model.RooJavaType)28 SpringletsJavaType (org.springframework.roo.model.SpringletsJavaType)28 FieldMetadata (org.springframework.roo.classpath.details.FieldMetadata)26 Jsr303JavaType (org.springframework.roo.model.Jsr303JavaType)25 StringAttributeValue (org.springframework.roo.classpath.details.annotations.StringAttributeValue)23