Search in sources :

Example 6 with JavaType

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

the class EntityDeserializerMetadataProviderImpl method getMetadata.

@Override
protected ItdTypeDetailsProvidingMetadataItem getMetadata(final String metadataIdentificationString, final JavaType aspectName, final PhysicalTypeMetadata governorPhysicalTypeMetadata, final String itdFilename) {
    final EntityDeserializerAnnotationValues values = new EntityDeserializerAnnotationValues(governorPhysicalTypeMetadata);
    final JavaType deserializerType = governorPhysicalTypeMetadata.getType();
    final JavaType entity = values.getEntity();
    final ClassOrInterfaceTypeDetails entityDetails = getTypeLocationService().getTypeDetails(entity);
    Validate.notNull(entityDetails, "Can't get details of '%s' defined on '%s.@%s.entity'", entity.getFullyQualifiedTypeName(), deserializerType, RooJavaType.ROO_DESERIALIZER.getSimpleTypeName());
    Validate.notNull(entityDetails.getAnnotation(RooJavaType.ROO_JPA_ENTITY), "Class '%s' defined on '%s.@%s.entity' has no @%s annotation. Only JPA entities can set as mixin", entity.getFullyQualifiedTypeName(), deserializerType, RooJavaType.ROO_DESERIALIZER.getSimpleTypeName());
    // Register JpaEntityMetadata dependency
    final String entityId = JpaEntityMetadata.createIdentifier(entityDetails);
    final JpaEntityMetadata entityMetadata = getMetadataService().get(entityId);
    if (entityMetadata == null) {
        // not ready to this metadata yet
        return null;
    }
    registerDependency(entityId, metadataIdentificationString);
    // Register JavaBeanMetadata dependency
    final String javaBeanId = JavaBeanMetadata.createIdentifier(entityDetails);
    final JavaBeanMetadata javaBeanMetadata = getMetadataService().get(javaBeanId);
    if (javaBeanMetadata == null) {
        // not ready to this metadata yet
        return null;
    }
    registerDependency(javaBeanId, metadataIdentificationString);
    // Register ServiceMetadata dependency
    ClassOrInterfaceTypeDetails serviceDetails = getServiceLocator().getService(entity);
    String serviceMetadataId = ServiceMetadata.createIdentifier(serviceDetails);
    ServiceMetadata serviceMetadata = getMetadataService().get(serviceMetadataId);
    if (serviceMetadata == null) {
        // not ready to this metadata yet
        return null;
    }
    return new EntityDeserializerMetadata(metadataIdentificationString, aspectName, governorPhysicalTypeMetadata, values, entityMetadata, serviceMetadata);
}
Also used : RooJavaType(org.springframework.roo.model.RooJavaType) JavaType(org.springframework.roo.model.JavaType) JavaBeanMetadata(org.springframework.roo.addon.javabean.addon.JavaBeanMetadata) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) JpaEntityMetadata(org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata) ServiceMetadata(org.springframework.roo.addon.layers.service.addon.ServiceMetadata)

Example 7 with JavaType

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

the class JSONMixinMetadataProviderImpl method getMetadata.

