public class GenericsUtil extends Object
Generics
を扱うためのユーティリティクラス。コンストラクタと説明 |
---|
GenericsUtil() |
修飾子とタイプ | メソッドと説明 |
---|---|
protected static <T> boolean |
checkInterfaceAncestors(Class<T> genericClass,
List<ParameterizedType> ancestorTypeList,
Class clazz)
インタフェース型から型パラメータを宣言したクラスまでの
ParameterizedType のリストを取得する。 |
protected static <T> boolean |
checkParameterizedType(Type type,
Class<T> genericClass,
List<ParameterizedType> ancestorTypeList)
Type 型をチェックし、ParameterizedType
かつ、型パラメータを宣言したクラスのサブクラスである場合、リストに追加する。 |
protected static <T> List<ParameterizedType> |
getAncestorTypeList(Class<T> genericClass,
Class<? extends T> descendantClass)
特定の型から型パラメータを宣言したクラスまでの
ParameterizedType のリストを取得する。 |
protected static Class |
getRawClass(Type type)
引数
type の実際の型を返却する。 |
protected static boolean |
isNotTypeVariable(Type type)
引数
type がClass 型
であるか、TypeVariable 型かを判定する。 |
static <T> Class[] |
resolveParameterizedClass(Class<T> genericClass,
Class<? extends T> descendantClass)
クラスの型パラメータの実際の型を取得する。
|
static <T> Class |
resolveParameterizedClass(Class<T> genericClass,
Class<? extends T> descendantClass,
int index)
型パラメータの実際の型を取得する。
|
protected static Class |
resolveTypeVariable(Type type,
List<ParameterizedType> ancestorTypeList)
型パラメータの具体的な
Type を取得する。 |
public static <T> Class[] resolveParameterizedClass(Class<T> genericClass, Class<? extends T> descendantClass) throws IllegalArgumentException, IllegalStateException
WildCardType
が
指定されている場合、および、コード中で変数宣言の際に実際の型が
指定されている場合は取得できない。
public class Descendant extends Generic<String, Integer> {
...
}
この場合、正しく[String
, Integer
]の
配列が取得できる
public class Descendant extends Generic<String[], Integer> {
...
}
この場合、正しく[String[]
, Integer
]の
配列が取得できる
public class Descendant
extends Generic<String[], Map<String, Object>> {
...
}
この場合、正しく[String[]
, Map
]の
配列が取得できる
public class Descendant<P, Q> extends Generic<S, T> {
...
}
public class Descendant<P extends String, Q super Bean>
extends Generic<S, T> {
...
}
Generic<String, Integer> descendant =
new Generic<String, Integer>();
genericType
からdescendantClass
まで
多世代の継承がある場合も、実際の型を取得することができる。
public class Child<S, T> extends Generic<S, T> {
...
}
public class GrandChild<S, T> extends Child<S, T> {
...
}
public class Descendant extends GrandChild<String, Integer> {
...
}
この場合、正しく[String
, Integer
]の
配列が取得できる
genericType
からdescendantClass
までの
継承の過程で型パラメータの順番が入れ替わった場合でも、
genercClass
で宣言された順番で実際の型を取得できる。
public class Generic<S, T> {
...
}
public class Child<A, T, B, S> extends Generic<S, T> {
...
}
public class Descendant
extends Generic<Boolean, Integer, Double, String> {
...
}
この場合、正しく[String
, Integer
]の
配列が取得できる
T
- 型パラメータ宣言をしたクラスの型。genericClass
- 型パラメータ宣言をしたクラス。descendantClass
- genericsClass
を継承し、
具体的な型パラメータを指定したクラス。Class
インスタンスの配列。
順番はgenercClass
で宣言された順番である。IllegalArgumentException
- 引数genericClass
が
null
の場合。
引数descendantClass
がnull
の場合。IllegalStateException
- descendantClass
の実装で型パラメータが
具体クラスとして指定されていない場合。
引数genercClass
が型パラメータを宣言したクラスでは
なかった場合。public static <T> Class resolveParameterizedClass(Class<T> genericClass, Class<? extends T> descendantClass, int index) throws IllegalArgumentException, IllegalStateException
WildCardType
が
指定されている場合、および、コード中で変数宣言の際に実際の型が
指定されている場合は取得できない。
public class Descendant extends Generic<String, Integer> {
...
}
この場合、正しくString
またはInteger
が
取得できる
public class Descendant extends Generic<String[], Integer> {
...
}
この場合、正しくString[]
またはInteger
が
取得できる
public class Descendant
extends Generic<String[], Map<String, Object>> {
...
}
この場合、正しくString[]
またはMap
が取得できる
public class Descendant<P, Q> extends Generic<S, T> {
...
}
public class Descendant<P extends String, Q super Bean>
extends Generic<S, T> {
...
}
Generic<String, Integer> descendant =
new Generic<String, Integer>();
genericType
からdescendantClass
まで
多世代の継承がある場合も、実際の型を取得することができる。
public class Child<S, T> extends Generic<S, T> {
...
}
public class GrandChild<S, T> extends Child<S, T> {
...
}
public class Descendant extends GrandChild<String, Integer> {
...
}
この場合、正しくString
またはInteger
が取得できる
genericType
からdescendantClass
までの
継承の過程で型パラメータの順番が入れ替わった場合でも、
genercClass
で宣言された順番で実際の型を取得できる。
public class Generic<S, T> {
...
}
public class Child<A, T, B, S> extends Generic<S, T> {
...
}
public class Descendant
extends Generic<Boolean, Integer, Double, String> {
...
}
この場合、正しくString
またはInteger
が取得できる
T
- 型パラメータ宣言をしたクラスの型。genericClass
- 型パラメータ宣言をしたクラス。descendantClass
- genericsClass
を継承し、
具体的な型パラメータを指定したクラス。index
- 実際の型を取得する型パラメータの宣言順序。Class
インスタンス。IllegalArgumentException
- 引数genericClass
が
null
の場合。
引数descendantClass
がnull
の場合。
引数index
が0
より小さい、または、
宣言された型パラメータ数以上の場合。IllegalStateException
- descendantClass
の実装で型パラメータが
具体クラスとして指定されていない場合。
引数genercClass
が型パラメータを宣言したクラスでは
なかった場合。protected static <T> List<ParameterizedType> getAncestorTypeList(Class<T> genericClass, Class<? extends T> descendantClass) throws IllegalStateException
ParameterizedType
のリストを取得する。T
- 型パラメータ宣言をしたクラスの型。genericClass
- 型パラメータ宣言をしたクラス。descendantClass
- genericsClass
を継承し、
具体的な型パラメータを指定したクラス。ParameterizedType
のリスト。IllegalStateException
- descendantClass
の
実装で型パラメータが具体クラスとして指定されていない場合。
引数genercClass
が型パラメータを宣言したクラスでは
なかった場合。protected static <T> boolean checkInterfaceAncestors(Class<T> genericClass, List<ParameterizedType> ancestorTypeList, Class clazz)
ParameterizedType
のリストを取得する。T
- 型パラメータを宣言したクラスの型。genericClass
- 型パラメータを宣言したクラス。ancestorTypeList
- ParameterizedType
を
追加するリスト。clazz
- 検査対象のインタフェース型。true
。
見つからなかった場合はfalse
。protected static <T> boolean checkParameterizedType(Type type, Class<T> genericClass, List<ParameterizedType> ancestorTypeList)
Type
型をチェックし、ParameterizedType
かつ、型パラメータを宣言したクラスのサブクラスである場合、リストに追加する。T
- 型パラメータを宣言したクラスの型。type
- 検査対象の型。genericClass
- 型パラメータを宣言したクラス。ancestorTypeList
- ParameterizedType
を
追加するリスト。type
が型パラメータを宣言したクラスと同じクラスの場合。protected static Class resolveTypeVariable(Type type, List<ParameterizedType> ancestorTypeList) throws IllegalStateException
Type
を取得する。type
- 解決する必要のあるType
インスタンス。ancestorTypeList
- type
の具体的な型が
宣言されている可能性のあるParameterizedType
のリスト。IllegalStateException
- 引数type
が
Class
型、および、
TypeVariable
型ではない場合。
引数type
がメソッド、
または、コンストラクタで宣言されている場合。
引数type
の実際の型がClass
ではない
(ワイルドカード、配列)場合。protected static boolean isNotTypeVariable(Type type) throws IllegalStateException
type
がClass
型
であるか、TypeVariable
型かを判定する。type
- Type
インスタンス。type
が
Class, ParameterizedType, GenericArrayType
の場合
true
。
引数type
がTypeVariable
の場合
false
。IllegalStateException
- 引数type
が
Class
、ParameterizedType
、
GenericArrayType
、TypeVariable
の
いずれでもない場合。protected static Class getRawClass(Type type) throws IllegalStateException
type
の実際の型を返却する。type
- Type
インスタンス。Class
インスタンス。IllegalStateException
- 引数type
が
Class
、ParameterizedType
、
GenericArrayType
のいずれでもない場合。Copyright © 2014. All Rights Reserved.