Skip to content

Commit

Permalink
CodeCheck修改
Browse files Browse the repository at this point in the history
Signed-off-by: TLPC <470193496@qq.com>
  • Loading branch information
TangLeDaily committed Nov 16, 2023
1 parent ba9c882 commit 5de9cc4
Show file tree
Hide file tree
Showing 70 changed files with 688 additions and 359 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.huaweicloud.sermant.core.classloader;

import com.huaweicloud.sermant.core.common.CommonConstant;
import com.huaweicloud.sermant.core.exception.FileCheckException;
import com.huaweicloud.sermant.core.plugin.classloader.PluginClassFinder;
import com.huaweicloud.sermant.core.plugin.classloader.PluginClassLoader;
import com.huaweicloud.sermant.core.utils.FileUtils;
Expand All @@ -25,6 +26,8 @@
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -76,7 +79,13 @@ private static FrameworkClassLoader initFrameworkClassLoader(String path) throws
* @return PluginClassLoader
*/
public static PluginClassLoader createPluginClassLoader() {
return new PluginClassLoader(new URL[0], sermantClassLoader);
// return new PluginClassLoader(new URL[0], sermantClassLoader);

Check warning on line 82 in sermant-agentcore/sermant-agentcore-core/src/main/java/com/huaweicloud/sermant/core/classloader/ClassLoaderManager.java

View workflow job for this annotation

GitHub Actions / Checkstyle

[Checkstyle Check] reported by reviewdog 🐶 编程规范-建议3.8 禁止插入空格水平对齐(包括多余括号)。 Raw Output: /home/runner/work/Sermant/Sermant/./sermant-agentcore/sermant-agentcore-core/src/main/java/com/huaweicloud/sermant/core/classloader/ClassLoaderManager.java:82:0: warning: 编程规范-建议3.8 禁止插入空格水平对齐(包括多余括号)。 (com.puppycrawl.tools.checkstyle.checks.regexp.RegexpSinglelineCheck)

Check failure on line 82 in sermant-agentcore/sermant-agentcore-core/src/main/java/com/huaweicloud/sermant/core/classloader/ClassLoaderManager.java

View workflow job for this annotation

GitHub Actions / Checkstyle

[Checkstyle Check] reported by reviewdog 🐶 编程规范-规则2.7 : 不用的代码段直接删除,不要注释掉 Raw Output: /home/runner/work/Sermant/Sermant/./sermant-agentcore/sermant-agentcore-core/src/main/java/com/huaweicloud/sermant/core/classloader/ClassLoaderManager.java:82:0: error: 编程规范-规则2.7 : 不用的代码段直接删除,不要注释掉 (com.puppycrawl.tools.checkstyle.checks.regexp.RegexpSinglelineCheck)
return AccessController.doPrivileged(new PrivilegedAction<PluginClassLoader>() {
@Override
public PluginClassLoader run() {
return new PluginClassLoader(new URL[0], sermantClassLoader);
}
});
}