@Override
protected ItdTypeDetailsProvidingMetadataItem getMetadata(final String metadataIdentificationString, final JavaType aspectName, final PhysicalTypeMetadata governorPhysicalTypeMetadata, final String itdFilename) {
    final JSONMixinAnnotationValues values = new JSONMixinAnnotationValues(governorPhysicalTypeMetadata);
    final JavaType mixinType = governorPhysicalTypeMetadata.getType();
    final JavaType entity = values.getEntity();
    final ClassOrInterfaceTypeDetails entityDetails = getTypeLocationService().getTypeDetails(entity);
    Validate.notNull(entityDetails, "Can't get details of '%s' defined on '%s.@%s.entity'", entity.getFullyQualifiedTypeName(), mixinType, RooJavaType.ROO_JSON_MIXIN.getSimpleTypeName());
    Validate.notNull(entityDetails.getAnnotation(RooJavaType.ROO_JPA_ENTITY), "Class '%s' defined on '%s.@%s.entity' has no @%s annotation. Only JPA entities can set as mixin", entity.getFullyQualifiedTypeName(), mixinType, RooJavaType.ROO_JSON_MIXIN.getSimpleTypeName());
    final String entityId = JpaEntityMetadata.createIdentifier(entityDetails);
    final JpaEntityMetadata entityMetadata = getMetadataService().get(entityId);
    if (entityMetadata == null) {
        // not ready for this metadata yet
        return null;
    }
    // register metadata dependency
    registerDependency(entityId, metadataIdentificationString);
    // Register dependency with DomainModelModule
    Set<ClassOrInterfaceTypeDetails> domainModelModuleDetails = getTypeLocationService().findClassesOrInterfaceDetailsWithAnnotation(RooJavaType.ROO_DOMAIN_MODEL_MODULE);
    if (!domainModelModuleDetails.isEmpty()) {
        String domainModelModuleMetadataId = DomainModelModuleMetadata.createIdentifier(domainModelModuleDetails.iterator().next());
        registerDependency(metadataIdentificationString, domainModelModuleMetadataId);
    }
    Map<FieldMetadata, JavaType> jsonDeserializerByEntity = new TreeMap<FieldMetadata, JavaType>(FieldMetadata.COMPARATOR_BY_NAME);
    for (FieldMetadata field : entityMetadata.getRelationsAsChild().values()) {
        if (isAnyToOneRelation(field)) {
            JavaType parentEntity = field.getFieldType().withoutParameters();
            JavaType entityDeserializer = getEntityDeserializerFor(parentEntity);
            Validate.notNull(entityDeserializer, "Can't locate class with @%s.entity=%s required for %s entity Json Mixin (%s)", RooJavaType.ROO_DESERIALIZER, parentEntity, entity.getFullyQualifiedTypeName(), mixinType.getFullyQualifiedTypeName());
            jsonDeserializerByEntity.put(field, entityDeserializer);
        }
    }
    return new JSONMixinMetadata(metadataIdentificationString, aspectName, governorPhysicalTypeMetadata, values, entityMetadata, jsonDeserializerByEntity);
}
Also used : RooJavaType(org.springframework.roo.model.RooJavaType) JpaJavaType(org.springframework.roo.model.JpaJavaType) JavaType(org.springframework.roo.model.JavaType) FieldMetadata(org.springframework.roo.classpath.details.FieldMetadata) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) JpaEntityMetadata(org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata) TreeMap(java.util.TreeMap)

Example 8 with JavaType

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

the class ExceptionsMetadata method getHandlerExceptionMethod.

