Skip to content

Commit

Permalink
optimize code style
Browse files Browse the repository at this point in the history
  • Loading branch information
supervate committed Apr 28, 2024
1 parent 2642201 commit 214220a
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 149 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import java.util.Objects;

/**
* 默认日志输出-控制台
* 默认日志输出-打印流
*
* @author suopovate
* @since 2024/04/27
Expand Down
37 changes: 0 additions & 37 deletions src/main/java/vt/suopo/vlog/common/CachePools.java

This file was deleted.

22 changes: 0 additions & 22 deletions src/main/java/vt/suopo/vlog/common/Constants.java

This file was deleted.

58 changes: 0 additions & 58 deletions src/main/java/vt/suopo/vlog/common/MethodWrapper.java

This file was deleted.

83 changes: 78 additions & 5 deletions src/main/java/vt/suopo/vlog/common/ReflectUtils.java
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -12,6 +15,7 @@
* All rights Reserved.
*/
public final class ReflectUtils {

private ReflectUtils() {}

/**
Expand Down Expand Up @@ -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;
}
}
Expand All @@ -88,7 +92,7 @@ public static <T> 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;
}
}
Expand All @@ -100,7 +104,7 @@ public static <T> 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;
}
}
Expand All @@ -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);
Expand All @@ -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<String, Field> FIELD_POOL_CACHE = new ConcurrentHashMap<>();
private static final Map<String, Method> 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> 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;
}
}
}

Expand Down
28 changes: 2 additions & 26 deletions src/main/java/vt/suopo/vlog/common/SystemUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand All @@ -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();
}

}

0 comments on commit 214220a

Please sign in to comment.