-つづき

protected Type findPageModelType(Class clazz1) {
  for(Type type2: clazz1.getGenericInterfaces()) {
    if(type2 instanceof Class) {
      Class clazz2 = (Class)type2;
      Type ret2 = findPageModelType(clazz2);
      if(ret2 != null) {
        return ret2;
      }
    } else if(type2 instanceof ParameterizedType) {
      ParameterizedType param2 = (ParameterizedType)type2;
      Type raw = param2.getRawType();
      if(raw.equals(ActionPage.class)) {
        Type[] types3 = param2.getActualTypeArguments();
        return types3[0];
      }
    }
  }
  return null;
}
    
public Class getPageModelClass(
    Class formPageClass) {
  Type type = findPageModelType(formPageClass);
  if(type == null) {
    return Object.class;
  if(type instanceof Class) {
    return Class.class.cast(type);
  }
  // TODO 総称型をモデルに指定したときはクラス型が取れない。
  throw new IllegalStateException();
}

先日の総称型のリフレクションですが。。。Typeは上記コード(findPageModelType)で取れるようです。しかし、Typeから確実にインスタンスを作る方法が無いのでちょっと制限ができてしまう。。。IndexPage extends ActionPage> みたいなParameterizedTypeがネストするとfindPageModelTypeの戻りはClassではないので、爆裂してしまいます。さらに辿って調べることはできるけど、生成したいモデルのクラス型は取れないようなのです。