Flexjson安全问题反序列化java.lang.Class,没有构造函数可访问

时间:2013-11-07 02:07:49

标签: java serialization deserialization flexjson

我可以序列化GenericContainer类型的对象,但是在反序列化时,我得到了一个安全异常(见下文)。我怀疑问题是java.lang.Class没有默认(没有arg)构造函数,Flexjson要求;有办法解决这个问题吗?

public class GenericContainer {

       private Map<Class<?>, Object> container = new HashMap<Class<?>, Object>();

       public static void main(String[] args) {
          GenericContainer gc = new GenericContainer();
          gc.setTypedValue(String.class, "Java");
          gc.setTypedValue(Integer.class, 123);

          JSONSerializer ser = new JSONSerializer();     
          ser.prettyPrint(true);      
          String json = ser.deepSerialize(gc);      

          JSONDeserializer<GenericContainer> der = new JSONDeserializer<GenericContainer>();  
          GenericContainer gc2 = der.deserialize(json);  //exception here
       }

       public <T> void setTypedValue(Class<T> klass, T thing) {
          container.put(klass, thing);
       }

       public <T> T getTypedValue(Class<T> klass) {
          return klass.cast(container.get(klass));
       }

       public Map<Class<?>, Object> getContainer() {
          return container;
       }

       public void setContainer(Map<Class<?>, Object> container) {
          this.container = container;
       }

...异常

Exception in thread "main" java.lang.SecurityException: Can not make a java.lang.Class constructor accessible
    at java.lang.reflect.AccessibleObject.setAccessible0(AccessibleObject.java:139)
    at java.lang.reflect.AccessibleObject.setAccessible(AccessibleObject.java:129)
    at flexjson.factories.BeanObjectFactory.instantiate(BeanObjectFactory.java:31)
    at flexjson.factories.BeanObjectFactory.instantiate(BeanObjectFactory.java:16)
    at flexjson.ObjectBinder.bind(ObjectBinder.java:95)
    at flexjson.ObjectBinder.bindIntoMap(ObjectBinder.java:123)
    at flexjson.factories.MapObjectFactory.instantiate(MapObjectFactory.java:18)
    at flexjson.ObjectBinder.bind(ObjectBinder.java:95)
    at flexjson.ObjectBinder.bindIntoObject(ObjectBinder.java:149)
    at flexjson.factories.BeanObjectFactory.instantiate(BeanObjectFactory.java:17)
    at flexjson.ObjectBinder.bind(ObjectBinder.java:95)
    at flexjson.ObjectBinder.bind(ObjectBinder.java:74)
    at flexjson.JSONDeserializer.deserialize(JSONDeserializer.java:158)
    at com.raffian.ztask.test.GenericContainer.main(GenericContainer.java:28)

0 个答案:

没有答案