Skip to content

Commit

Permalink
update version v0.0.20
Browse files Browse the repository at this point in the history
  • Loading branch information
wycst committed Dec 1, 2024
1 parent 8a26242 commit eb4c701
Show file tree
Hide file tree
Showing 30 changed files with 1,584 additions and 1,398 deletions.
21 changes: 11 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ WAST是一个高性能Java工具集库包,包括JSON、YAML、CSV、HttpClient
<dependency>
<groupId>io.github.wycst</groupId>
<artifactId>wast</artifactId>
<version>0.0.19</version>
<version>0.0.20</version>
</dependency>
```

## JSON

> 1 java语言整体性能最快的json库之一;<br>
> 2 支持IO流文件读写,JSON节点树按需解析,按需解析,序列化格式化,驼峰下划线自动转换;<br>
> 2 支持IO流文件读写,JSON节点树按需解析,序列化格式化,驼峰下划线自动转换,实体类解析绑定;<br>
> 3 支持自定义序列化和反序列化;<br>
> 4 支持JSON的xpath提取功能;<br>
> 5 没有漏洞风险;<br>
Expand All @@ -45,7 +45,7 @@ WAST是一个高性能Java工具集库包,包括JSON、YAML、CSV、HttpClient
> 1 java表达式引擎性能最快之一;<br>
> 2 支持java中所有的操作运算符(加减乘除余,位运算,逻辑运算,字符串+);<br>
> 3 支持**指数运算(java本身不支持); <br>
> 4 支持函数以及自定义函数实现,函数可以任意嵌套; <br>
> 4 支持函数以及自定义函数实现; <br>
> 5 科学记数法支持,16进制,8进制等解析,支持大数运算(BigDecimal);<br>
> 6 支持三目运算;<br>
> 7 没有漏洞风险;<br>
Expand Down Expand Up @@ -173,12 +173,11 @@ System.out.println(map);

### 强大的JSONNode功能

> 1、支持对大文本json的懒加载解析功能,按需解析,当需要读取一个大文本json中一个或多个属性值时非常有用;<br>
> 2、支持按需解析;<br>
> 3、支持上下文查找;<br>
> 4、支持在大文本json中提取部分内容作为解析上下文结果,使用JSONNode.from(source, path);<br>
> 5、支持对节点的属性修改,删除等,节点的JSON反向序列化;<br>
> 6、支持直接提取功能(v0.0.2+支持),参考JSONPath;
> 1、支持对大文本json的懒加载按需解析功能,当需要读取一个大文本json中一个或多个属性值时非常有用;<br>
> 2、支持上下文查找;<br>
> 3、支持在大文本json中提取部分内容作为解析上下文结果,使用JSONNode.from(source, path);<br>
> 4、支持对节点的属性修改,删除等,节点的JSON反向序列化;<br>
> 5、支持提取功能(v0.0.2+支持),参考JSONPath;
使用'/'作为路径的分隔符,数组下标使用n访问支持*, n+, n-,n等复合下标访问,例如/store/book/*/author

Expand Down Expand Up @@ -267,8 +266,10 @@ System.out.println(map);
List authors = JSONNode.extract(json2, "/store/book/1-/author").get(1);
// 提取从第2本书开始后面所有的作者使用下标n+(包含n)
List authors = JSONNode.extract(json2, "/store/book/1+/author");
List authors1 = JSONNode.extract(json2, "/store/book/1+/author");
// 使用collect可以支持递归过滤
List authors2 = JSONNode.collect(json2, "//book/1+/author");
```

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>io.github.wycst</groupId>
<artifactId>wast</artifactId>
<version>0.0.19</version>
<version>0.0.20</version>
<modelVersion>4.0.0</modelVersion>
<packaging>jar</packaging>
<description>Wast is a high-performance Java toolset library package that includes JSON, YAML, CSV, HttpClient, JDBC and EL engines</description>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,14 @@ public class JDKCompiler {
private static final StandardJavaFileManager FILE_MANAGER = JAVA_COMPILER.getStandardFileManager(null, null, null);

public synchronized static Class<?> compileJavaSource(JavaSourceObject sourceObject) {
return compileJavaSource(sourceObject, ClassLoader.getSystemClassLoader());
}

public synchronized static Class<?> compileJavaSource(JavaSourceObject sourceObject, ClassLoader classLoader) {
MemoryJavaFileManager javaFileManager = new MemoryJavaFileManager(FILE_MANAGER);
try {
List<String> options = null;
if(EnvUtils.JDK_16_PLUS) {
if (EnvUtils.JDK_16_PLUS) {
options = Arrays.asList("-encoding", "UTF-8", "-XDuseUnsharedTable", "-Xlint:-options");
} else {
options = Arrays.asList("-encoding", "UTF-8", "-XDuseUnsharedTable");
Expand All @@ -34,7 +38,7 @@ public synchronized static Class<?> compileJavaSource(JavaSourceObject sourceObj
boolean bl = task.call();
if (bl) {
MemoryJavaFileObject memoryJavaFileObject = javaFileManager.getLastMemoryJavaFileObject();
MemoryClassLoader memoryClassLoader = new MemoryClassLoader(memoryJavaFileObject);
MemoryClassLoader memoryClassLoader = new MemoryClassLoader(memoryJavaFileObject, classLoader);
return memoryClassLoader.loadClass(sourceObject.packageName + "." + sourceObject.className);
} else {
throw new Exception("ERROR");
Expand All @@ -51,6 +55,10 @@ public synchronized static Class<?> compileJavaSource(JavaSourceObject sourceObj
}

public static List<Class<?>> compileJavaSources(JavaSourceObject... sourceObjects) {
return compileJavaSources(ClassLoader.getSystemClassLoader(), sourceObjects);
}

public synchronized static List<Class<?>> compileJavaSources(ClassLoader classLoader, JavaSourceObject... sourceObjects) {
MemoryJavaFileManager javaFileManager = new MemoryJavaFileManager(FILE_MANAGER, sourceObjects);
try {
List<Class<?>> targetList = new ArrayList<Class<?>>();
Expand All @@ -64,7 +72,7 @@ public static List<Class<?>> compileJavaSources(JavaSourceObject... sourceObject
boolean bl = task.call();
if (bl) {
Map<String, MemoryJavaFileObject> memoryJavaFileObject = javaFileManager.getFileObjectMap();
MemoryClassLoader memoryClassLoader = new MemoryClassLoader();
MemoryClassLoader memoryClassLoader = new MemoryClassLoader(classLoader);
Set<Map.Entry<String, MemoryJavaFileObject>> entrySet = memoryJavaFileObject.entrySet();
for (Map.Entry<String, MemoryJavaFileObject> entry : entrySet) {
targetList.add(memoryClassLoader.loadClass(entry.getKey(), entry.getValue().getBytes()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,24 @@ public class MemoryClassLoader extends ClassLoader {
private MemoryJavaFileObject javaFileObject;

public MemoryClassLoader(MemoryJavaFileObject javaFileObject) {
this(javaFileObject, ClassLoader.getSystemClassLoader());
}

public MemoryClassLoader(MemoryJavaFileObject javaFileObject, ClassLoader parentLoader) {
super(parentLoader);
this.javaFileObject = javaFileObject;
}

public MemoryClassLoader() {
}

public MemoryClassLoader(ClassLoader parentLoader) {
super(parentLoader);
}

@Override
protected Class<?> findClass(String name) throws ClassNotFoundException {
if(javaFileObject == null) return null;
if (javaFileObject == null) return null;
byte[] codeBytes = javaFileObject.getBytes();
return defineClass(name, codeBytes, 0, codeBytes.length);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public final class ClassStrucWrap {
private ClassStrucWrap(Class<?> sourceClass) {
this.sourceClass = sourceClass;
this.privateFlag = Modifier.isPrivate(sourceClass.getModifiers());
this.publicFlag = Modifier.isPublic(sourceClass.getModifiers());
this.assignableFromMap = Map.class.isAssignableFrom(sourceClass);

Map<Class<? extends Annotation>, Annotation> annotationMap = new HashMap<Class<? extends Annotation>, Annotation>();
Expand All @@ -65,6 +66,7 @@ private ClassStrucWrap(Class<?> sourceClass) {
// jdk invoke
private final Class<?> sourceClass;
private final boolean privateFlag;
private final boolean publicFlag;
private final boolean assignableFromMap;
private final Map<Class<? extends Annotation>, Annotation> annotationMap;

Expand Down Expand Up @@ -300,6 +302,7 @@ private static ClassStrucWrap createBy(Class<?> sourceClass) {
return wrapper;
}

// record use getter method and parameters construction
private static void wrapperWithRecordConstructor(ClassStrucWrap wrapper, Map<String, Class<?>> superGenericClassMap) {
// sourceClass
Class<?> sourceClass = wrapper.sourceClass;
Expand Down Expand Up @@ -351,15 +354,15 @@ private static void wrapperWithRecordConstructor(ClassStrucWrap wrapper, Map<Str
fieldInfo.setIndex(i);

Field nameField = sourceClass.getDeclaredField(name);
Method nameMethod = sourceClass.getDeclaredMethod(name);
setAccessible(nameField);
clearFinalModifiers(nameField);
Method nameMethod = sourceClass.getDeclaredMethod(name);
setAccessible(nameMethod);

Class<?> fieldType = nameField.getType();
constructorArgs[i] = defaulTypeValue(fieldType);

// 构建getter
GetterInfo getterInfo = new GetterInfo();
GetterInfo getterInfo = new GetterMethodInfo(nameMethod);
getterInfo.setField(nameField);
getterInfo.setRecord(true);

Expand All @@ -373,7 +376,7 @@ private static void wrapperWithRecordConstructor(ClassStrucWrap wrapper, Map<Str
getterInfoOfFields.add(getterInfo);

// 构建setter
SetterInfo setterInfo = SetterInfo.fromField(nameField);
SetterInfo setterInfo = new SetterInfo.FieldImpl();
setterInfo.setName(name);
setterInfo.setField(nameField);
setterInfo.setParameterType(fieldType);
Expand Down Expand Up @@ -628,7 +631,7 @@ private void checkClassStructure() {

// jdk17 java.lang.Record
Class<?> theSuperClass = sourceClass.getSuperclass();
if(theSuperClass == null) {
if (theSuperClass == null) {
return;
}
if (theSuperClass.getName().equals("java.lang.Record")) {
Expand Down Expand Up @@ -855,6 +858,19 @@ private static void parseWrapperFields(ClassStrucWrap wrapper, Class<?> sourceCl
field = Field.class.getDeclaredField("modifiers");
setAccessible(field);
} catch (Exception e) {
try {
Method getDeclaredFields0 = Class.class.getDeclaredMethod("getDeclaredFields0", boolean.class);
setAccessible(getDeclaredFields0);
Field[] fields = (Field[]) getDeclaredFields0.invoke(Field.class, false);
for (Field target : fields) {
if ("modifiers".equals(target.getName())) {
field = target;
setAccessible(field);
break;
}
}
} catch (Throwable throwable) {
}
}
modifierField = field;

Expand Down Expand Up @@ -994,6 +1010,10 @@ public boolean isPrivate() {
return privateFlag;
}

public boolean isPublic() {
return publicFlag;
}

public boolean isJavaBuiltInModule() {
return javaBuiltInModule;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ Object invokeInternal(Object target) {
void setField(Field field) {
this.field = field;
try {
this.fieldOffset = UnsafeHelper.objectFieldOffset(field);
this.fieldPrimitive = field.getType().isPrimitive();
this.fieldOffset = UnsafeHelper.objectFieldOffset(field);
if (this.fieldPrimitive) {
this.primitiveType = ReflectConsts.PrimitiveType.typeOf(field.getType());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ public Class<?> getReturnType() {
return method.getReturnType();
}

public boolean isPrimitive() {
return method.getReturnType().isPrimitive();
}

@Override
public String getMethodName() {
return method.getName();
Expand Down
40 changes: 29 additions & 11 deletions src/main/java/io/github/wycst/wast/common/reflect/SetterInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
public class SetterInfo {

// field of class
private Field field;
Field field;
// field memory offset
long fieldOffset = -1;
private String name;
Expand Down Expand Up @@ -40,7 +40,7 @@ public class SetterInfo {

public static SetterInfo fromField(Field field) {
boolean primitive = field.getType().isPrimitive();
SetterInfo setterInfo = primitive ? new SetterInfo.PrimitiveSetterInfo() : new SetterInfo();
SetterInfo setterInfo = primitive ? new PrimitiveImpl() : new SetterInfo();
setterInfo.setField(field);
return setterInfo;
}
Expand All @@ -52,14 +52,14 @@ public final boolean isInstance(Object target) {
public void invoke(Object target, Object value) {
target.getClass();
if ((parameterType.isInstance(value) || value == null) && isInstance(target)) {
UnsafeHelper.UNSAFE.putObject(target, fieldOffset, value);
invokeInternal(target, value);
} else {
throw new SecurityException("invoke error: parameter mismatch");
}
}

void invokeInternal(Object target, Object value) {
UnsafeHelper.UNSAFE.putObject(target, fieldOffset, value); // UnsafeHelper.putObjectValue(target, fieldOffset, value);
UnsafeHelper.UNSAFE.putObject(target, fieldOffset, value);
}

public final String getName() {
Expand Down Expand Up @@ -94,10 +94,6 @@ void setNonInstanceType(boolean nonInstanceType) {
this.nonInstanceType = nonInstanceType;
}

// Map<Class<? extends Annotation>, Annotation> getAnnotations() {
// return annotations;
// }

void setAnnotations(Map<Class<? extends Annotation>, Annotation> annotations) {
this.annotations = annotations;
}
Expand Down Expand Up @@ -135,7 +131,7 @@ Object getDefaultFieldValue(Object instance) {
return null;
}

Object getFieldValue(Object instance) {
Object getFieldValue(Object instance) throws IllegalAccessException {
return UnsafeHelper.getObjectValue(instance, fieldOffset);
}

Expand Down Expand Up @@ -167,11 +163,33 @@ public boolean isFieldDisabled() {
return fieldDisabled;
}

static final class PrimitiveSetterInfo extends SetterInfo {
static final class FieldImpl extends SetterInfo {
void setField(Field field) {
this.field = field;
}

public void invoke(Object target, Object value) {
invokeInternal(target, value);
}

Object getFieldValue(Object instance) throws IllegalAccessException {
return field.get(instance);
}

void invokeInternal(Object target, Object value) {
try {
field.set(target, value);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
}

static final class PrimitiveImpl extends SetterInfo {
private ReflectConsts.PrimitiveType primitiveType;

public void invoke(Object target, Object value) {
if(isInstance(target)) {
if (isInstance(target)) {
primitiveType.put(target, fieldOffset, value);
} else {
throw new SecurityException("invoke error: parameter mismatch");
Expand Down
Loading

0 comments on commit eb4c701

Please sign in to comment.