mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2025-06-28 13:16:19 +08:00
🎨 #1558 修复Java9及以上版本报非法反射警告信息的问题
Co-authored-by: duanhongyi <duanhongyi@doopai.com>
This commit is contained in:
parent
92ea01e371
commit
609b38a9db
@ -1,13 +1,18 @@
|
|||||||
package me.chanjar.weixin.common.util.xml;
|
package me.chanjar.weixin.common.util.xml;
|
||||||
|
|
||||||
import java.io.Writer;
|
|
||||||
|
|
||||||
import com.thoughtworks.xstream.XStream;
|
import com.thoughtworks.xstream.XStream;
|
||||||
|
import com.thoughtworks.xstream.converters.basic.*;
|
||||||
|
import com.thoughtworks.xstream.converters.collections.CollectionConverter;
|
||||||
import com.thoughtworks.xstream.converters.reflection.PureJavaReflectionProvider;
|
import com.thoughtworks.xstream.converters.reflection.PureJavaReflectionProvider;
|
||||||
|
import com.thoughtworks.xstream.converters.reflection.ReflectionConverter;
|
||||||
import com.thoughtworks.xstream.core.util.QuickWriter;
|
import com.thoughtworks.xstream.core.util.QuickWriter;
|
||||||
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
|
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
|
||||||
import com.thoughtworks.xstream.io.xml.PrettyPrintWriter;
|
import com.thoughtworks.xstream.io.xml.PrettyPrintWriter;
|
||||||
import com.thoughtworks.xstream.io.xml.XppDriver;
|
import com.thoughtworks.xstream.io.xml.XppDriver;
|
||||||
|
import com.thoughtworks.xstream.security.NoTypePermission;
|
||||||
|
import com.thoughtworks.xstream.security.WildcardTypePermission;
|
||||||
|
|
||||||
|
import java.io.Writer;
|
||||||
|
|
||||||
public class XStreamInitializer {
|
public class XStreamInitializer {
|
||||||
private static final XppDriver XPP_DRIVER = new XppDriver() {
|
private static final XppDriver XPP_DRIVER = new XppDriver() {
|
||||||
@ -41,14 +46,34 @@ public class XStreamInitializer {
|
|||||||
};
|
};
|
||||||
|
|
||||||
public static XStream getInstance() {
|
public static XStream getInstance() {
|
||||||
XStream xstream = new XStream(new PureJavaReflectionProvider(), XPP_DRIVER);
|
XStream xstream = new XStream(new PureJavaReflectionProvider(), XPP_DRIVER) {
|
||||||
|
// only register the converters we need; other converters generate a private access warning in the console on Java9+...
|
||||||
|
@Override
|
||||||
|
protected void setupConverters() {
|
||||||
|
registerConverter(new NullConverter(), PRIORITY_VERY_HIGH);
|
||||||
|
registerConverter(new IntConverter(), PRIORITY_NORMAL);
|
||||||
|
registerConverter(new FloatConverter(), PRIORITY_NORMAL);
|
||||||
|
registerConverter(new DoubleConverter(), PRIORITY_NORMAL);
|
||||||
|
registerConverter(new LongConverter(), PRIORITY_NORMAL);
|
||||||
|
registerConverter(new ShortConverter(), PRIORITY_NORMAL);
|
||||||
|
registerConverter(new BooleanConverter(), PRIORITY_NORMAL);
|
||||||
|
registerConverter(new ByteConverter(), PRIORITY_NORMAL);
|
||||||
|
registerConverter(new StringConverter(), PRIORITY_NORMAL);
|
||||||
|
registerConverter(new DateConverter(), PRIORITY_NORMAL);
|
||||||
|
registerConverter(new CollectionConverter(getMapper()), PRIORITY_NORMAL);
|
||||||
|
registerConverter(new ReflectionConverter(getMapper(), getReflectionProvider()), PRIORITY_VERY_LOW);
|
||||||
|
}
|
||||||
|
};
|
||||||
xstream.ignoreUnknownElements();
|
xstream.ignoreUnknownElements();
|
||||||
xstream.setMode(XStream.NO_REFERENCES);
|
xstream.setMode(XStream.NO_REFERENCES);
|
||||||
XStream.setupDefaultSecurity(xstream);
|
XStream.setupDefaultSecurity(xstream);
|
||||||
xstream.allowTypesByWildcard(new String[]{
|
xstream.autodetectAnnotations(true);
|
||||||
"me.chanjar.weixin.**", "cn.binarywang.wx.**", "com.github.binarywang.**"
|
|
||||||
});
|
|
||||||
|
|
||||||
|
// setup proper security by limiting which classes can be loaded by XStream
|
||||||
|
xstream.addPermission(NoTypePermission.NONE);
|
||||||
|
xstream.addPermission(new WildcardTypePermission(new String[]{
|
||||||
|
"me.chanjar.weixin.**", "cn.binarywang.wx.**", "com.github.binarywang.**"
|
||||||
|
}));
|
||||||
xstream.setClassLoader(Thread.currentThread().getContextClassLoader());
|
xstream.setClassLoader(Thread.currentThread().getContextClassLoader());
|
||||||
return xstream;
|
return xstream;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user