Search in sources :

Example 1 with JavaSymbolName

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

the class EntityDeserializerMetadata method getDeserializeMethod.

private MethodMetadata getDeserializeMethod() {
    // Define methodName
    final JavaSymbolName methodName = new JavaSymbolName("deserializeObject");
    // Adding parameter types
    List<AnnotatedJavaType> parameterTypes = new ArrayList<AnnotatedJavaType>();
    parameterTypes.add(AnnotatedJavaType.convertFromJavaType(JSON_PARSER));
    parameterTypes.add(AnnotatedJavaType.convertFromJavaType(DESERIALIZATION_CONTEXT));
    parameterTypes.add(AnnotatedJavaType.convertFromJavaType(OBJECT_CODEC));
    parameterTypes.add(AnnotatedJavaType.convertFromJavaType(JSON_NODE));
    MethodMetadata existingMethod = getGovernorMethod(methodName, AnnotatedJavaType.convertFromAnnotatedJavaTypes(parameterTypes));
    if (existingMethod != null) {
        return existingMethod;
    }
    // Adding parameter names
    final List<JavaSymbolName> parameterNames = new ArrayList<JavaSymbolName>();
    parameterNames.add(new JavaSymbolName("jsonParser"));
    parameterNames.add(new JavaSymbolName("context"));
    parameterNames.add(new JavaSymbolName("codec"));
    parameterNames.add(new JavaSymbolName("tree"));
    // Adding annotations
    final List<AnnotationMetadataBuilder> annotations = new ArrayList<AnnotationMetadataBuilder>();
    // Generate body
    InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
    final JavaType idType = getTypeToUseAsIdentifier(entityMetadata.getCurrentIndentifierField().getFieldType());
    // String idText = tree.asText();
    bodyBuilder.appendFormalLine("String idText = tree.asText();");
    // Long id = conversionService.convert(idText, Long.class);
    bodyBuilder.appendFormalLine("%s %s = %s.convert(idText, %s.class);", getNameOfJavaType(idType), entityMetadata.getCurrentIndentifierField().getFieldName(), conversionServiceField.getFieldName(), getNameOfJavaType(idType));
    final String entityItemName = StringUtils.uncapitalize(entityMetadata.getDestination().getSimpleTypeName());
    // Product product = productSercie.findOne(id);
    bodyBuilder.appendFormalLine("%s %s = %s.%s(%s);", getNameOfJavaType(entityMetadata.getDestination()), entityItemName, serviceField.getFieldName(), serviceMetadata.getCurrentFindOneMethod().getMethodName(), entityMetadata.getCurrentIndentifierField().getFieldName());
    // if (product == null) {
    bodyBuilder.appendFormalLine("if (%s == null) {", entityItemName);
    // throw new NotFoundException("Product not found");
    bodyBuilder.indent();
    bodyBuilder.appendFormalLine("throw new %s(\"%s not found\");", getNameOfJavaType(SpringletsJavaType.SPRINGLETS_NOT_FOUND_EXCEPTION), entityMetadata.getDestination().getSimpleTypeName());
    bodyBuilder.indentRemove();
    // }
    bodyBuilder.appendFormalLine("}");
    // return product;
    bodyBuilder.appendFormalLine("return %s;", entityItemName);
    MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(getId(), Modifier.PUBLIC, methodName, entityMetadata.getDestination(), parameterTypes, parameterNames, bodyBuilder);
    methodBuilder.setAnnotations(annotations);
    methodBuilder.addThrowsType(IO_EXCEPTION);
    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) 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) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)

Example 2 with JavaSymbolName

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

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

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

the class ExceptionsMetadata method getLoggerField.

