diff --git a/src/main/java/vt/suopo/vlog/appender/DefaultPrintStreamAppender.java b/src/main/java/vt/suopo/vlog/appender/DefaultPrintStreamAppender.java index 8d20623..fc969fe 100644 --- a/src/main/java/vt/suopo/vlog/appender/DefaultPrintStreamAppender.java +++ b/src/main/java/vt/suopo/vlog/appender/DefaultPrintStreamAppender.java @@ -9,7 +9,7 @@ import java.util.Objects; /** - * 默认日志输出-控制台 + * 默认日志输出-打印流 * * @author suopovate * @since 2024/04/27 diff --git a/src/main/java/vt/suopo/vlog/common/CachePools.java b/src/main/java/vt/suopo/vlog/common/CachePools.java deleted file mode 100644 index 7117c8d..0000000 --- a/src/main/java/vt/suopo/vlog/common/CachePools.java +++ /dev/null @@ -1,37 +0,0 @@ -package vt.suopo.vlog.common; - -import java.lang.reflect.Field; -import java.lang.reflect.Method; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; - -/** - * Cache pools - * - * @author suopovate - * @since 2024/04/27 - *

- * All rights Reserved. - */ -public final class CachePools { - private CachePools() {} - - private static final Map FIELD_POOL_CACHE = new ConcurrentHashMap<>(); - private static final Map METHOD_POOL_CACHE = new ConcurrentHashMap<>(); - - public static Field getFieldCache(String key) { - return FIELD_POOL_CACHE.get(key); - } - - public static void setFieldCache(String key, Field field) { - FIELD_POOL_CACHE.put(key, field); - } - - public static Method getMethodCache(String key) { - return METHOD_POOL_CACHE.get(key); - } - - public static void setMethodCache(String key, Method method) { - METHOD_POOL_CACHE.put(key, method); - } -} diff --git a/src/main/java/vt/suopo/vlog/common/Constants.java b/src/main/java/vt/suopo/vlog/common/Constants.java deleted file mode 100644 index b625337..0000000 --- a/src/main/java/vt/suopo/vlog/common/Constants.java +++ /dev/null @@ -1,22 +0,0 @@ -package vt.suopo.vlog.common; - -/** - * Constants - * - * @author suopovate - * @since 2024/04/27 - * - * All rights Reserved. - */ -public final class Constants { - private Constants(){} - - public static final String COMMA = ","; - public static final String EMPTY_STRING = ""; - public static final String GET_HEADER = "getHeader"; - public static final String SET_HEADER = "setHeader"; - public static final String GET_ATTRIBUTE = "getAttribute"; - public static final String SET_ATTRIBUTE = "setAttribute"; - public static final String INJECT_DIR_ROOT = "autotrace4j/inject"; - public static final String INJECT_DIR_BOOTSTRAP = "bootstrap"; -} diff --git a/src/main/java/vt/suopo/vlog/common/MethodWrapper.java b/src/main/java/vt/suopo/vlog/common/MethodWrapper.java deleted file mode 100644 index 6f5d2bb..0000000 --- a/src/main/java/vt/suopo/vlog/common/MethodWrapper.java +++ /dev/null @@ -1,58 +0,0 @@ -package vt.suopo.vlog.common; - -import java.lang.reflect.Method; -import java.util.Objects; - -/** - * Method Wrapper for avoiding NPE. - * - * @author suopovate - * @since 2024/04/27 - *

- * All rights Reserved. - */ -public class MethodWrapper { - private Method method; - private Object object; - - private MethodWrapper(){} - - public static MethodWrapper of(Object obj, String methodName, boolean declared, Class... parameterTypes) { - MethodWrapper wrapper = new MethodWrapper(); - if (Objects.isNull(obj)) { - return wrapper; - } - wrapper.object = obj; - - Class clazz = obj instanceof Class ? (Class)obj : obj.getClass(); - try { - final String key = clazz.getName() + "." + methodName; - wrapper.method = CachePools.getMethodCache(key); - if (Objects.isNull(wrapper.method)) { - wrapper.method = declared - ? clazz.getDeclaredMethod(methodName, parameterTypes) - : clazz.getMethod(methodName, parameterTypes); - wrapper.method.setAccessible(true); - CachePools.setMethodCache(key, wrapper.method); - } - } catch (Exception e) { - e.printStackTrace(); - } - - return wrapper; - } - - @SuppressWarnings("unchecked") - public T invoke(Object... args) { - if (Objects.isNull(object)) { - return null; - } - - try { - return (T)method.invoke(object, args); - } catch (Exception e) { - e.printStackTrace(); - return null; - } - } -} diff --git a/src/main/java/vt/suopo/vlog/common/ReflectUtils.java b/src/main/java/vt/suopo/vlog/common/ReflectUtils.java index 5a9d771..00dc7b4 100644 --- a/src/main/java/vt/suopo/vlog/common/ReflectUtils.java +++ b/src/main/java/vt/suopo/vlog/common/ReflectUtils.java @@ -1,7 +1,10 @@ package vt.suopo.vlog.common; import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.util.Map; import java.util.Objects; +import java.util.concurrent.ConcurrentHashMap; /** * Core Reflect Utils @@ -12,6 +15,7 @@ * All rights Reserved. */ public final class ReflectUtils { + private ReflectUtils() {} /** @@ -74,7 +78,7 @@ public static Field getField(Object obj, String fieldName, boolean declared) { field.setAccessible(true); CachePools.setFieldCache(cacheKey, field); } catch (Exception e) { - System.err.println(ThrowableUtils.throwableToStr(e));; + System.err.println(ThrowableUtils.throwableToStr(e)); return null; } } @@ -88,7 +92,7 @@ public static T getFieldValue(Object obj, String fieldName) { try { return Objects.isNull(field) ? null : (T) field.get(obj); } catch (Exception e) { - System.err.println(ThrowableUtils.throwableToStr(e));; + System.err.println(ThrowableUtils.throwableToStr(e)); return null; } } @@ -100,7 +104,7 @@ public static T getFieldValue(Object obj, String fieldName, boolean declared try { return Objects.isNull(field) ? null : (T) field.get(obj); } catch (Exception e) { - System.err.println(ThrowableUtils.throwableToStr(e));; + System.err.println(ThrowableUtils.throwableToStr(e)); return null; } } @@ -115,7 +119,7 @@ public static void setFieldValue(String className, String fieldName, Object valu clazz = Class.forName(className, true, ClassLoader.getSystemClassLoader()); } catch (Exception e) { System.err.println(e.getMessage()); - System.err.println(ThrowableUtils.throwableToStr(e));; + System.err.println(ThrowableUtils.throwableToStr(e)); return; } setFieldValue(clazz, fieldName, value, declared); @@ -137,7 +141,76 @@ public static void setFieldValue(Object obj, String fieldName, Object value, boo field.set(obj, value); } } catch (Exception e) { - System.err.println(ThrowableUtils.throwableToStr(e));; + System.err.println(ThrowableUtils.throwableToStr(e)); + } + } + + public static final class CachePools { + private CachePools() {} + + private static final Map FIELD_POOL_CACHE = new ConcurrentHashMap<>(); + private static final Map METHOD_POOL_CACHE = new ConcurrentHashMap<>(); + + public static Field getFieldCache(String key) { + return FIELD_POOL_CACHE.get(key); + } + + public static void setFieldCache(String key, Field field) { + FIELD_POOL_CACHE.put(key, field); + } + + public static Method getMethodCache(String key) { + return METHOD_POOL_CACHE.get(key); + } + + public static void setMethodCache(String key, Method method) { + METHOD_POOL_CACHE.put(key, method); + } + } + + public static class MethodWrapper { + private Method method; + private Object object; + + private MethodWrapper(){} + + public static MethodWrapper of(Object obj, String methodName, boolean declared, Class... parameterTypes) { + MethodWrapper wrapper = new MethodWrapper(); + if (Objects.isNull(obj)) { + return wrapper; + } + wrapper.object = obj; + + Class clazz = obj instanceof Class ? (Class)obj : obj.getClass(); + try { + final String key = clazz.getName() + "." + methodName; + wrapper.method = CachePools.getMethodCache(key); + if (Objects.isNull(wrapper.method)) { + wrapper.method = declared + ? clazz.getDeclaredMethod(methodName, parameterTypes) + : clazz.getMethod(methodName, parameterTypes); + wrapper.method.setAccessible(true); + CachePools.setMethodCache(key, wrapper.method); + } + } catch (Exception e) { + System.err.println(ThrowableUtils.throwableToStr(e)); + } + + return wrapper; + } + + @SuppressWarnings("unchecked") + public T invoke(Object... args) { + if (Objects.isNull(object)) { + return null; + } + + try { + return (T)method.invoke(object, args); + } catch (Exception e) { + System.err.println(ThrowableUtils.throwableToStr(e)); + return null; + } } } diff --git a/src/main/java/vt/suopo/vlog/common/SystemUtils.java b/src/main/java/vt/suopo/vlog/common/SystemUtils.java index 5b0c277..f2fd4fa 100644 --- a/src/main/java/vt/suopo/vlog/common/SystemUtils.java +++ b/src/main/java/vt/suopo/vlog/common/SystemUtils.java @@ -19,8 +19,8 @@ public final class SystemUtils { private SystemUtils() {} /** - * @throws java.nio.file.InvalidPathException if a {@code Path} object cannot be constructed from the abstract - * path (see {@link java.nio.file.FileSystem#getPath FileSystem.getPath}) + * @throws java.nio.file.InvalidPathException if a {@code Path} object cannot be constructed from the abstract path + * (see {@link java.nio.file.FileSystem#getPath FileSystem.getPath}) */ public static Path getSysPropertyPath(String name) { if (Objects.isNull(System.getProperty(name)) || System.getProperty(name).isEmpty()) { @@ -37,28 +37,4 @@ public static Path getSysTempDir() { return Paths.get(System.getProperty("java.io.tmpdir")); } - /** - * get the injectDir for store the class to be injected - * - * @param injectDir the real directory - * @return the absolute path of injectDir - */ - private static Path getClassInjectTempDirPath(String injectDir) { - return getSysTempDir().resolve(Constants.INJECT_DIR_ROOT).resolve(injectDir); - } - - /** - * get the injectDir for store the class to be injected - * - * @param injectDir the real directory - * @return the absolute path of injectDir - */ - public static File getClassInjectTempDir(String injectDir) throws IOException { - Path dirPath = getClassInjectTempDirPath(injectDir); - if (!Files.exists(dirPath)) { - Files.createDirectories(dirPath); - } - return dirPath.toFile(); - } - }