【代码优化】增强 JDK17、JDK8 之间的兼容性

This commit is contained in:
YunaiV
2025-08-29 13:21:21 +08:00
parent ba390c67f8
commit 22d2de9dd2
8 changed files with 30 additions and 16 deletions

View File

@@ -12,8 +12,10 @@ import org.springframework.beans.factory.config.BeanPostProcessor;
public class TenantRabbitMQInitializer implements BeanPostProcessor { public class TenantRabbitMQInitializer implements BeanPostProcessor {
@Override @Override
@SuppressWarnings("PatternVariableCanBeUsed")
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
if (bean instanceof RabbitTemplate rabbitTemplate) { if (bean instanceof RabbitTemplate) {
RabbitTemplate rabbitTemplate = (RabbitTemplate) bean;
rabbitTemplate.addBeforePublishPostProcessors(new TenantRabbitMQMessagePostProcessor()); rabbitTemplate.addBeforePublishPostProcessors(new TenantRabbitMQMessagePostProcessor());
} }
return bean; return bean;

View File

@@ -17,10 +17,13 @@ import org.springframework.beans.factory.config.BeanPostProcessor;
public class TenantRocketMQInitializer implements BeanPostProcessor { public class TenantRocketMQInitializer implements BeanPostProcessor {
@Override @Override
@SuppressWarnings("PatternVariableCanBeUsed")
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
if (bean instanceof DefaultRocketMQListenerContainer container) { if (bean instanceof DefaultRocketMQListenerContainer) {
DefaultRocketMQListenerContainer container = (DefaultRocketMQListenerContainer) bean;
initTenantConsumer(container.getConsumer()); initTenantConsumer(container.getConsumer());
} else if (bean instanceof RocketMQTemplate template) { } else if (bean instanceof RocketMQTemplate) {
RocketMQTemplate template = (RocketMQTemplate) bean;
initTenantProducer(template.getProducer()); initTenantProducer(template.getProducer());
} }
return bean; return bean;

View File

@@ -21,15 +21,18 @@ public class YudaoAsyncAutoConfiguration {
return new BeanPostProcessor() { return new BeanPostProcessor() {
@Override @Override
@SuppressWarnings("PatternVariableCanBeUsed")
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
// 处理 ThreadPoolTaskExecutor // 处理 ThreadPoolTaskExecutor
if (bean instanceof ThreadPoolTaskExecutor executor) { if (bean instanceof ThreadPoolTaskExecutor) {
ThreadPoolTaskExecutor executor = (ThreadPoolTaskExecutor) bean;
executor.setTaskDecorator(TtlRunnable::get); executor.setTaskDecorator(TtlRunnable::get);
return executor; return executor;
} }
// 处理 SimpleAsyncTaskExecutor // 处理 SimpleAsyncTaskExecutor
// 参考 https://t.zsxq.com/CBoks 增加 // 参考 https://t.zsxq.com/CBoks 增加
if (bean instanceof SimpleAsyncTaskExecutor executor) { if (bean instanceof SimpleAsyncTaskExecutor) {
SimpleAsyncTaskExecutor executor = (SimpleAsyncTaskExecutor) bean;
executor.setTaskDecorator(TtlRunnable::get); executor.setTaskDecorator(TtlRunnable::get);
return executor; return executor;
} }

View File

@@ -18,8 +18,10 @@ import java.util.Objects;
public class DefaultDBFieldHandler implements MetaObjectHandler { public class DefaultDBFieldHandler implements MetaObjectHandler {
@Override @Override
@SuppressWarnings("PatternVariableCanBeUsed")
public void insertFill(MetaObject metaObject) { public void insertFill(MetaObject metaObject) {
if (Objects.nonNull(metaObject) && metaObject.getOriginalObject() instanceof BaseDO baseDO) { if (Objects.nonNull(metaObject) && metaObject.getOriginalObject() instanceof BaseDO) {
BaseDO baseDO = (BaseDO) metaObject.getOriginalObject();
LocalDateTime current = LocalDateTime.now(); LocalDateTime current = LocalDateTime.now();
// 创建时间为空,则以当前时间为插入时间 // 创建时间为空,则以当前时间为插入时间

View File

@@ -182,9 +182,11 @@ public class GlobalExceptionHandler {
* 例如说,接口上设置了 @RequestBody 实体中 xx 属性类型为 Integer结果传递 xx 参数类型为 String * 例如说,接口上设置了 @RequestBody 实体中 xx 属性类型为 Integer结果传递 xx 参数类型为 String
*/ */
@ExceptionHandler(HttpMessageNotReadableException.class) @ExceptionHandler(HttpMessageNotReadableException.class)
@SuppressWarnings("PatternVariableCanBeUsed")
public CommonResult<?> methodArgumentTypeInvalidFormatExceptionHandler(HttpMessageNotReadableException ex) { public CommonResult<?> methodArgumentTypeInvalidFormatExceptionHandler(HttpMessageNotReadableException ex) {
log.warn("[methodArgumentTypeInvalidFormatExceptionHandler]", ex); log.warn("[methodArgumentTypeInvalidFormatExceptionHandler]", ex);
if (ex.getCause() instanceof InvalidFormatException invalidFormatException) { if (ex.getCause() instanceof InvalidFormatException) {
InvalidFormatException invalidFormatException = (InvalidFormatException) ex.getCause();
return CommonResult.error(BAD_REQUEST.getCode(), String.format("请求参数类型错误:%s", invalidFormatException.getValue())); return CommonResult.error(BAD_REQUEST.getCode(), String.format("请求参数类型错误:%s", invalidFormatException.getValue()));
} }
if (StrUtil.startWith(ex.getMessage(), "Required request body is missing")) { if (StrUtil.startWith(ex.getMessage(), "Required request body is missing")) {

View File

@@ -144,11 +144,13 @@ public class WebFrameworkUtils {
return (CommonResult<?>) request.getAttribute(REQUEST_ATTRIBUTE_COMMON_RESULT); return (CommonResult<?>) request.getAttribute(REQUEST_ATTRIBUTE_COMMON_RESULT);
} }
@SuppressWarnings("PatternVariableCanBeUsed")
public static HttpServletRequest getRequest() { public static HttpServletRequest getRequest() {
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes(); RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
if (!(requestAttributes instanceof ServletRequestAttributes servletRequestAttributes)) { if (!(requestAttributes instanceof ServletRequestAttributes)) {
return null; return null;
} }
ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) requestAttributes;
return servletRequestAttributes.getRequest(); return servletRequestAttributes.getRequest();
} }

View File

@@ -77,8 +77,7 @@ public class AuthRequestFactory {
extendList = extend.getConfig() extendList = extend.getConfig()
.keySet() .keySet()
.stream() .stream()
.map(String::toUpperCase) .filter(x -> names.contains(x.toUpperCase()))
.filter(names::contains)
.collect(Collectors.toList()); .collect(Collectors.toList());
} }

View File

@@ -113,13 +113,14 @@ public class AliyunSmsClient extends AbstractSmsClient {
} }
@VisibleForTesting @VisibleForTesting
@SuppressWarnings("EnhancedSwitchMigration")
Integer convertSmsTemplateAuditStatus(Integer templateStatus) { Integer convertSmsTemplateAuditStatus(Integer templateStatus) {
return switch (templateStatus) { switch (templateStatus) {
case 0 -> SmsTemplateAuditStatusEnum.CHECKING.getStatus(); case 0: return SmsTemplateAuditStatusEnum.CHECKING.getStatus();
case 1 -> SmsTemplateAuditStatusEnum.SUCCESS.getStatus(); case 1: return SmsTemplateAuditStatusEnum.SUCCESS.getStatus();
case 2 -> SmsTemplateAuditStatusEnum.FAIL.getStatus(); case 2: return SmsTemplateAuditStatusEnum.FAIL.getStatus();
default -> throw new IllegalArgumentException(String.format("未知审核状态(%d)", templateStatus)); default: throw new IllegalArgumentException(String.format("未知审核状态(%d)", templateStatus));
}; }
} }
/** /**