public FieldMetadata getLoggerField() {
    InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
    bodyBuilder.append(getNameOfJavaType(LOGGER_FACTORY_JAVA_TYPE));
    bodyBuilder.append(".getLogger(");
    bodyBuilder.append(governorPhysicalTypeMetadata.getType().getSimpleTypeName());
    bodyBuilder.append(".class)");
    final String initializer = bodyBuilder.getOutput();
    FieldMetadataBuilder field = new FieldMetadataBuilder(getId(), Modifier.PRIVATE | Modifier.STATIC | Modifier.FINAL, new JavaSymbolName("LOG"), LOGGER_JAVA_TYPE, initializer);
    return field.build();
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) InvocableMemberBodyBuilder(org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder) FieldMetadataBuilder(org.springframework.roo.classpath.details.FieldMetadataBuilder)

Example 5 with JavaSymbolName

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

the class ExceptionsOperationsImpl method addHandlersAnnotations.

/**
 * Generates {@link RooExceptionHandlers} and {@link RooExceptionHandler} annotations
 * and adds or updates it on specified class.
 *
 * @param exception
 * @param targetClass
 * @param errorView
 */
private void addHandlersAnnotations(JavaType exception, JavaType targetClass, String errorView) {
    Validate.notNull(targetClass, "Target class is required to add @RooExceptionHandlers annotation");
    // Create @RooExceptionHandler Annotation
    final AnnotationMetadataBuilder exceptionHandlerAnnotationBuilder = new AnnotationMetadataBuilder(RooJavaType.ROO_EXCEPTION_HANDLER);
    final List<AnnotationAttributeValue<?>> exceptionHandlerAnnotationAttributes = new ArrayList<AnnotationAttributeValue<?>>();
    exceptionHandlerAnnotationAttributes.add(new ClassAttributeValue(new JavaSymbolName(EXCEPTION), exception));
    if (errorView != null) {
        exceptionHandlerAnnotationAttributes.add(new StringAttributeValue(new JavaSymbolName(ERROR_VIEW), errorView));
    }
    exceptionHandlerAnnotationBuilder.setAttributes(exceptionHandlerAnnotationAttributes);
    // Check if container annotation already exists
    ClassOrInterfaceTypeDetails typeDetails = typeLocationService.getTypeDetails(targetClass);
    ClassOrInterfaceTypeDetailsBuilder typeDetailsBuilder = new ClassOrInterfaceTypeDetailsBuilder(typeDetails);
    AnnotationMetadata exceptionHandlersAnnotation = typeDetails.getAnnotation(RooJavaType.ROO_EXCEPTION_HANDLERS);
    AnnotationMetadataBuilder exceptionHandlersAnnotationBuilder = null;
    if (exceptionHandlersAnnotation != null) {
        exceptionHandlersAnnotationBuilder = new AnnotationMetadataBuilder(exceptionHandlersAnnotation);
    } else {
        exceptionHandlersAnnotationBuilder = new AnnotationMetadataBuilder(RooJavaType.ROO_EXCEPTION_HANDLERS);
    }
    Validate.notNull(exceptionHandlersAnnotationBuilder);
    // Add @RooExceptionHandler annotation into @RooExceptionHandlers
    final List<NestedAnnotationAttributeValue> exceptionHandlersArrayValues = new ArrayList<NestedAnnotationAttributeValue>();
    exceptionHandlersArrayValues.add(new NestedAnnotationAttributeValue(new JavaSymbolName(VALUE), exceptionHandlerAnnotationBuilder.build()));
    final List<AnnotationAttributeValue<?>> attributeValues = new ArrayList<AnnotationAttributeValue<?>>();
    attributeValues.add(new ArrayAttributeValue<NestedAnnotationAttributeValue>(new JavaSymbolName(VALUE), exceptionHandlersArrayValues));
    if (exceptionHandlersAnnotation == null) {
        // Add new @RooExceptionHandlers annotation with given values
        exceptionHandlersAnnotationBuilder.setAttributes(attributeValues);
        typeDetailsBuilder.addAnnotation(exceptionHandlersAnnotationBuilder.build());
    } else {
        // Get current annotation values from @RooExceptionHandlers annotation
        AnnotationAttributeValue<?> currentHandlers = exceptionHandlersAnnotation.getAttribute(VALUE);
        if (currentHandlers != null) {
            List<?> values = (List<?>) currentHandlers.getValue();
            Iterator<?> it = values.iterator();
            while (it.hasNext()) {
                NestedAnnotationAttributeValue handler = (NestedAnnotationAttributeValue) it.next();
                if (handler.getValue() != null) {
                    // Check if there is a @RooExceptionHandlers with same 'exception' value
                    if (exceptionHandlerAnnotationBuilder.build().getAttribute(EXCEPTION).getValue().equals(handler.getValue().getAttribute(EXCEPTION).getValue())) {
                        LOGGER.warning(String.format("There is already a handler for exception %s in class %s", exception.getSimpleTypeName(), targetClass.getSimpleTypeName()));
                        return;
                    }
                    exceptionHandlersArrayValues.add(handler);
                }
            }
        }
        // Add found values
        attributeValues.add(new ArrayAttributeValue<NestedAnnotationAttributeValue>(new JavaSymbolName(VALUE), exceptionHandlersArrayValues));
        exceptionHandlersAnnotationBuilder.setAttributes(attributeValues);
        // Update annotation
        typeDetailsBuilder.updateTypeAnnotation(exceptionHandlersAnnotationBuilder.build());
    }
    // Write to disk
    getTypeManagementService().createOrUpdateTypeOnDisk(typeDetailsBuilder.build());
}
Also used : AnnotationAttributeValue(org.springframework.roo.classpath.details.annotations.AnnotationAttributeValue) NestedAnnotationAttributeValue(org.springframework.roo.classpath.details.annotations.NestedAnnotationAttributeValue) ClassAttributeValue(org.springframework.roo.classpath.details.annotations.ClassAttributeValue) ArrayList(java.util.ArrayList) AnnotationMetadata(org.springframework.roo.classpath.details.annotations.AnnotationMetadata) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) ClassOrInterfaceTypeDetailsBuilder(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder) ArrayList(java.util.ArrayList) List(java.util.List) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) StringAttributeValue(org.springframework.roo.classpath.details.annotations.StringAttributeValue) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder) NestedAnnotationAttributeValue(org.springframework.roo.classpath.details.annotations.NestedAnnotationAttributeValue)

