Skip to content

Commit

Permalink
perf($VarAndVal): synchronize codes - ExceptionControllerAdvice.java,…
Browse files Browse the repository at this point in the history
… MethodArgumentValidationAspect.java
  • Loading branch information
johnnymillergh committed May 8, 2020
1 parent ff54ca5 commit bc5b365
Show file tree
Hide file tree
Showing 29 changed files with 163 additions and 157 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
@Component
public class Constants {
public Constants(ProjectProperty projectProperty) {
REDIS_JWT_KEY_PREFIX = projectProperty.getParentArtifactId() + ":jwt:";
REDIS_JWT_KEY_PREFIX = String.format("%s:jwt:", projectProperty.getParentArtifactId());
log.info("Initiated 'REDIS_JWT_KEY_PREFIX': {}", REDIS_JWT_KEY_PREFIX);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.jmsoftware.common.exception.BaseException;
import com.jmsoftware.common.util.RequestUtil;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.springframework.context.support.DefaultMessageSourceResolvable;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.validation.BindException;
Expand Down Expand Up @@ -124,18 +125,17 @@ public ResponseBodyBean<Object> handleException(HttpServletRequest request,
*/
private String getFieldErrorMessageFromException(MethodArgumentNotValidException exception) {
try {
DefaultMessageSourceResolvable firstErrorField =
val firstErrorField =
(DefaultMessageSourceResolvable) Objects.requireNonNull(exception.getBindingResult()
.getAllErrors()
.get(0)
.getArguments())[0];
String firstErrorFieldName = firstErrorField.getDefaultMessage();
String firstErrorFieldMessage = exception.getBindingResult().getAllErrors().get(0).getDefaultMessage();
return firstErrorFieldName + " " + firstErrorFieldMessage;
val firstErrorFieldName = firstErrorField.getDefaultMessage();
val firstErrorFieldMessage = exception.getBindingResult().getAllErrors().get(0).getDefaultMessage();
return String.format("%s %s", firstErrorFieldName, firstErrorFieldMessage);
} catch (Exception e) {
log.error("Exception occurred when get field error message from exception. Exception message: {}",
e.getMessage(),
e);
e.getMessage(), e);
return HttpStatus.INVALID_PARAM.getMessage();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
Expand Down Expand Up @@ -41,9 +42,9 @@ public class MethodArgumentValidationAspect {
private final Validator validator;

public MethodArgumentValidationAspect() {
var validatorFactory = Validation.buildDefaultValidatorFactory();
val validatorFactory = Validation.buildDefaultValidatorFactory();
this.validator = validatorFactory.getValidator();
log.info("Validator for {} has been initiated.", this.getClass().getSimpleName());
log.info("The validator for {} has been initiated.", this.getClass().getSimpleName());
}

/**
Expand Down Expand Up @@ -83,31 +84,31 @@ public void beforeMethodHandleArgument(JoinPoint joinPoint) {
@Around("validateMethodArgumentPointcut()")
public Object aroundMethodHandleArgument(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
log.info("======= METHOD'S ARGUMENT VALIDATION START =======");
var args = proceedingJoinPoint.getArgs();
var signature = (MethodSignature) proceedingJoinPoint.getSignature();
var parameterAnnotations = signature.getMethod().getParameterAnnotations();
val args = proceedingJoinPoint.getArgs();
val signature = (MethodSignature) proceedingJoinPoint.getSignature();
val parameterAnnotations = signature.getMethod().getParameterAnnotations();
// argumentIndexes is the array list that stores the index of argument we need to validate (the argument
// annotated by `@Valid`)
var argumentIndexListThatNeedsToBeValidated = new LinkedList<Integer>();
for (var parameterAnnotation : parameterAnnotations) {
int paramIndex = ArrayUtil.indexOf(parameterAnnotations, parameterAnnotation);
for (var annotation : parameterAnnotation) {
val argumentIndexListThatNeedsToBeValidated = new LinkedList<Integer>();
for (val parameterAnnotation : parameterAnnotations) {
val paramIndex = ArrayUtil.indexOf(parameterAnnotations, parameterAnnotation);
for (val annotation : parameterAnnotation) {
if (annotation instanceof Valid) {
argumentIndexListThatNeedsToBeValidated.add(paramIndex);
}
}
}
var errorMessageList = new LinkedList<String>();
for (var index : argumentIndexListThatNeedsToBeValidated) {
var constraintViolationSet = validator.validate(args[index]);
val errorMessageList = new LinkedList<String>();
for (val index : argumentIndexListThatNeedsToBeValidated) {
val constraintViolationSet = validator.validate(args[index]);
if (CollUtil.isNotEmpty(constraintViolationSet)) {
var errorMessage = String.format("Argument validation failed: %s",
val errorMessage = String.format("Argument validation failed: %s",
getAllFieldErrorMessage(constraintViolationSet));
errorMessageList.add(errorMessage);
}
}
if (CollUtil.isNotEmpty(errorMessageList)) {
var joinedErrorMessage = StrUtil.join(", ", errorMessageList);
val joinedErrorMessage = StrUtil.join(", ", errorMessageList);
log.info("Method : {}#{}", proceedingJoinPoint.getSignature().getDeclaringTypeName(),
proceedingJoinPoint.getSignature().getName());
log.info("Argument : {}", args);
Expand Down Expand Up @@ -149,8 +150,8 @@ public void afterThrowingException(JoinPoint joinPoint, Exception e) {
* @return the all field error message
*/
private String getAllFieldErrorMessage(Set<ConstraintViolation<Object>> constraintViolationSet) {
var allErrorMessageList = new LinkedList<String>();
for (var constraintViolation : constraintViolationSet) {
val allErrorMessageList = new LinkedList<String>();
for (val constraintViolation : constraintViolationSet) {
allErrorMessageList.add(String.format("invalid field: %s, %s", constraintViolation.getPropertyPath(),
constraintViolation.getMessage()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.jmsoftware.common.exception.BaseException;
import com.jmsoftware.common.util.RequestUtil;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.springframework.context.support.DefaultMessageSourceResolvable;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.validation.BindException;
Expand Down Expand Up @@ -124,18 +125,17 @@ public ResponseBodyBean<Object> handleException(HttpServletRequest request,
*/
private String getFieldErrorMessageFromException(MethodArgumentNotValidException exception) {
try {
DefaultMessageSourceResolvable firstErrorField =
val firstErrorField =
(DefaultMessageSourceResolvable) Objects.requireNonNull(exception.getBindingResult()
.getAllErrors()
.get(0)
.getArguments())[0];
String firstErrorFieldName = firstErrorField.getDefaultMessage();
String firstErrorFieldMessage = exception.getBindingResult().getAllErrors().get(0).getDefaultMessage();
return firstErrorFieldName + " " + firstErrorFieldMessage;
val firstErrorFieldName = firstErrorField.getDefaultMessage();
val firstErrorFieldMessage = exception.getBindingResult().getAllErrors().get(0).getDefaultMessage();
return String.format("%s %s", firstErrorFieldName, firstErrorFieldMessage);
} catch (Exception e) {
log.error("Exception occurred when get field error message from exception. Exception message: {}",
e.getMessage(),
e);
e.getMessage(), e);
return HttpStatus.INVALID_PARAM.getMessage();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
Expand Down Expand Up @@ -41,9 +42,9 @@ public class MethodArgumentValidationAspect {
private final Validator validator;

public MethodArgumentValidationAspect() {
var validatorFactory = Validation.buildDefaultValidatorFactory();
val validatorFactory = Validation.buildDefaultValidatorFactory();
this.validator = validatorFactory.getValidator();
log.info("Validator for {} has been initiated.", this.getClass().getSimpleName());
log.info("The validator for {} has been initiated.", this.getClass().getSimpleName());
}

/**
Expand Down Expand Up @@ -83,31 +84,31 @@ public void beforeMethodHandleArgument(JoinPoint joinPoint) {
@Around("validateMethodArgumentPointcut()")
public Object aroundMethodHandleArgument(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
log.info("======= METHOD'S ARGUMENT VALIDATION START =======");
var args = proceedingJoinPoint.getArgs();
var signature = (MethodSignature) proceedingJoinPoint.getSignature();
var parameterAnnotations = signature.getMethod().getParameterAnnotations();
val args = proceedingJoinPoint.getArgs();
val signature = (MethodSignature) proceedingJoinPoint.getSignature();
val parameterAnnotations = signature.getMethod().getParameterAnnotations();
// argumentIndexes is the array list that stores the index of argument we need to validate (the argument
// annotated by `@Valid`)
var argumentIndexListThatNeedsToBeValidated = new LinkedList<Integer>();
for (var parameterAnnotation : parameterAnnotations) {
int paramIndex = ArrayUtil.indexOf(parameterAnnotations, parameterAnnotation);
for (var annotation : parameterAnnotation) {
val argumentIndexListThatNeedsToBeValidated = new LinkedList<Integer>();
for (val parameterAnnotation : parameterAnnotations) {
val paramIndex = ArrayUtil.indexOf(parameterAnnotations, parameterAnnotation);
for (val annotation : parameterAnnotation) {
if (annotation instanceof Valid) {
argumentIndexListThatNeedsToBeValidated.add(paramIndex);
}
}
}
var errorMessageList = new LinkedList<String>();
for (var index : argumentIndexListThatNeedsToBeValidated) {
var constraintViolationSet = validator.validate(args[index]);
val errorMessageList = new LinkedList<String>();
for (val index : argumentIndexListThatNeedsToBeValidated) {
val constraintViolationSet = validator.validate(args[index]);
if (CollUtil.isNotEmpty(constraintViolationSet)) {
var errorMessage = String.format("Argument validation failed: %s",
val errorMessage = String.format("Argument validation failed: %s",
getAllFieldErrorMessage(constraintViolationSet));
errorMessageList.add(errorMessage);
}
}
if (CollUtil.isNotEmpty(errorMessageList)) {
var joinedErrorMessage = StrUtil.join(", ", errorMessageList);
val joinedErrorMessage = StrUtil.join(", ", errorMessageList);
log.info("Method : {}#{}", proceedingJoinPoint.getSignature().getDeclaringTypeName(),
proceedingJoinPoint.getSignature().getName());
log.info("Argument : {}", args);
Expand Down Expand Up @@ -149,8 +150,8 @@ public void afterThrowingException(JoinPoint joinPoint, Exception e) {
* @return the all field error message
*/
private String getAllFieldErrorMessage(Set<ConstraintViolation<Object>> constraintViolationSet) {
var allErrorMessageList = new LinkedList<String>();
for (var constraintViolation : constraintViolationSet) {
val allErrorMessageList = new LinkedList<String>();
for (val constraintViolation : constraintViolationSet) {
allErrorMessageList.add(String.format("invalid field: %s, %s", constraintViolation.getPropertyPath(),
constraintViolation.getMessage()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
Expand Down Expand Up @@ -41,9 +42,9 @@ public class MethodArgumentValidationAspect {
private final Validator validator;

public MethodArgumentValidationAspect() {
var validatorFactory = Validation.buildDefaultValidatorFactory();
val validatorFactory = Validation.buildDefaultValidatorFactory();
this.validator = validatorFactory.getValidator();
log.info("Validator for {} has been initiated.", this.getClass().getSimpleName());
log.info("The validator for {} has been initiated.", this.getClass().getSimpleName());
}

/**
Expand Down Expand Up @@ -83,31 +84,31 @@ public void beforeMethodHandleArgument(JoinPoint joinPoint) {
@Around("validateMethodArgumentPointcut()")
public Object aroundMethodHandleArgument(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
log.info("======= METHOD'S ARGUMENT VALIDATION START =======");
var args = proceedingJoinPoint.getArgs();
var signature = (MethodSignature) proceedingJoinPoint.getSignature();
var parameterAnnotations = signature.getMethod().getParameterAnnotations();
val args = proceedingJoinPoint.getArgs();
val signature = (MethodSignature) proceedingJoinPoint.getSignature();
val parameterAnnotations = signature.getMethod().getParameterAnnotations();
// argumentIndexes is the array list that stores the index of argument we need to validate (the argument
// annotated by `@Valid`)
var argumentIndexListThatNeedsToBeValidated = new LinkedList<Integer>();
for (var parameterAnnotation : parameterAnnotations) {
int paramIndex = ArrayUtil.indexOf(parameterAnnotations, parameterAnnotation);
for (var annotation : parameterAnnotation) {
val argumentIndexListThatNeedsToBeValidated = new LinkedList<Integer>();
for (val parameterAnnotation : parameterAnnotations) {
val paramIndex = ArrayUtil.indexOf(parameterAnnotations, parameterAnnotation);
for (val annotation : parameterAnnotation) {
if (annotation instanceof Valid) {
argumentIndexListThatNeedsToBeValidated.add(paramIndex);
}
}
}
var errorMessageList = new LinkedList<String>();
for (var index : argumentIndexListThatNeedsToBeValidated) {
var constraintViolationSet = validator.validate(args[index]);
val errorMessageList = new LinkedList<String>();
for (val index : argumentIndexListThatNeedsToBeValidated) {
val constraintViolationSet = validator.validate(args[index]);
if (CollUtil.isNotEmpty(constraintViolationSet)) {
var errorMessage = String.format("Argument validation failed: %s",
val errorMessage = String.format("Argument validation failed: %s",
getAllFieldErrorMessage(constraintViolationSet));
errorMessageList.add(errorMessage);
}
}
if (CollUtil.isNotEmpty(errorMessageList)) {
var joinedErrorMessage = StrUtil.join(", ", errorMessageList);
val joinedErrorMessage = StrUtil.join(", ", errorMessageList);
log.info("Method : {}#{}", proceedingJoinPoint.getSignature().getDeclaringTypeName(),
proceedingJoinPoint.getSignature().getName());
log.info("Argument : {}", args);
Expand Down Expand Up @@ -149,8 +150,8 @@ public void afterThrowingException(JoinPoint joinPoint, Exception e) {
* @return the all field error message
*/
private String getAllFieldErrorMessage(Set<ConstraintViolation<Object>> constraintViolationSet) {
var allErrorMessageList = new LinkedList<String>();
for (var constraintViolation : constraintViolationSet) {
val allErrorMessageList = new LinkedList<String>();
for (val constraintViolation : constraintViolationSet) {
allErrorMessageList.add(String.format("invalid field: %s, %s", constraintViolation.getPropertyPath(),
constraintViolation.getMessage()));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.jmsoftware.exercisemis;
package com.jmsoftware.musclemis;

import com.jmsoftware.exercisemis.universal.configuration.ProjectProperty;
import com.jmsoftware.exercisemis.universal.configuration.ServerConfiguration;
import com.jmsoftware.musclemis.universal.configuration.ProjectProperty;
import com.jmsoftware.musclemis.universal.configuration.ServerConfiguration;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.jmsoftware.exercisemis.universal.aspect;
package com.jmsoftware.musclemis.universal.aspect;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.json.JSONUtil;
Expand All @@ -7,6 +7,7 @@
import com.jmsoftware.common.exception.BaseException;
import com.jmsoftware.common.util.RequestUtil;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.springframework.context.support.DefaultMessageSourceResolvable;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.validation.BindException;
Expand Down Expand Up @@ -124,18 +125,17 @@ public ResponseBodyBean<Object> handleException(HttpServletRequest request,
*/
private String getFieldErrorMessageFromException(MethodArgumentNotValidException exception) {
try {
DefaultMessageSourceResolvable firstErrorField =
val firstErrorField =
(DefaultMessageSourceResolvable) Objects.requireNonNull(exception.getBindingResult()
.getAllErrors()
.get(0)
.getArguments())[0];
String firstErrorFieldName = firstErrorField.getDefaultMessage();
String firstErrorFieldMessage = exception.getBindingResult().getAllErrors().get(0).getDefaultMessage();
return firstErrorFieldName + " " + firstErrorFieldMessage;
val firstErrorFieldName = firstErrorField.getDefaultMessage();
val firstErrorFieldMessage = exception.getBindingResult().getAllErrors().get(0).getDefaultMessage();
return String.format("%s %s", firstErrorFieldName, firstErrorFieldMessage);
} catch (Exception e) {
log.error("Exception occurred when get field error message from exception. Exception message: {}",
e.getMessage(),
e);
e.getMessage(), e);
return HttpStatus.INVALID_PARAM.getMessage();
}
}
Expand Down
Loading

0 comments on commit bc5b365

Please sign in to comment.