Search in sources :

Example 61 with JavaType

use of org.springframework.roo.model.JavaType in project spring-roo by spring-projects.

the class SeiImplMetadataProviderImpl method getMetadata.

@Override
protected ItdTypeDetailsProvidingMetadataItem getMetadata(final String metadataIdentificationString, final JavaType aspectName, final PhysicalTypeMetadata governorPhysicalTypeMetadata, final String itdFilename) {
    // Getting annotated class details
    ClassOrInterfaceTypeDetails endpoint = governorPhysicalTypeMetadata.getMemberHoldingTypeDetails();
    AnnotationMetadata seiImplAnnotation = endpoint.getAnnotation(ROO_SEI_IMPL);
    AnnotationAttributeValue<?> seiAttr = seiImplAnnotation.getAttribute(new JavaSymbolName("sei"));
    Validate.notNull(seiAttr, "ERROR: You must provide a valid SEI to be able to generate a Endpoint.");
    // Getting SEI from annotation
    JavaType seiType = (JavaType) seiAttr.getValue();
    // Getting SEI details
    ClassOrInterfaceTypeDetails seiTypeDetails = getTypeLocationService().getTypeDetails(seiType);
    // Getting SEI Metadata
    final String seiMetadataId = SeiMetadata.createIdentifier(seiTypeDetails.getType(), PhysicalTypeIdentifier.getPath(seiTypeDetails.getDeclaredByMetadataId()));
    final SeiMetadata seiMetadata = (SeiMetadata) getMetadataService().get(seiMetadataId);
    // Getting SEI methods from service and save it
    Map<MethodMetadata, MethodMetadata> seiMethods = seiMetadata.getSeiMethods();
    // Registering dependency between SeiMetadata and this one, to be able to
    // update Endpoint if SEI changes
    final String seiMetadataKey = SeiMetadata.createIdentifier(seiTypeDetails);
    registerDependency(seiMetadataKey, metadataIdentificationString);
    return new SeiImplMetadata(metadataIdentificationString, aspectName, governorPhysicalTypeMetadata, getProjectOperations().getTopLevelPackage(""), endpoint, seiType, seiMetadata.getService(), seiMethods);
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) RooJavaType(org.springframework.roo.model.RooJavaType) JavaType(org.springframework.roo.model.JavaType) MethodMetadata(org.springframework.roo.classpath.details.MethodMetadata) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) AnnotationMetadata(org.springframework.roo.classpath.details.annotations.AnnotationMetadata)

Example 62 with JavaType

use of org.springframework.roo.model.JavaType 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 63 with JavaType

use of org.springframework.roo.model.JavaType in project spring-roo by spring-projects.

the class JavaParserUtils method getJavaType.

/**
 * Resolves the effective {@link JavaType} a
 * {@link ClassOrInterfaceDeclaration} represents, including any type
 * parameters.
 *
 * @param compilationUnitServices for package management (required)
 * @param typeDeclaration the type declaration to resolve (required)
 * @return the effective Java type (never null)
 */
public static JavaType getJavaType(final CompilationUnitServices compilationUnitServices, final TypeDeclaration typeDeclaration) {
    Validate.notNull(compilationUnitServices, "Compilation unit services required");
    Validate.notNull(typeDeclaration, "Type declaration required");
    // Convert the ClassOrInterfaceDeclaration name into a JavaType
    final NameExpr nameExpr = getNameExpr(typeDeclaration.getName());
    final JavaType effectiveType = getJavaType(compilationUnitServices, nameExpr, null);
    final List<JavaType> parameterTypes = new ArrayList<JavaType>();
    if (typeDeclaration instanceof ClassOrInterfaceDeclaration) {
        final ClassOrInterfaceDeclaration clazz = (ClassOrInterfaceDeclaration) typeDeclaration;
        // Populate JavaType with type parameters
        final List<TypeParameter> typeParameters = clazz.getTypeParameters();
        if (typeParameters != null) {
            final Set<JavaSymbolName> locatedTypeParameters = new HashSet<JavaSymbolName>();
            for (final TypeParameter candidate : typeParameters) {
                final JavaSymbolName currentTypeParam = new JavaSymbolName(candidate.getName());
                locatedTypeParameters.add(currentTypeParam);
                JavaType javaType = null;
                if (candidate.getTypeBound() == null) {
                    javaType = new JavaType(OBJECT.getFullyQualifiedTypeName(), 0, DataType.TYPE, currentTypeParam, null);
                } else {
                    final ClassOrInterfaceType cit = candidate.getTypeBound().get(0);
                    javaType = JavaParserUtils.getJavaTypeNow(compilationUnitServices, cit, locatedTypeParameters);
                    javaType = new JavaType(javaType.getFullyQualifiedTypeName(), javaType.getArray(), javaType.getDataType(), currentTypeParam, javaType.getParameters());
                }
                parameterTypes.add(javaType);
            }
        }
    }
    return new JavaType(effectiveType.getFullyQualifiedTypeName(), effectiveType.getArray(), effectiveType.getDataType(), null, parameterTypes);
}
Also used : JdkJavaType(org.springframework.roo.model.JdkJavaType) JavaType(org.springframework.roo.model.JavaType) TypeParameter(com.github.antlrjavaparser.api.TypeParameter) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) ClassOrInterfaceDeclaration(com.github.antlrjavaparser.api.body.ClassOrInterfaceDeclaration) NameExpr(com.github.antlrjavaparser.api.expr.NameExpr) QualifiedNameExpr(com.github.antlrjavaparser.api.expr.QualifiedNameExpr) ArrayList(java.util.ArrayList) ClassOrInterfaceType(com.github.antlrjavaparser.api.type.ClassOrInterfaceType) HashSet(java.util.HashSet)

