Android 4.4上的db4o。+:存储对象失败,因为无法将long转换为整数

时间:2014-07-25 23:47:17

标签: android db4o

运行db4o库(8.1-SNAPSHOT和8.0)时,打开数据库工作

oc = Db4oEmbedded.openFile(dbConfig(), db4oDBFullPath(context));    //context.getDir("data", 0) + "/" + "mDB.DB4O"; to get the full path

但是,一旦我开始尝试存储对象

  

oc.store(对象)

我收到错误cannot cast java.lang.Long to java.lang.Integer。这个问题可能是由新的Android操作系统版本的新dalvik实现引起的,但有没有针对此错误的补丁或解决方法?

一个人正在推出一个补丁here,但它无法访问。

错误的完整堆栈跟踪

  

java.lang.UnsupportedOperationException:java.lang.ClassCastException:java.lang.Long无法强制转换为java.lang.Integer    在com.db4o.internal.DalvikVM $ Dalvik3ObjectFactoryFactory。(DalvikVM.java:120)    在com.db4o.internal.DalvikVM.factory(DalvikVM.java:75)    在com.db4o.internal.DalvikVM.supportSkipConstructorCall(DalvikVM.java:55)    在com.db4o.internal.Platform4.callConstructor(Platform4.java:463)    在com.db4o.reflect.core.ConstructorSupport.createConstructor(ConstructorSupport.java:21)    在com.db4o.reflect.jdk.JdkClass.constructorSpec(JdkClass.java:176)    at com.db4o.reflect.jdk.JdkClass.ensureCanBeInstantiated(JdkClass.java:182)    at com.db4o.reflect.generic.GenericClass.ensureCanBeInstantiated(GenericClass.java:282)    at com.db4o.internal.ClassMetadata.createConstructor(ClassMetadata.java:684)    at com.db4o.internal.ClassMetadata.initializeConstructor(ClassMetadata.java:648)    at com.db4o.internal.ClassMetadata.initializeAspects(ClassMetadata.java:266)    at com.db4o.internal.ClassMetadata.checkChanges(ClassMetadata.java:494)    at com.db4o.internal.ClassMetadataRepository.readClassMetadata(ClassMetadataRepository.java:465)    at com.db4o.internal.ClassMetadataRepository.classMetadataForId(ClassMetadataRepository.java:259)    at com.db4o.internal.ObjectContainerBase.classMetadataForID(ObjectContainerBase.java:1069)    在com.db4o.internal.marshall.ObjectHeader。(ObjectHeader.java:39)    在com.db4o.internal.marshall.ObjectHeader。(ObjectHeader.java:23)    在com.db4o.internal.marshall.UnmarshallingContext.readObjectHeader(UnmarshallingContext.java:124)    at com.db4o.internal.marshall.UnmarshallingContext.read(UnmarshallingContext.java:47)    at com.db4o.internal.ObjectReference.read(ObjectReference.java:306)    at com.db4o.internal.ObjectReference.read(ObjectReference.java:292)    at com.db4o.internal.ObjectContainerBase.getByID2(ObjectContainerBase.java:886)    at com.db4o.internal.ObjectContainerBase.getByID(ObjectContainerBase.java:857)    在com.db4o.internal.fileheader.FileHeaderVariablePart.readIdentity(FileHeaderVariablePart.java:44)    在com.db4o.internal.fileheader.NewFileHeaderBase.readIdentity(NewFileHeaderBase.java:92)    at com.db4o.internal.LocalObjectContainer.readThis(LocalObjectContainer.java:497)    at com.db4o.internal.IoAdaptedObjectContainer.openImpl(IoAdaptedObjectContainer.java:71)    at com.db4o.internal.ObjectContainerBase $ 1.run(ObjectContainerBase.java:140)    在com.db4o.foundation.DynamicVariable.with(DynamicVariable.java:54)    在com.db4o.foundation.Environments.runWith(Environments.java:28)    at com.db4o.internal.ObjectContainerBase.withEnvironment(ObjectContainerBase.java:161)    at com.db4o.internal.ObjectContainerBase.open(ObjectContainerBase.java:131)    在com.db4o.internal.IoAdaptedObjectContainer。(IoAdaptedObjectContainer.java:35)    at com.db4o.internal.ObjectContainerFactory.openObjectContainer(ObjectContainerFactory.java:18)    在com.db4o.Db4oEmbedded.openFile(Db4oEmbedded.java:64)

更新

因此,在跟踪错误之后,事实证明在版本8.0中com.db4o.internal.DalvikVM内有从Long到Integer的可怕强制转换

  

int _methodId;     _methodId =(Integer)constructorIdMethod.invoke(null,   Object.class);

我试图通过将变量声明为long来解决这个问题。我重新编译并在Android上再次运行,我得到了另一个错误 UnsupportedOperationException: java.lang.NoSuchMethodException: newInstance [class java.lang.Class, int]

由此引起:

        try {
            _method = ObjectStreamClass.class.getDeclaredMethod("newInstance", Class.class, Integer.TYPE); 
            _method.setAccessible(true);
        } catch (Exception e) {
            throw new UnsupportedOperationException(e);
        } 
    }

有任何线索如何解决这个问题?

1 个答案:

答案 0 :(得分:3)

我设法通过在db4o 8.1

的代码中创建补丁来解决这个问题

您可以在this GITHUB repository中找到新的JAR文件和代码更改。还有一个用于在repo上测试库的演示。

我在com.db4o.internal.Platform4中更改了几行。基本上我改变了JDK的顺序来查找。

private static void createJdk() {


    Class<?>[] jdkFactories = {

            JDK_5.Factory.class,//1 switched both positions.
            DalvikVM.Factory.class,//2
            JDK_1_4.Factory.class,
            JDK_1_3.Factory.class,
            JDK_1_2.Factory.class,
            JDKReflect.Factory.class,
    };
    //-----