简体   繁体   English

未选中的强制转换:&#39;java.lang.Class <capture<?> &gt;&#39;to&#39;java.lang.Class <T>

[英]Unchecked cast: 'java.lang.Class<capture<?>>' to 'java.lang.Class<T>

I am trying to write the following method in my super class: 我想在我的超类中编写以下方法:

public <T extends Downloader> T getDownloader(Context context, Integer... positions) throws Exception {

    Class<T> mClass = (Class<T>)Class.forName(getDownloaderClassName());
    T downloader = mClass.cast(mClass.getConstructors()[0].newInstance(context));
    if (downloader != null)
        downloader.setPositions(positions);

    return downloader;
}

Yet, I don't know how to avoid AndroidStudio telling me 但是,我不知道如何避免AndroidStudio告诉我

Unchecked cast: 'java.lang.Class<capture<?>>' to 'java.lang.Class<T>

Either I get it regarding the first line, or, if I change 要么我得到它关于第一行,或者,如果我改变

Class<T> to Class<?>

Then I have to perform a direct cast 然后我必须进行直接演员表演

(T)mClass.getConstructors....

To be more precise, this super class is abstract, and getDownloaderClassName() is actually defined as follows: 更确切地说,这个超类是抽象的,getDownloaderClassName()实际上定义如下:

public abstract String getDownloaderClassName();

So that the daughter classes can choose with descendant of Downloader they need to retrieve using getDownloader(). 因此,子类可以选择Downloader的后代,他们需要使用getDownloader()进行检索。

Ok, I guess I am confused between T and ?, So I found a warning free way of doing it: 好吧,我想我在T和?之间感到困惑,所以我找到了一个无警告的方法:

   public Downloader getDownloader(Context context, Integer... positions) throws Exception {

    Class<? extends Downloader> mClass = getDownloaderClassName();
    Downloader downloader = mClass.cast(mClass.getConstructors()[0].newInstance(context));
    if (downloader != null)
        downloader.setPositions(positions);

    return downloader;
}

public abstract Class<? extends Downloader> getDownloaderClassName();

and then, the abstract method just becomes: 然后,抽象方法变成:

@Override
public Class<? extends Downloader> getDownloaderClassName() {
    return DemoDownloader.class;
}

in the descendants. 在后代。

I guess I'll just have to cast the result of getDownloader when I need specific fields or methods. 我想当我需要特定的字段或方法时,我只需要转换getDownloader的结果。

The signature public <T extends Downloader> T getDownloader(Context context, Integer... positions) is not type safe. 签名public <T extends Downloader> T getDownloader(Context context, Integer... positions)不是类型安全的。 It makes getDownloader a generic method, which means that it must work correctly no matter what the caller wants T to be, without knowing what T is . 它使getDownloader的通用方法,这意味着它必须正常工作,不管什么呼叫者想要T是, 不知道是什么T Note that T does not show up in any of the parameter types. 请注意, T不会显示在任何参数类型中。 This means that the same exact call with the same exact arguments, must somehow return type Downloader1 if that's what one caller wants and also return type Downloader2 if that's what another caller wants, without the getDownloader method having any information about what the caller wants! 这意味着具有相同精确参数的相同精确调用必须以某种方式返回类型Downloader1如果这是一个调用者想要的并且还返回类型Downloader2如果这是另一个调用者想要的,而没有getDownloader方法具有关于调用者想要什么的任何信息! This is clearly impossible, unless getDownloader always returns null . 除非getDownloader始终返回null ,否则这显然是不可能的。

The signature public Downloader getDownloader(Context context, Integer... positions) is different, because it says that the getDownloader method returns type Downloader . 签名public Downloader getDownloader(Context context, Integer... positions)是不同的,因为它表示getDownloader方法返回Downloader类型。 Your getDownloader method chooses the type of the thing to return (as long as it's a subtype of Downloader ); 你的getDownloader方法选择要返回的东西的类型(只要它是Downloader的子类型); the caller doesn't choose the type, and cannot make any assumptions about the thing returned except that it's an instance of Downloader . 调用者不选择类型,并且不能对返回的事物做任何假设,除非它是Downloader的一个实例。 That is type safe. 这是类型安全的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM