2017-04-21 2 views
0

Excelのシートから動的にJavaメソッドを渡し、別のJavaファイルから呼び出す方法はありますか?出来ますか? 以下のコード、ctrlNameとctrlValueをExcelシートから取り出しました。 GenericFunctionsのJava FIEで CTRLNAME = GenericFunctions.fg_wait ctrlValue = 5どのようにExcelのシートから動的にJavaメソッドを渡し、それらを異なるJavaファイルから呼び出すか?

public static void callFun(String ctrlName,String ctrlValue) { 
// TODO Auto-generated method stub 
    ctrlName(ctrlValue); 

私は以下の持っている:あなたはこの目標を達成するためにリフレクションを使用することができ

public class GenericFunctions extends GenericFunctionsHelper 
{ 

    public static void fg_wait(int seconds) 
    { 
     sleep(seconds); 
    } 
} 

答えて

0

。 Excelスプレッドシートのメソッド名がクラスのメソッドと一致しない場合は、例外が発生します。あなたはまた、私は簡単にするため、一般的なキャッチを使用している、特定の例外をキャッチしたいと思う:

String methodName = "methodToCall"; // get this from your Excel spreadsheet. 
    Integer value = 17; // Get this from your Excel spreadsheet. 
    GenericClass classInstance = new GenericClass(); 

    try { 
     Method method = GenericClass.class.getDeclaredMethod(methodName, Integer.class); 
     method.invoke(classInstance, value); 
    } catch (Exception e) { 
     // Handle the exception. 
    } 

あなたはこの道をつもりなら、あなたは何the documentation for Java's reflection library

+0

を検討したいと思います整数値= 17は??説明していただけますか? @RaceYouAnytime –

+0

Iは以下のように自分のコードを変更した: 'パブリック静的ボイドcallFun(文字列ctrlType、文字CTRLNAME、列ctrlValue){ \t \t \t列methodNameの= CTRLNAME。 \t \t文字列値= ctrlValue; \t \t GenericFunctions classInstance = new GenericFunctions(); \t \t try { \t \tメソッドメソッド= GenericFunctions.class.getDeclaredMethod(methodName、String.class); \t \t method.invoke(classInstance、value); \t \t}キャッチ(例外e){ \t \t \t System.err.println(E); \t \t}} ' 私は "java.lang.NoSuchMethodException:GenericFunctions.fg_wait(java.lang.Stringでは)" 取得していますが、この方法はGenericFunctions上に存在することを確信している –

+0

@GopinathRaviを支援する、と文字列を取りError..Please入力として? – RaceYouAnytime

関連する問題