2
次の行で汎用引数にアクセスすることは可能でしょうか?戻り型の汎用引数にアクセスする
public List<StoryLikeRef> getLikes() throws IOException
戻り値の型からStoryLikeRefを取得することを意味しますか?
おかげ
次の行で汎用引数にアクセスすることは可能でしょうか?戻り型の汎用引数にアクセスする
public List<StoryLikeRef> getLikes() throws IOException
戻り値の型からStoryLikeRefを取得することを意味しますか?
おかげ
はい、あなたは、StoryLikeRef
は、具体的なタイプ(タイプではなく、パラメータそのもの)であると仮定することができます。 Method.getGenericReturnType
を使用してType
を取得してください。サンプルコード:
import java.lang.reflect.*;
import java.util.*;
public class Test {
public List<String> getStringList() {
return null;
}
public List<Integer> getIntegerList() {
return null;
}
public static void main(String[] args) throws Exception {
showTypeParameters("getStringList");
showTypeParameters("getIntegerList");
}
// Only using throws Exception for sample code. Don't do
// this in real life.
private static void showTypeParameters(String methodName)
throws Exception {
Method method = Test.class.getMethod(methodName);
Type returnType = method.getGenericReturnType();
System.out.println("Overall return type: " + returnType);
if (returnType instanceof ParameterizedType) {
ParameterizedType type = (ParameterizedType) returnType;
for (Type t: type.getActualTypeArguments()) {
System.out.println(" Type parameter: " + t);
}
} else {
System.out.println("Not a generic type");
}
}
}
うわー、私はそれを知らなかった –