public static SermantClassLoader getSermantClassLoader() {
Expand All @@ -99,11 +108,11 @@ public static PluginClassFinder getPluginClassFinder() {
private static URL[] listCoreImplementUrls(String coreImplementPath) throws MalformedURLException {
File coreImplementDir = new File(FileUtils.validatePath(coreImplementPath));
if (!coreImplementDir.exists() || !coreImplementDir.isDirectory()) {
throw new RuntimeException("core implement directory is not exist or is not directory.");
throw new FileCheckException("core implement directory is not exist or is not directory.");
}
File[] jars = coreImplementDir.listFiles((file, name) -> name.endsWith(".jar"));
if (jars == null || jars.length == 0) {
throw new RuntimeException("core implement directory is empty");
throw new FileCheckException("core implement directory is empty");
}
List<URL> urlList = new ArrayList<>();
for (File jar : jars) {
Expand All @@ -115,11 +124,11 @@ private static URL[] listCoreImplementUrls(String coreImplementPath) throws Malf
private static URL[] listCommonLibUrls(String commonLibPath) throws MalformedURLException {
File commonLibDir = new File(FileUtils.validatePath(commonLibPath));
if (!commonLibDir.exists() || !commonLibDir.isDirectory()) {
throw new RuntimeException("common lib is not exist or is not directory.");
throw new FileCheckException("common lib is not exist or is not directory.");
}
File[] jars = commonLibDir.listFiles((file, name) -> name.endsWith(".jar"));
if (jars == null || jars.length == 0) {
throw new RuntimeException("common lib directory is empty");
throw new FileCheckException("common lib directory is empty");
}
List<URL> urlList = new ArrayList<>();
for (File jar : jars) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import com.huaweicloud.sermant.core.classloader.ClassLoaderManager;
import com.huaweicloud.sermant.core.classloader.FrameworkClassLoader;
import com.huaweicloud.sermant.core.exception.LoggerInitException;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
Expand All @@ -35,9 +36,9 @@ public class LoggerFactory {

private static final String LOGGER_INIT_METHOD = "init";

private static Logger defaultLogger;
private static volatile Logger defaultLogger;

private static Logger sermantLogger;
private static volatile Logger sermantLogger;

private LoggerFactory() {
}
Expand All @@ -50,15 +51,19 @@ private LoggerFactory() {
*/
public static void init(String artifact) {
if (sermantLogger == null) {
FrameworkClassLoader frameworkClassLoader = ClassLoaderManager.getFrameworkClassLoader();
try {
Method initMethod = frameworkClassLoader
.loadClass(LOGGER_FACTORY_IMPL_CLASS)
.getMethod(LOGGER_INIT_METHOD, String.class);
sermantLogger = (Logger) initMethod.invoke(null, artifact);
} catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException
| InvocationTargetException e) {
throw new RuntimeException(e);
synchronized (LoggerFactory.class) {
if (sermantLogger == null) {
FrameworkClassLoader frameworkClassLoader = ClassLoaderManager.getFrameworkClassLoader();
try {
Method initMethod = frameworkClassLoader
.loadClass(LOGGER_FACTORY_IMPL_CLASS)
.getMethod(LOGGER_INIT_METHOD, String.class);
sermantLogger = (Logger) initMethod.invoke(null, artifact);
} catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException
| InvocationTargetException e) {
throw new LoggerInitException(e.getMessage());

Check failure on line 64 in sermant-agentcore/sermant-agentcore-core/src/main/java/com/huaweicloud/sermant/core/common/LoggerFactory.java

View workflow job for this annotation

GitHub Actions / Checkstyle

[Checkstyle Check] reported by reviewdog 🐶 Expected @throws tag for 'LoggerInitException'. Raw Output: /home/runner/work/Sermant/Sermant/./sermant-agentcore/sermant-agentcore-core/src/main/java/com/huaweicloud/sermant/core/common/LoggerFactory.java:64:35: error: Expected @throws tag for 'LoggerInitException'. (com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocMethodCheck)
}
}
}
}
}
Expand All @@ -75,7 +80,11 @@ public static Logger getLogger() {

// 避免日志重复获取
if (defaultLogger == null) {
defaultLogger = java.util.logging.Logger.getLogger("sermant");
synchronized (LoggerFactory.class) {
if (defaultLogger == null) {
defaultLogger = java.util.logging.Logger.getLogger("sermant");
}
}
}
return defaultLogger;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ private static LoadConfigStrategy<?> getLoadConfigStrategy(File configFile, Clas
* <p>文件中声明的所有实现都将会进行遍历,每一个实现类都会通过spi获取实例,然后调用{@code configConsumer}进行消费
*
* @param configConsumer 配置处理方法
* @param classLoader 类加载器
*/
private static void foreachConfig(ConfigConsumer configConsumer,
ClassLoader classLoader) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,16 @@

package com.huaweicloud.sermant.core.config.common;

import com.huaweicloud.sermant.core.config.utils.ConfigKeyUtil;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* 通用字段键注解
* <p>用于修饰配置对象的属性,与{@link ConfigTypeKey}一并构建配置信息键
* <p>用于修饰配置对象的属性,与{ConfigTypeKey一并构建配置信息键
* <p>主要作用是修正成员属性和配置键之间的差异
* <p>见{@link ConfigKeyUtil#getFieldKey(java.lang.reflect.Field)}
* <p>见ConfigKeyUtil#getFieldKey(java.lang.reflect.Field)
*
* @author HapThorin
* @version 1.0.0
Expand All @@ -36,5 +34,10 @@
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD})
public @interface ConfigFieldKey {
/**
* 属性名
*
* @return 属性名
*/
String value();
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

package com.huaweicloud.sermant.core.config.common;

import com.huaweicloud.sermant.core.config.utils.ConfigKeyUtil;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
Expand All @@ -27,7 +25,7 @@
* 通用配置对象前缀
* <p>如果配置对象中的所有属性对应的配置键都包含相同的前缀,那么可以使用该注解声明
* <p>与{@link ConfigFieldKey}一同构建配置键,{@link ConfigFieldKey}不存在时,直接使用属性名
* <p>见{@link ConfigKeyUtil#getTypeKey(Class)}
* <p>见ConfigKeyUtil#getTypeKey(Class)
*
* @author HapThorin
* @version 1.0.0
Expand All @@ -36,5 +34,10 @@
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
public @interface ConfigTypeKey {
/**
* 类型名
*
* @return 类型名
*/
String value();
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,34 @@ public class ConfigFieldUtil {
*/
private static final Logger LOGGER = LoggerFactory.getLogger();

/**
* boolean类型的get或set方法中属性名的最短长度
*/
private static final int FIELD_NAME_MIN_LENGTH = 3;

/**
* boolean类型的get或set方法中属性名的首字母下标
*/
private static final int FIELD_NAME_CHECK_INDEX = 2;

/**
* boolean类型的get方法前缀,首字母小写字母
*/
private static final String BOOLEAN_FUNCTION_PREFIX_LOWERCASE = "is";

/**
* boolean类型的get方法前缀,首字母大写字母
*/
private static final String BOOLEAN_FUNCTION_PREFIX_UPPERCASE = "Is";

private ConfigFieldUtil() {
}

/**
* 设置值,优先查找{@code setter}调用,不存在时尝试直接赋值
* <p>因此,要求配置对象的属性值需要拥有相应的{@code setter},或者要求改属性值是公有的
*
* @param obj 被设置值的对象
* @param obj 被设置值的对象
* @param field 被设置的字段
* @param value 被设置的字段值
*/
Expand All @@ -69,21 +92,23 @@ public static void setField(Object obj, Field field, Object value) {
/**
* 通过属性名称获取{@code setter}
*
* @param cls 配置对象类
* @param cls 配置对象类
* @param fieldName 属性名称
* @param type 属性类型
* @param type 属性类型
* @return setter方法
*/
private static Method getSetter(Class<?> cls, String fieldName, Class<?> type) {
final String setterName;
if ((type == boolean.class || type == Boolean.class) &&
fieldName.length() >= 3 && (fieldName.startsWith("is") || fieldName.startsWith("Is")) &&
fieldName.charAt(2) >= 'A' && fieldName.charAt(2) <= 'Z') {
setterName = "set" + fieldName.substring(2);
} else {
final char head = fieldName.charAt(0);
setterName = "set" + (
(head >= 'a' && head <= 'z') ? ((char) (head + 'A' - 'a')) + fieldName.substring(1) : fieldName);
String setterName;
final char head = fieldName.charAt(0);
setterName = "set" + (
(head >= 'a' && head <= 'z') ? ((char) (head + 'A' - 'a')) + fieldName.substring(1) : fieldName);
if ((type == boolean.class || type == Boolean.class) && fieldName.length() >= FIELD_NAME_MIN_LENGTH) {
if ((fieldName.startsWith(BOOLEAN_FUNCTION_PREFIX_LOWERCASE)
|| fieldName.startsWith(BOOLEAN_FUNCTION_PREFIX_UPPERCASE))
&& fieldName.charAt(FIELD_NAME_CHECK_INDEX) >= 'A'
&& fieldName.charAt(FIELD_NAME_CHECK_INDEX) <= 'Z') {
setterName = "set" + fieldName.substring(FIELD_NAME_CHECK_INDEX);
}
}
try {
return cls.getMethod(setterName, type);
Expand All @@ -109,6 +134,13 @@ private static void forceSet(Object obj, Field field, Object value) {
}
}

/**
* 获取属性对象
*
* @param obj 目标对象
* @param field 属性
* @return 属性对象
*/
public static Object getField(Object obj, Field field) {
try {
final Method getter = getGetter(field.getDeclaringClass(), field.getName(), field.getType());
Expand All @@ -129,15 +161,17 @@ public static Object getField(Object obj, Field field) {
}

private static Method getGetter(Class<?> cls, String fieldName, Class<?> type) {
final String getterName;
if ((type == boolean.class || type == Boolean.class) &&
fieldName.length() >= 3 && (fieldName.startsWith("is") || fieldName.startsWith("Is")) &&
fieldName.charAt(2) >= 'A' && fieldName.charAt(2) <= 'Z') {
getterName = "is" + fieldName.substring(2);
} else {
final char head = fieldName.charAt(0);
getterName = "get" + (
(head >= 'a' && head <= 'z') ? ((char) (head + 'A' - 'a')) + fieldName.substring(1) : fieldName);
String getterName;
final char head = fieldName.charAt(0);
getterName = "get" + (
(head >= 'a' && head <= 'z') ? ((char) (head + 'A' - 'a')) + fieldName.substring(1) : fieldName);
if ((type == boolean.class || type == Boolean.class) && fieldName.length() >= FIELD_NAME_MIN_LENGTH) {
if ((fieldName.startsWith(BOOLEAN_FUNCTION_PREFIX_LOWERCASE)
|| fieldName.startsWith(BOOLEAN_FUNCTION_PREFIX_UPPERCASE))
&& fieldName.charAt(FIELD_NAME_CHECK_INDEX) >= 'A'
&& fieldName.charAt(FIELD_NAME_CHECK_INDEX) <= 'Z') {
getterName = "is" + fieldName.substring(FIELD_NAME_CHECK_INDEX);
}
}
try {
return cls.getMethod(getterName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public class ConfigValueUtil {
key -> key.toUpperCase(Locale.ROOT).replace('.', '-'),
key -> key.toLowerCase(Locale.ROOT),
key -> key.toLowerCase(Locale.ROOT).replace('.', '_'),
key -> key.toLowerCase(Locale.ROOT).replace('.', '-'),
key -> key.toLowerCase(Locale.ROOT).replace('.', '-')
};

/**
Expand Down Expand Up @@ -121,7 +121,8 @@ public class ConfigValueUtil {
},
(key, argsMap, provider) -> System.getenv(key),
(key, argsMap, provider) -> System.getProperty(key),
(key, argsMap, provider) -> provider == null ? null : provider.getFixedValue(key)
(key, argsMap, provider) -> provider == null ? null : (
StringUtils.isEmpty(provider.getFixedValue(key)) ? null : provider.getFixedValue(key))
);

private ConfigValueUtil() {
Expand Down Expand Up @@ -326,6 +327,7 @@ public static String fixValue(String configKey, String configVal, Map<String, Ob
* @param key 键
* @param defaultVal 默认值
* @param provider 配置信息
* @param argsMap 参数Map
* @return 环境变量或系统变量
*/
private static String getFormatKeyFixVal(String key, String defaultVal, Map<String, Object> argsMap,
Expand All @@ -347,17 +349,18 @@ private static String getFormatKeyFixVal(String key, String defaultVal, Map<Stri
*
* @param key 键
* @param configVal 配置值
* @param argsMap 参数Map
* @return 最终配置参考值
*/
private static String getValByFixedKey(String key, String configVal, Map<String, Object> argsMap) {
// 1. xxx.xxx.appName直接获取 2. xxx.xxx.app-name处理为xxx.xxx.app.name再获取
// appName直接获取app-name处理为app.name再获取
String keyReplaceMiddleLine = transFromMiddleLine(key);
Optional<String> fixedValue = getValueByOrder(argsMap, keyReplaceMiddleLine);
if (fixedValue.isPresent()) {
return fixedValue.get();
}

// 3. xxx.xxx.appName分割为xxx.xxx.app.name
// appName分割为app.name
String keyWithoutCamel = transFromCamel(keyReplaceMiddleLine);
if (!keyReplaceMiddleLine.equals(keyWithoutCamel)) {
fixedValue = getValueByOrder(argsMap, keyWithoutCamel);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ public EventLevel getEventLevel() {
return eventLevel;
}

/**
* 获取范围

Check failure on line 92 in sermant-agentcore/sermant-agentcore-core/src/main/java/com/huaweicloud/sermant/core/event/collector/FrameworkEventDefinitions.java

View workflow job for this annotation

GitHub Actions / Checkstyle

[Checkstyle Check] reported by reviewdog 🐶 编程规范-规则2.6:注释正文与其下的各个Javadoc tag之间加1个空行 Raw Output: /home/runner/work/Sermant/Sermant/./sermant-agentcore/sermant-agentcore-core/src/main/java/com/huaweicloud/sermant/core/event/collector/FrameworkEventDefinitions.java:92:0: error: 编程规范-规则2.6:注释正文与其下的各个Javadoc tag之间加1个空行 (com.puppycrawl.tools.checkstyle.checks.regexp.RegexpMultilineCheck)
* @return framework
*/
public String getScope() {
return "framework";
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright (C) 2023-2023 Huawei Technologies Co., Ltd. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.huaweicloud.sermant.core.exception;

/**
* 文件检查异常
*
* @author tangle
* @since 2023-11-07
*/
public class FileCheckException extends RuntimeException {
private static final long serialVersionUID = 1339575470808108623L;

/**
* 文件检查异常
*
* @param message 异常信息
*/
public FileCheckException(String message) {
super(message);
}
}
Loading

0 comments on commit 5de9cc4

Please sign in to comment.