コンストラクタのリフレクション
引数有りコンストラクタのリフレクション。
try{ // getConstructorの引数は、リクレクションするクラスコンストラクタ引数の型 Constructor ct = hogeclass.getConstructor( arg.getClass() ); // new hogeclass(arg) と同意義。 return ct.newInstance(arg); } catch( NoSuchMethodException ex ){ ex.printStackTrace(); return null; } catch( Exception ex2 ){ ex2.printStackTrace(); return null; }
Class.getConstructorの引数、及びConstructor.newInstanceの引数は可変個なので、
複数の引数を持つコンストラクタにも対応している。
上記例は、wicketの共通Panel内で、任意のWebPageクラスインスタンスを生成する為に実装した例です。