private MethodMetadata getHandlerExceptionMethod(JavaType exception, ClassOrInterfaceTypeDetails exceptionDetails) {
    // Define annotations
    List<AnnotationMetadataBuilder> annotationTypes = new ArrayList<AnnotationMetadataBuilder>();
    AnnotationMetadataBuilder responsebody = new AnnotationMetadataBuilder(SpringJavaType.RESPONSE_BODY);
    AnnotationMetadataBuilder exceptionHandler = new AnnotationMetadataBuilder(SpringJavaType.EXCEPTION_HANDLER);
    exceptionHandler.addClassAttribute("value", exception);
    annotationTypes.add(responsebody);
    annotationTypes.add(exceptionHandler);
    // Define method parameter types
    List<AnnotatedJavaType> parameterTypes = new ArrayList<AnnotatedJavaType>();
    parameterTypes.add(AnnotatedJavaType.convertFromJavaType(HTTP_SERVLET_REQUEST));
    parameterTypes.add(AnnotatedJavaType.convertFromJavaType(JdkJavaType.EXCEPTION));
    parameterTypes.add(AnnotatedJavaType.convertFromJavaType(JdkJavaType.LOCALE));
    // Define method parameter names
    List<JavaSymbolName> parameterNames = new ArrayList<JavaSymbolName>();
    parameterNames.add(new JavaSymbolName(HTTP_SERVLET_REQUEST_PARAM_NAME));
    parameterNames.add(new JavaSymbolName(EXCEPTION_PARAM_NAME));
    parameterNames.add(new JavaSymbolName(LOCALE_PARAM_NAME));
    // Define method name
    JavaSymbolName methodName = new JavaSymbolName("handle".concat(exception.getSimpleTypeName()));
    MethodMetadata existingMethod = getGovernorMethod(methodName);
    if (existingMethod != null) {
        return existingMethod;
    }
    // Set throws types
    List<JavaType> throwTypes = new ArrayList<JavaType>();
    throwTypes.add(JdkJavaType.EXCEPTION);
    // Generate body
    InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
    // String errorMessage =
    bodyBuilder.appendFormalLine(String.format("%s errorMessage = ", getNameOfJavaType(JavaType.STRING)));
    // this.messageSource.getMessage("label_my_exception", null, locale);
    bodyBuilder.indent();
    bodyBuilder.appendFormalLine(String.format("this.%s.getMessage(\"label_%s\", null, locale);", this.messageSourceField.getFieldName(), exception.getSimpleTypeName().toLowerCase()));
    bodyBuilder.indentRemove();
    // LOG.error(errorMessage, e);
    bodyBuilder.appendFormalLine("LOG.error(errorMessage, e);");
    // return ResponseEntity.status(HttpStatus.ERROR_CODE)
    // .body(Collections.singletonMap("message", errorMessage));
    bodyBuilder.appendFormalLine(String.format("return %s.status(%s)", getNameOfJavaType(SpringJavaType.RESPONSE_ENTITY), exceptionDetails.getAnnotation(SpringJavaType.RESPONSE_STATUS).getAttribute("value").getValue()));
    bodyBuilder.indent();
    bodyBuilder.appendFormalLine(String.format(".body(%s.singletonMap(\"message\", errorMessage));", getNameOfJavaType(new JavaType(Collections.class))));
    bodyBuilder.indentRemove();
    MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(getId(), Modifier.PUBLIC, methodName, SpringJavaType.RESPONSE_ENTITY, parameterTypes, parameterNames, bodyBuilder);
    methodBuilder.setThrowsTypes(throwTypes);
    methodBuilder.setAnnotations(annotationTypes);
    return methodBuilder.build();
}
Also used : AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) ArrayList(java.util.ArrayList) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) SpringJavaType(org.springframework.roo.model.SpringJavaType) JavaType(org.springframework.roo.model.JavaType) JdkJavaType(org.springframework.roo.model.JdkJavaType) MethodMetadataBuilder(org.springframework.roo.classpath.details.MethodMetadataBuilder) MethodMetadata(org.springframework.roo.classpath.details.MethodMetadata) InvocableMemberBodyBuilder(org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder) Collections(java.util.Collections) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)

Example 9 with JavaType

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

the class ExceptionsMetadata method getViewHandlerExceptionMethod.

