-総称型のリフレクション
public intarface ActionFormPage< T > { }
というAPIに対して、
public intarface IndexPage extends ActionFormPage< IndexModel > { }
というユーザー定義インターフェイスがあったとして、このIndexModelの型をリフレクションで取りたい場合、
public Class getModelClass(
Class< ? extends ActionFormPage > formPageClass) {
Type types = formPageClass.getGenericInterfaces();
if(types.length == 1 && types[0] instanceof ParameterizedType) {
ParameterizedType param = (ParameterizedType)types[0];
Type actuals = param.getActualTypeArguments();
if(actuals.length == 1 && actuals[0] instanceof Class) {
return (Class)actuals[0];
}
}
throw new IllegalStateException();
}
とすると取れるけど、もっとスマートなやり方はないものか。。。さらには、これだと、
public interface ExIndexPage extends IndexPage { }
だと取れない(ま、スーパーインターフェイスを検査すればいいんだけど)。