2017-01-03 8 views
0

Javaリフレクション用のユーティリティクラスを作成しました。メソッド名が引数として が渡された場合、私の意図は、Collectonの値を返す必要があります。ユーティリティクラスは、Eclipse Pluginプロジェクト(com.abc.utility)で作成されます。 このユーティリティクラスを別のプラグインプロジェクト(com.abc.dao)に追加しました。今私はこのユーティリティメソッドを呼び出すと、私はClassNotFoundExceptionを得ています。 私は問題を理解しました。私はクラスへの依存性をcom.abc.utilityプロジェクトに追加したくありません。むしろ com.abc.utilityプラグインプロジェクトは他のプロジェクトに依存関係として追加する必要があります。classnotfound例外リフレクション

私は解決方法を知らない。これで私を助けてください。

@SuppressWarnings({ "unchecked", "rawtypes" }) 
    public <K>Collection<K> getCollection(T t, String methodName) { 

     Method[] methods =t.getClass().getMethods();   

     for (Method method : methods) {   
      String name = method.getName(); 
      if (name.equals(methodName)) { 
       String name2 = method.getReturnType().getName(); 
       Object newInstance = null; 
       try { 
        newInstance = Class.forName(name2).newInstance(); 
       } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e1) { 
        e1.printStackTrace(); 
       } 
       if (newInstance instanceof Collection) { 
        try { 
         return (Collection)method.invoke(t, null); 
        } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { 
         e.printStackTrace(); 
        } 
       } 

      } 
     } 

     return Collections.emptyList(); 
    } 

答えて

0

これはできません。各Eclipseプラグインには別々のクラスパスがありますので、プラグインでcom.abc.utilityClass.forNameはプラグインのクラスまたはその依存関係でのみ機能します。他のクラスはプラグインのクラスパスに含まれず、それらを見つけることができません。

クラスを含むプラグインが分かっている場合にのみ、他のプラグインでクラスを読み込むことができます。これを行うには、使用:

Bundle bundle = ... bundle for the plugin containing the class 

Class<?> theClass = bundle.loadClass("class name"); 

は、あなたが使用してプラグインIDからBundleを取得することができます。

Bundle bundle = Platform.getBundle("plugin id"); 
関連する問題