質問

I have roughly the following code: (SpiceRequest is class from RoboSpice project)

public class MyRequest extends SpiceRequest<SparseArray<MyService>> {
    // ...
    public MyRequest() {
        // This will not work
        super(SparseArray<MyService>.class);
    }

    public SparseArray<MyService> loadDataFromNetwork() {
        // ...
    }
}

Is it possible to pass to other method Class object of generic type? (Sorry if it's trivial, I'm new to Java world)

EDIT More detailed explanation

I'm trying to replace AsyncTask witch loads data from network and returns SparseArray<MyService> with SpiceRequest from RoboSpice project. Witch requires providing T in it's constructor call.

  • MyService is not an Android "Service"

EDIT 2 Problem solved by small work-around, addition of new intermediate class:

public class Result extends SparseArray<MyService> {}

And passing: Result.class to super SpiceRequest constructor

役に立ちましたか?

解決

No, it is not possible for code in a generic class to know the class of its type parameter at run time. The type parameter is not visible in the byte code. This is called run time type erasure.

It's covered in great detail in the Java tutorial, starting at http://docs.oracle.com/javase/tutorial/java/generics/erasure.html

他のヒント

NO. If you want to know type in super() or in any method, you can use getClass().

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top