Example 64 with JavaType

use of org.springframework.roo.model.JavaType in project spring-roo by spring-projects.

the class JavaParserUtils method getResolvedName.

public static ClassOrInterfaceType getResolvedName(final JavaType target, final JavaType current, final CompilationUnit compilationUnit) {
    final NameExpr nameExpr = JavaParserUtils.importTypeIfRequired(target, compilationUnit.getImports(), current);
    final ClassOrInterfaceType resolvedName = JavaParserUtils.getClassOrInterfaceType(nameExpr);
    if (current.getParameters() != null && current.getParameters().size() > 0) {
        resolvedName.setTypeArgs(new ArrayList<Type>());
        for (final JavaType param : current.getParameters()) {
            resolvedName.getTypeArgs().add(getResolvedName(target, param, compilationUnit));
        }
    }
    return resolvedName;
}
Also used : WildcardType(com.github.antlrjavaparser.api.type.WildcardType) ClassOrInterfaceType(com.github.antlrjavaparser.api.type.ClassOrInterfaceType) DataType(org.springframework.roo.model.DataType) JdkJavaType(org.springframework.roo.model.JdkJavaType) VoidType(com.github.antlrjavaparser.api.type.VoidType) JavaType(org.springframework.roo.model.JavaType) PrimitiveType(com.github.antlrjavaparser.api.type.PrimitiveType) ReferenceType(com.github.antlrjavaparser.api.type.ReferenceType) Type(com.github.antlrjavaparser.api.type.Type) JdkJavaType(org.springframework.roo.model.JdkJavaType) JavaType(org.springframework.roo.model.JavaType) NameExpr(com.github.antlrjavaparser.api.expr.NameExpr) QualifiedNameExpr(com.github.antlrjavaparser.api.expr.QualifiedNameExpr) ClassOrInterfaceType(com.github.antlrjavaparser.api.type.ClassOrInterfaceType)

Example 65 with JavaType

use of org.springframework.roo.model.JavaType 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)

Aggregations

JavaType (org.springframework.roo.model.JavaType)411 RooJavaType (org.springframework.roo.model.RooJavaType)212 ArrayList (java.util.ArrayList)142 JavaSymbolName (org.springframework.roo.model.JavaSymbolName)133 AnnotatedJavaType (org.springframework.roo.classpath.details.annotations.AnnotatedJavaType)129 ClassOrInterfaceTypeDetails (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails)114 JdkJavaType (org.springframework.roo.model.JdkJavaType)110 SpringJavaType (org.springframework.roo.model.SpringJavaType)101 JpaJavaType (org.springframework.roo.model.JpaJavaType)83 AnnotationMetadataBuilder (org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)78 FieldMetadata (org.springframework.roo.classpath.details.FieldMetadata)76 MethodMetadata (org.springframework.roo.classpath.details.MethodMetadata)72 InvocableMemberBodyBuilder (org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder)70 MethodMetadataBuilder (org.springframework.roo.classpath.details.MethodMetadataBuilder)65 LogicalPath (org.springframework.roo.project.LogicalPath)62 SpringletsJavaType (org.springframework.roo.model.SpringletsJavaType)60 AnnotationMetadata (org.springframework.roo.classpath.details.annotations.AnnotationMetadata)59 Jsr303JavaType (org.springframework.roo.model.Jsr303JavaType)38 MemberDetails (org.springframework.roo.classpath.scanner.MemberDetails)35 ClassOrInterfaceTypeDetailsBuilder (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder)30