2016-08-10 27 views
1

私のIDEはNCM_Callableは、このライン上Callable<ReturnInterface<? extends Object>>に変換することはできません」という不平を言っている私は、オブジェクトの任意の型を含むReturnInterfaceを返す私のECSに呼び出し可能オブジェクトを渡すことができるようにしたいthis.ph.submitCallable(new NCM_Callable(this, new DeviceGroup(this.deviceGroupNames.get(i)), ph)); In the "fetchDevices()" method Javaのジェネリック引き起こす問題

任意の助けをいただければ幸いです。私は<>一般的な定義の私の使い方に問題がある疑いがあるが、私はそれが何であるかを見つけ出すように見えることはできません。

@Override 
public void fetchDevices() throws Exception { 
    System.out.println("[NCM_Fetcher]fetchingDevices()"); 
    for (int i = 0; i < this.deviceGroupNames.size(); i++) { 
     System.out.println("[NCM_Fetcher]fetchingDevices().submitting DeviceGroup Callable " + i+ " of "+this.deviceGroupNames.size()); 
     this.ph.submitCallable(new NCM_Callable(this, new DeviceGroup(this.deviceGroupNames.get(i)), ph)); 
    } 
    this.ph.execute();//See progressBarhelper below 
} 

ProgressB

arHelper: "ecs.submit()"に奇妙なエラーがあります。私が読んだことから、ヘルパーメソッドが必要なようですね?どうすれば修正できますか?

public class ProgressBarHelper extends SwingWorker<Void, ReturnInterface> implements ActionListener { 
ExecutorService pool = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()); 

protected CompletionService<ReturnInterface<?>> ecs; 

public final void submitCallable(Callable<? extends ReturnInterface<?>> c) { 
    //create a map for this future 
    ecs.submit(c); 
    this.callables.add(c);//Error here is Callable<CAP#1 cannot be converted to Callable<ReturnInterface<?>> 
    System.out.println("[ProgressBarHelper]submitted"); 

} 
} 

最後に、NCM_CallableクラスはGenericsを使用します。

public class NCM_Callable implements Callable<ReturnInterface<ResultSet>>, ReturnInterface<ResultSet> { 
+0

まず[MCVE]作成:

最後に、これはあなたがこの機能を呼び出すことができる方法です。あなたはおそらくプロセスのあなたの問題を解決するでしょう。 – 4castle

+0

[Javaジェネリック、ネストされたワイルドカードのコレクション]の可能な複製(http://stackoverflow.com/questions/6293871/java-generics-nested-collection-of-wildcard) – 4castle

+0

重複していません。 –

答えて

0

私の知る限りがチェックできるよう、問題のある行がecs.submit次のとおりです。

public final void submitCallable(Callable<? extends ReturnInterface<?>> c) { 
    // create a map for this future 
    ecs.submit(c); // Error here is Callable<CAP#1 cannot be converted to Callable<ReturnInterface<?>> 
    this.callables.add(c); // this is fine on my machine 
} 

問題はJavaのジェネリック医薬品がinvariantであるということです。これを回避するには、あります:あなたは、名前のとワイルドカードを置き換えることができ、拘束タイプ

public final <T extends ReturnInterface<?>> void // this is how you name and bind the type 
    submitCallable(Callable<T> c) { // here you are referring to it 
    // create a map for this future 
    ecs.submit((Callable<ReturnInterface<?>>) c); // here you need to cast. 
    ... 
} 

は、鋳造がtype erasureによるランタイムで大丈夫です。しかし、それでは、あなたが何をするかに非常に注意してください。Callables

private static class ReturnInterfaceImpl implements ReturnInterface<String> { 

}; 

public void foo() { 
    Callable<ReturnInterfaceImpl> c = new Callable<ReturnInterfaceImpl>() { 
     @Override 
     public ReturnInterfaceImpl call() throws Exception { 
      return null; 
     } 
    }; 
    submitCallable(c); 
} 
+0

私は不安定になってしまい、呼び出し可能なものを受け入れてしまいました。誰かがなぜ私のコードがうまくいかないのかを説明することができたことは良いことです。 –

関連する問題