2016-04-04 14 views
0

私たちのプラットフォームでは、呼び出すために使用された引数のタイプの情報を知っているので、メソッドの正しいオーバーロードを選択する方法を考え出す必要があります。Java:適切なオーバーロードを選択した公開サービス

以前は、私たちのために適切なオーバーロードを選択するためにJavaラン自体に頼っていましたが、いくつかの変更点で、メソッドパラメータと引数タイプを考慮して、適切なオーバーロードを選択するアルゴリズムが必要になります。

私は、公開されているサービスなどがある場合、関数を呼び出すために使用された実際の引数と関数のパラメータとサービスの戻り値を渡すことができる場所があるかどうかを知りたいと思っています正しい関数インスタンスを比較して選択するために使用できるランクまたは何かを戻しますか?

1) Get all the methods defined with the same name and same number of parameters. These are our set of candidate functions. 
2) Once we have this set of candidate functions , we loop through each one of them and do a instance of check with the argument types and pick a function 
3) Use this picked function parameters to do an instance of checks with other function parameters that way we pick the top most subclass in case instance of passes for multiple method definitions (example provided below) 
4) In case we still have more than one candidate, we throw the ambigous method definition exception exactly the way that java does currently. 

例:

Class Exception2 extends Exception1 
Class Exception1 extends MyException 
Class MyException 

私は

func(Exception2 ex2) 
func(Exception1 ex1) 

として定義されたメソッドを持って、私たちは私が行う必要があります。この場合

func(new MyException()) 

を使用してこの機能を呼び出す場合必ずfunc(Exception2)geピックしたものとしなかったもの(例外1)

思考?

+0

'func(Exception1)'は、 'MyException'型のパラメータに対して適切なオーバーロードではありません。どちらも 'func(Exception2)'ではありません。 Javaで独自の過負荷を選択させることをお勧めします。 – RealSkeptic

答えて

0

リフレクションでクラスのメソッドを呼び出すことができます。完全なドキュメントはhere

ショート例を見つけることができます:

Class<?> c = Class.forName("my.package.MyClass"); 
Object t = c.newInstance(); 
c.getDeclaredMethods(); 
Method m ; // some method from the above list 
m.invoke(t, arg1, arg2 ..) 

あなたがそれを起動しようとする前に、あなたは、getParameterTypes()とその仮引数をチェックするように、名前、戻り値の型を方法についての詳細を調べることができます、などメソッドhereについてさらに詳しい情報があります。

関連する問題