Aggregations

JavaSymbolName (org.springframework.roo.model.JavaSymbolName)317 ArrayList (java.util.ArrayList)186 AnnotationMetadataBuilder (org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)155 MethodMetadataBuilder (org.springframework.roo.classpath.details.MethodMetadataBuilder)143 AnnotatedJavaType (org.springframework.roo.classpath.details.annotations.AnnotatedJavaType)142 JavaType (org.springframework.roo.model.JavaType)133 InvocableMemberBodyBuilder (org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder)121 MethodMetadata (org.springframework.roo.classpath.details.MethodMetadata)119 SpringJavaType (org.springframework.roo.model.SpringJavaType)61 FieldMetadata (org.springframework.roo.classpath.details.FieldMetadata)59 JdkJavaType (org.springframework.roo.model.JdkJavaType)59 FieldMetadataBuilder (org.springframework.roo.classpath.details.FieldMetadataBuilder)48 AnnotationMetadata (org.springframework.roo.classpath.details.annotations.AnnotationMetadata)47 SpringletsJavaType (org.springframework.roo.model.SpringletsJavaType)45 ClassOrInterfaceTypeDetails (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails)39 AnnotationAttributeValue (org.springframework.roo.classpath.details.annotations.AnnotationAttributeValue)36 ClassOrInterfaceTypeDetailsBuilder (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder)29 Jsr303JavaType (org.springframework.roo.model.Jsr303JavaType)29 StringAttributeValue (org.springframework.roo.classpath.details.annotations.StringAttributeValue)27 EnumDetails (org.springframework.roo.model.EnumDetails)25