Skip to content

Commit

Permalink
refactor: fix PMD
Browse files Browse the repository at this point in the history
  • Loading branch information
luckyQing committed May 10, 2024
1 parent 66fb00b commit 5223d8c
Show file tree
Hide file tree
Showing 86 changed files with 294 additions and 239 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,23 @@
*/
public abstract class AbstractUserContext {

/**
* 用户上下文信息
*/
protected static final ThreadLocal<SmartUser> USER_THREAD_LOCAL = new InheritableThreadLocal<>();

/**
* 设置用户上下文信息
*
* @param smartUser
*/
public static void setContext(SmartUser smartUser) {
USER_THREAD_LOCAL.set(smartUser);
}

/**
* 移除用户上下文信息
*/
public static void remove() {
USER_THREAD_LOCAL.remove();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
@ToString
public class SmartUser implements Serializable {

private static final long serialVersionUID = 1L;

/**
* 用户id
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public boolean checkExceptionApiAndNotice() {
* @return
*/
private String buildWeworkRobotMessage(List<UnHealthApiDTO> unHealthInfos) {
StringBuilder content = new StringBuilder(32);
StringBuilder content = new StringBuilder(64);
content.append("**").append(environment.getProperty("spring.application.name")).append("** ")
.append(TimeUnit.SECONDS.toMinutes(healthProperties.getCleanIntervalSeconds()))
.append("分钟异常接口统计:");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public class ApiHealthRepository implements InitializingBean, DisposableBean {
*/
private ConcurrentMap<String, ApiHealthCacheDTO> apiStatusStatistics = new ConcurrentHashMap<>();
private CreateApiHealthCacheDtoFunction createApiHealthCacheDtoFunction = new CreateApiHealthCacheDtoFunction();
private ScheduledExecutorService cleanSchedule = null;
private ScheduledExecutorService cleanSchedule;

/**
* 添加接口访问记录
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ public class PercentUtil {
* @return
*/
public static String format(Object obj) {
return PERCENT_FORMAT.format(obj);
synchronized (PERCENT_FORMAT) {
return PERCENT_FORMAT.format(obj);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,32 +15,20 @@
*/
package io.github.smart.cloud.starter.actuator.test.cases;

import com.fasterxml.jackson.databind.JsonNode;
import io.github.smart.cloud.starter.actuator.repository.ApiHealthRepository;
import io.github.smart.cloud.starter.actuator.test.prepare.App;
import io.github.smart.cloud.starter.actuator.test.util.JacksonUtil;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

import javax.servlet.Filter;
import java.nio.charset.StandardCharsets;
import java.util.Map;

public abstract class AbstractTest {

protected MockMvc mockMvc = null;
protected MockMvc mockMvc;
@Autowired
protected ApplicationContext applicationContext;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.json.JsonMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
package io.github.smart.cloud.starter.core.business.autoconfigure;

import io.github.smart.cloud.starter.core.constants.PackageConfig;
import org.apache.commons.lang3.ArrayUtils;
import io.github.smart.cloud.starter.core.constants.SmartEnv;
import io.github.smart.cloud.starter.core.support.annotation.SmartBootApplication;
import io.github.smart.cloud.starter.core.support.annotation.YamlScan;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.boot.env.YamlPropertySourceLoader;
Expand All @@ -35,6 +35,8 @@

import java.io.IOException;
import java.lang.reflect.Field;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.*;

/**
Expand Down Expand Up @@ -89,16 +91,15 @@ public void postProcessEnvironment(ConfigurableEnvironment environment, SpringAp
}

private boolean isRegisterShutdownHook(SpringApplication application) {
boolean registerShutdownHook = false;
try {
Field field = SpringApplication.class.getDeclaredField("registerShutdownHook");
field.setAccessible(true);
registerShutdownHook = field.getBoolean(application);
} catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
e.printStackTrace();
}

return registerShutdownHook;
return AccessController.doPrivileged((PrivilegedAction<Boolean>) () -> {
try {
Field field = SpringApplication.class.getDeclaredField("registerShutdownHook");
field.setAccessible(true);
return field.getBoolean(application);
} catch (ReflectiveOperationException | SecurityException e) {
throw new RuntimeException(e);
}
});
}

/**
Expand Down Expand Up @@ -194,10 +195,10 @@ private static void loadYaml(String[] locationPatterns, ConfigurableEnvironment

// 2、将所有Resource加入Environment中
try {
YamlPropertySourceLoader yamlPropertySourceLoader = new YamlPropertySourceLoader();
for (Resource resource : resourceSet) {
System.out.println("load yaml ==> " + resource.getFilename());

YamlPropertySourceLoader yamlPropertySourceLoader = new YamlPropertySourceLoader();
List<PropertySource<?>> propertySources = yamlPropertySourceLoader.load(resource.getFilename(),
resource);
for (PropertySource<?> propertySource : propertySources) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,11 @@ public static String getApiExpression(String[] basePackages) {
* @return
*/
public static String buildExpression(String[] basePackages) {
StringBuilder executions = new StringBuilder();
StringBuilder executions = new StringBuilder(32);
for (int i = 0; i < basePackages.length; i++) {
executions.append(PointcutPrimitive.EXECUTION.getName()).append("( * " + basePackages[i] + "..*.*(..))");
executions.append(PointcutPrimitive.EXECUTION.getName()).append("( * ").append(basePackages[i]).append("..*.*(..))");
if (i != basePackages.length - 1) {
executions.append(" || ");
executions.append("||");
}
}

Expand All @@ -104,7 +104,7 @@ public static String buildExpression(String[] basePackages) {
* @return
*/
public static String buildExpression(String[] basePackages, String expression) {
return "(" + buildExpression(basePackages) + ") && (" + expression + ")";
return "(" + buildExpression(basePackages) + ")&&(" + expression + ")";
}

/**
Expand All @@ -117,9 +117,9 @@ private static String getAnnotationExpression(String annotationPointcut, List<Cl
StringBuilder expression = new StringBuilder();
for (int i = 0; i < annotations.size(); i++) {
expression.append(annotationPointcut);
expression.append("(" + annotations.get(i).getTypeName() + ")");
expression.append('(').append(annotations.get(i).getTypeName()).append(')');
if (i != annotations.size() - 1) {
expression.append(" || ");
expression.append("||");
}
}
return expression.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
*/
public class ReflectionUtil extends ReflectionUtils {

private static Reflections reflections = null;
private static Reflections reflections;

static {
String[] basePackages = PackageConfig.getBasePackages();
Expand All @@ -54,6 +54,7 @@ public class ReflectionUtil extends ReflectionUtils {
}

private ReflectionUtil() {
super();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class PackageConfig {
* 基础包
*/
@Setter
private static String[] basePackages = null;
private static String[] basePackages;

public static String[] getBasePackages() {
Assert.isTrue(ArrayUtils.isNotEmpty(PackageConfig.basePackages), "basePackages未配置!!!");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
*/
public class SmartBootApplicationCondition implements Condition {

private static String bootstrapClassName = null;
private static String bootstrapClassName;

@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@
*/
package io.github.smart.cloud.starter.core.test.unit;

import io.github.smart.cloud.starter.core.test.prepare.Application;
import io.github.smart.cloud.starter.core.test.prepare.service.SmsService;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.appender.ConsoleAppender;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import io.github.smart.cloud.starter.core.test.prepare.Application;
import io.github.smart.cloud.starter.core.test.prepare.service.SmsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit.jupiter.SpringExtension;
Expand All @@ -44,13 +44,14 @@ void testAsync() throws InterruptedException {
smsService.asyncSend();
TimeUnit.SECONDS.sleep(1);

LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
String appenderName = "console";
ConsoleAppender appender = ctx.getConfiguration().getAppender(appenderName);
ByteBuffer byteBuffer = appender.getManager().getByteBuffer().asReadOnlyBuffer();
String logContent = StandardCharsets.UTF_8.decode(byteBuffer).toString();
byteBuffer.flip();
Assertions.assertThat(StringUtils.containsAny(logContent, "asyncException@method=asyncSend; param=")).isTrue();
try (LoggerContext ctx = (LoggerContext) LogManager.getContext(false)) {
String appenderName = "console";
ConsoleAppender appender = ctx.getConfiguration().getAppender(appenderName);
ByteBuffer byteBuffer = appender.getManager().getByteBuffer().asReadOnlyBuffer();
String logContent = StandardCharsets.UTF_8.decode(byteBuffer).toString();
byteBuffer.flip();
Assertions.assertThat(StringUtils.containsAny(logContent, "asyncException@method=asyncSend; param=")).isTrue();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ private void addUserInfoCredentials(URI uri) {
}

private Credentials createUserInfoCredentials(String userInfo) {
int delimiter = userInfo.indexOf(":");
int delimiter = userInfo.indexOf(':');
if (delimiter == -1) {
return new UsernamePasswordCredentials(userInfo, null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
*/
public class DynamicElasticsearchPropertiesNotConfigException extends BaseException {

private static final long serialVersionUID = 1L;

public DynamicElasticsearchPropertiesNotConfigException() {
setCode(ElasticsearchReturnCodes.DYNAMIC_ELASTICSEARCH_PROPERTIES_NOT_CONFIG);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
*/
public class ElasticsearchDataSourceNotFoundException extends BaseException {

private static final long serialVersionUID = 1L;

public ElasticsearchDataSourceNotFoundException(String dsKey) {
setCode(ElasticsearchReturnCodes.ELASTICSEARCH_DS_NOT_FOUND);
setArgs(new Object[]{dsKey});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
*/
public class ElasticsearchDatasourceKeyNotExistException extends BaseException {

private static final long serialVersionUID = 1L;

public ElasticsearchDatasourceKeyNotExistException() {
setCode(ElasticsearchReturnCodes.ELASTICSEARCH_DS_KEY_CAN_NOT_BLANK);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@
*/
package io.github.smart.cloud.starter.rpc.feign.condition;

import java.util.Set;

import io.github.smart.cloud.starter.core.business.util.ReflectionUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
Expand All @@ -27,7 +26,7 @@
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import lombok.extern.slf4j.Slf4j;
import java.util.Set;

/**
* <code>FeignClient</code>生效条件判断
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
*/
public class GlobalId {

private static SnowflakeId snowflakeId = null;
private static SnowflakeId snowflakeId;

private GlobalId() {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

public abstract class AbstractIntegrationTest {

private static RedisServer redisServer = null;
private static RedisServer redisServer;
/**
* redis server端口
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ private Object wrap(Object bean) {
* @param advice
* @return
*/
Object createProxy(Object bean, boolean cglibProxy, Advice advice) {
private Object createProxy(Object bean, boolean cglibProxy, Advice advice) {
ProxyFactoryBean factory = new ProxyFactoryBean();
factory.setProxyTargetClass(cglibProxy);
factory.addAdvice(advice);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public void execute() throws Exception {
}

private void doExecute() throws Exception {
ScopedSpan span = tracing.tracer().startScopedSpanWithParent(spanNamer().name(delegate, "xxl-job"),
ScopedSpan span = tracing.tracer().startScopedSpanWithParent(getSpanNamer().name(delegate, "xxl-job"),
tracing.currentTraceContext().get());
try {
delegate.execute();
Expand All @@ -74,7 +74,7 @@ private void doExecute() throws Exception {
*
* @return
*/
private SpanNamer spanNamer() {
private SpanNamer getSpanNamer() {
if (spanNamer == null) {
try {
spanNamer = beanFactory.getBean(SpanNamer.class);
Expand Down

0 comments on commit 5223d8c

Please sign in to comment.