private MethodMetadata getViewHandlerExceptionMethod(JavaType exception, String errorView) {
    // Define annotations
    List<AnnotationMetadataBuilder> annotationTypes = new ArrayList<AnnotationMetadataBuilder>();
    AnnotationMetadataBuilder exceptionHandler = new AnnotationMetadataBuilder(SpringJavaType.EXCEPTION_HANDLER);
    exceptionHandler.addClassAttribute("value", exception);
    annotationTypes.add(exceptionHandler);
    // Define method parameter types
    List<AnnotatedJavaType> parameterTypes = new ArrayList<AnnotatedJavaType>();
    parameterTypes.add(AnnotatedJavaType.convertFromJavaType(HTTP_SERVLET_REQUEST));
    parameterTypes.add(AnnotatedJavaType.convertFromJavaType(JdkJavaType.EXCEPTION));
    parameterTypes.add(AnnotatedJavaType.convertFromJavaType(JdkJavaType.LOCALE));
    // Define method parameter names
    List<JavaSymbolName> parameterNames = new ArrayList<JavaSymbolName>();
    parameterNames.add(new JavaSymbolName(HTTP_SERVLET_REQUEST_PARAM_NAME));
    parameterNames.add(new JavaSymbolName(EXCEPTION_PARAM_NAME));
    parameterNames.add(new JavaSymbolName(LOCALE_PARAM_NAME));
    // Define method name
    JavaSymbolName methodName = new JavaSymbolName("handle".concat(exception.getSimpleTypeName()));
    MethodMetadata existingMethod = getGovernorMethod(methodName);
    if (existingMethod != null) {
        return existingMethod;
    }
    // Set throws types
    List<JavaType> throwTypes = new ArrayList<JavaType>();
    throwTypes.add(JdkJavaType.EXCEPTION);
    // Generate body
    InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
    // if (AnnotationUtils.findAnnotation(e.getClass(), ResponseStatus.class) != null) {
    bodyBuilder.appendFormalLine(String.format("if (%s.findAnnotation(e.getClass(), %s.class) != null) {", getNameOfJavaType(SpringJavaType.ANNOTATION_UTILS), getNameOfJavaType(SpringJavaType.RESPONSE_STATUS)));
    bodyBuilder.indent();
    // throw e;
    bodyBuilder.appendFormalLine("throw e;");
    bodyBuilder.indentRemove();
    bodyBuilder.appendFormalLine("}");
    // String errorMessage =
    bodyBuilder.appendFormalLine(String.format("%s errorMessage = ", getNameOfJavaType(JavaType.STRING)));
    // this.messageSource.getMessage("label_my_exception", null, locale);
    bodyBuilder.indent();
    bodyBuilder.appendFormalLine(String.format("this.%s.getMessage(\"label_%s\", null, locale);", this.messageSourceField.getFieldName(), exception.getSimpleTypeName().toLowerCase()));
    bodyBuilder.indentRemove();
    // LOG.error(errorMessage, e);
    bodyBuilder.appendFormalLine("LOG.error(errorMessage, e);");
    // ModelAndView mav = new ModelAndView();
    bodyBuilder.appendFormalLine(String.format("%s mav = new %s();", getNameOfJavaType(SpringJavaType.MODEL_AND_VIEW), getNameOfJavaType(SpringJavaType.MODEL_AND_VIEW)));
    // mav.addObject("exception", e);
    bodyBuilder.appendFormalLine("mav.addObject(\"exception\", e);");
    // mav.addObject("message", errorMessage);
    bodyBuilder.appendFormalLine("mav.addObject(\"message\", errorMessage);");
    // mav.addObject("url", req.getRequestURL());
    bodyBuilder.appendFormalLine("mav.addObject(\"url\", req.getRequestURL());");
    // mav.setViewName("errorView");
    bodyBuilder.appendFormalLine(String.format("mav.setViewName(\"%s\");", errorView));
    // return mav;
    bodyBuilder.appendFormalLine("return mav;");
    MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(getId(), Modifier.PUBLIC, methodName, SpringJavaType.MODEL_AND_VIEW, parameterTypes, parameterNames, bodyBuilder);
    methodBuilder.setThrowsTypes(throwTypes);
    methodBuilder.setAnnotations(annotationTypes);
    return methodBuilder.build();
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) SpringJavaType(org.springframework.roo.model.SpringJavaType) JavaType(org.springframework.roo.model.JavaType) JdkJavaType(org.springframework.roo.model.JdkJavaType) 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) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)

Example 10 with JavaType

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

the class ExceptionsMetadataProviderImpl method getGovernorPhysicalTypeIdentifier.

@Override
protected String getGovernorPhysicalTypeIdentifier(String metadataIdentificationString) {
    final JavaType javaType = ExceptionsMetadata.getJavaType(metadataIdentificationString);
    final LogicalPath path = ExceptionsMetadata.getPath(metadataIdentificationString);
    return PhysicalTypeIdentifier.createIdentifier(javaType, path);
}
Also used : RooJavaType(org.springframework.roo.model.RooJavaType) JavaType(org.springframework.roo.model.JavaType) LogicalPath(org.springframework.roo.project.LogicalPath)

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