超出无处ClassCastException异常在android系统 [英] Out-of-nowhere ClassCastException in android

查看:154
本文介绍了超出无处ClassCastException异常在android系统的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天早上,我醒来的时候对这个奇怪的异常:

This morning I woke up on this strange exception:

java.lang.ClassCastException:
  org.apache.harmony.luni.lang.reflect.ImplForVariable不能转换为
  的java.lang.Class

java.lang.ClassCastException: org.apache.harmony.luni.lang.reflect.ImplForVariable cannot be cast to java.lang.Class

当我试图得到一个类型参数类它发生。

It happens when I try to get a Type argument Class.

例如:

public static Class getTypeParameterClass(Object o, int index) {
    return (Class)
            ((ParameterizedType)o.getClass()
                    .getGenericSuperclass())
                    .getActualTypeArguments()[index];
}

正如你所看到的,这种方法获取给定对象的类的类型参数之一。

As you can see, this method gets one of the type parameter for a given object's class.

现在它不过今天已经工作了几个月,就停止了。

It's been working for months now but today, it stopped.

用法示例:

Collection<Object> foo = new ArrayList<Object>();

Class<?> fooClass = Utils.getTypeParameterClass(foo, 0);

和它崩溃。

有没有java的指责太多次对我的诅咒;)

Is there a curse on me for blaming java too many times;) ?

谢谢!

推荐答案

所以,首先,让我解决一个巨大的谢谢,以灰色为他的耐心。

So, first of all, let me address a huge "thank you" to Gray for his patience.

我终于想通了什么事。

我是专注于错误的领域。

I was focusing on the wrong field.

所以,作为格雷指出,由于类型擦除你不能让一个类型参数未明确在编译时给出。

So, as Gray points out, because of type erasure you can't get a type parameter that is not explicitly given at compile time.

我是这么说服的错误是在集合场,我完全忘了其他领域。

I was so persuaded that the error was on a Collection field that I totally forgot about other fields.

所以,因为我的数据来自web服务,而且由于JSON映射器不能直接获取列表为根,我不得不定义包含对象的集合,一个日期(缓存),很少与其他机型的事情分页。

So, because my data come from webservices, and because the JSON mapper can't directly get a list as root, I had to define models containing a Collection of objects, a date (for caching) and few other things related to paging.

所以,因为写这些相似的类是废话,我试图概括的过程中,像这样:

So, because writing all these similar classes was a nonsense, I tried to generalize the process, like so:

public abstract class ModelList<T> {

        [ paging and caching stuffs ]

        @ForeignCollectionField
        Collection<T> collection;

        [ constructor, getters, setters ]

}

和则只有有这样的子类:

and then only have subclasses like:

@DatabaseTable(tableName = "category_list")
public class CategoryList extends ModelList<Category> {

}

所以我可以有更简洁班,所以我可以概括使用ObjectPersister从Robospice缓存过程。

So I could have less verbose classes, and so I could generalize the caching process using ObjectPersister from Robospice.

我不明白(因为缺乏知识),是该参数将不会在运行时发现!

What I couldn't understand (because of a lack of knowledge), is that the parameter would not be found at run time !

什么困惑我更是我进行的测试中,试图重现错误,正在生产的bug!但他们错了。

What confused me even more is that the tests I ran to try to reproduce the bug, WERE producing the bug ! but they were wrong too.

那么,提醒自己(和其他像我这样做,可能会面临完全是一派胡言):

So, note to self (and others that may face complete nonsense like I did):


  • 请不要在语言失去信心(它不是越野车,这不是车,这不是马车......)

  • 请不要责怪的语言(不要太多)

  • 一贯重视来源罐子和设置断点

在最后的解决方案是非常简单,只需从ModelList移动​​采集到sublasses,并保持getter和setter作为ModelList抽象方法(为通用ObjectPersisters)。

At the end the solution was quite simple, just move the collection from the ModelList to the sublasses, and keep the getters and setters as abstract methods in ModelList (for the generic ObjectPersisters).

我觉得这一切有点太复杂了,如果有人知道这种情况下使用ormlite /杰克逊/ robospice我会很高兴听到它一个更好的解决方案。

I think it all got a little too complicated, if someone knows a better solution for this case using ormlite/jackson/robospice I'd be glad to hear it.

这篇关于超出无处ClassCastException异常在android系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