Java : Executable - API使用例
Executable (Java SE 18 & JDK 18) の使用例まとめです。
だいたいのメソッドを網羅済みです。
API仕様のおともにどうぞ。
概要
MethodおよびConstructorに共通する機能のための共有スーパークラス。
Executable は、リフレクションのための Method および Constructor のベースとなるクラスです。
public class Foo {
public Foo(int num) {
}
public void func(String str) {
}
}
final var cls = Foo.class;
final Executable constructor = cls.getDeclaredConstructor(int.class);
System.out.println(constructor); // public Foo(int)
final Executable method = cls.getDeclaredMethod("func", String.class);
System.out.println(method); // public void Foo.func(java.lang.String)
Memberで宣言されたフィールド
DECLARED, PUBLIC
「Java API 使用例 : Member」をご参照ください。
メソッド
AnnotatedType[] getAnnotatedExceptionTypes ()
このExecutableによって表されるメソッドまたはコンストラクタの宣言された例外を指定する型の使用を表すAnnotatedTypeオブジェクトの配列を返します。
public class Foo {
public void func() throws IOException, InterruptedException {
}
}
final var cls = Foo.class;
final Executable executable = cls.getDeclaredMethod("func");
System.out.println(executable);
System.out.println("-- types --");
for (final var type : executable.getAnnotatedExceptionTypes()) {
System.out.println(type);
}
// 結果
// ↓
//public void Foo.func() throws java.io.IOException,java.lang.InterruptedException
//-- types --
//java.io.IOException
//java.lang.InterruptedException
AnnotatedType[] getAnnotatedParameterTypes ()
このExecutableによって表されるメソッドまたはコンストラクタの仮パラメータ型を指定する型の使用を表すAnnotatedTypeオブジェクトの配列を返します。
public class Foo {
public void func(int num, String str) {
}
}
final var cls = Foo.class;
final Executable executable = cls.getDeclaredMethod("func", int.class, String.class);
System.out.println(executable);
System.out.println("-- types --");
for (final var type : executable.getAnnotatedParameterTypes()) {
System.out.println(type);
}
// 結果
// ↓
//public void Foo.func(int,java.lang.String)
//-- types --
//int
//java.lang.String
AnnotatedType getAnnotatedReceiverType ()
このExecutableオブジェクトによって表されるメソッド/コンストラクタのレシーバ型を指定する型の使用を表すAnnotatedTypeオブジェクトを返します。
public class Foo {
public void func(Foo this) {
}
}
final var cls = Foo.class;
final Executable executable = cls.getDeclaredMethod("func");
System.out.println(executable); // public void Foo.func()
final var type = executable.getAnnotatedReceiverType();
System.out.println(type); // Foo
abstract AnnotatedType getAnnotatedReturnType ()
このExecutableによって表されるメソッドまたはコンストラクタの戻り型を指定する型の使用を表すAnnotatedTypeオブジェクトを返します。
public class Foo {
public String func(int num) {
return "abcd";
}
}
final var cls = Foo.class;
final Executable executable = cls.getDeclaredMethod("func", int.class);
System.out.println(executable); // public java.lang.String Foo.func(int)
final var type = executable.getAnnotatedReturnType();
System.out.println(type); // java.lang.String
<T extends Annotation> T getAnnotation (Class<T> annotationClass)
存在する場合は、この要素の指定された型の注釈を返し、そうでない場合はnullを返します。
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.CONSTRUCTOR})
public @interface Foo {
}
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Bar {
}
public class A {
@Foo
public A() {
}
@Bar
public void func() {
}
}
final Executable executable = A.class.getDeclaredConstructor();
final var ret1 = executable.getAnnotation(Foo.class);
System.out.println(ret1); // @Foo()
final var ret2 = executable.getAnnotation(Bar.class);
System.out.println(ret2); // null
final Executable executable = A.class.getDeclaredMethod("func");
final var ret1 = executable.getAnnotation(Foo.class);
System.out.println(ret1); // null
final var ret2 = executable.getAnnotation(Bar.class);
System.out.println(ret2); // @Bar()
<T extends Annotation> T[] getAnnotationsByType (Class<T> annotationClass)
この要素に関連付けられている注釈を返します。
@Repeatable(FooArray.class)
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Foo {
String value();
}
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface FooArray {
Foo[] value();
}
public class A {
@Foo("a1")
@Foo("a2")
public void func() {
}
}
final Executable executable = A.class.getDeclaredMethod("func");
System.out.println("-- annotations --");
for (final var a : executable.getAnnotationsByType(Foo.class)) {
System.out.println(a.annotationType().getSimpleName() + " : " + a.value());
}
// 結果
// ↓
//-- annotations --
//Foo : a1
//Foo : a2
Annotation[] getDeclaredAnnotations ()
この要素に直接存在する注釈を返します。
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Foo {
}
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Bar {
}
public class A {
@Foo
@Bar
public void func() {
}
}
final Executable executable = A.class.getDeclaredMethod("func");
System.out.println("-- annotations --");
for (final var a : executable.getDeclaredAnnotations()) {
System.out.println(a);
}
// 結果
// ↓
//-- annotations --
//@Foo()
//@Bar()
abstract Class<?> getDeclaringClass ()
このオブジェクトによって表される実行可能ファイルを宣言するクラスまたはインタフェースを表すClassオブジェクトを返します。
public class Foo {
public void func() {
}
}
final var cls = Foo.class;
final Executable executable = cls.getDeclaredMethod("func");
System.out.println(executable); // public void Foo.func()
final var ret = executable.getDeclaringClass();
System.out.println(ret); // class Foo
System.out.println(cls.equals(ret)); // true
abstract Class<?>[] getExceptionTypes ()
このオブジェクトで表される基礎となる実行可能ファイルによってスローされるように宣言されている例外の型を表すClassオブジェクトの配列を返します。
public class Foo<T extends Exception> {
public void func() throws T, IOException {
}
}
final var cls = Foo.class;
final Executable executable = cls.getDeclaredMethod("func");
System.out.println(executable);
System.out.println("-- getExceptionTypes --");
for (final var type : executable.getExceptionTypes()) {
System.out.println(type);
}
System.out.println("-- getGenericExceptionTypes --");
for (final var type : executable.getGenericExceptionTypes()) {
System.out.println(type);
}
// 結果
// ↓
//public void Foo.func() throws java.lang.Exception,java.io.IOException
//-- getExceptionTypes --
//class java.lang.Exception
//class java.io.IOException
//-- getGenericExceptionTypes --
//T
//class java.io.IOException
Type[] getGenericExceptionTypes ()
このexecutableオブジェクトによってスローされることが宣言されている例外を表すTypeオブジェクトの配列を返します。
このメソッドの使用例は、getExceptionTypes() にまとめて記載しました。
そちらのAPI使用例をご参照ください。
Type[] getGenericParameterTypes ()
このオブジェクトによって表される実行可能要素の仮パラメータ型を宣言順で表すTypeオブジェクトの配列を返します。
public class Foo<T> {
public void func(int num, T value) {
}
}
final var cls = Foo.class;
final Executable executable = cls.getDeclaredMethod("func", int.class, Object.class);
System.out.println(executable);
System.out.println("-- getParameterTypes --");
for (final var type : executable.getParameterTypes()) {
System.out.println(type);
}
System.out.println("-- getGenericParameterTypes --");
for (final var type : executable.getGenericParameterTypes()) {
System.out.println(type);
}
// 結果
// ↓
//public void Foo.func(int,java.lang.Object)
//-- getParameterTypes --
//int
//class java.lang.Object
//-- getGenericParameterTypes --
//int
//T
abstract int getModifiers ()
このオブジェクトによって表される実行可能要素のJava言語修飾子を返します。
class Foo {
public void a() {
}
protected void b() {
}
private void c() {
}
}
final var cls = Foo.class;
for (final Executable executable : cls.getDeclaredMethods()) {
final var mod = executable.getModifiers();
System.out.println("name = " + executable.getName() + " : mod = " + mod);
System.out.println(" isPublic : " + Modifier.isPublic(mod));
System.out.println(" isProtected : " + Modifier.isProtected(mod));
System.out.println(" isPrivate : " + Modifier.isPrivate(mod));
}
// 結果
// ↓
//name = b : mod = 4
// isPublic : false
// isProtected : true
// isPrivate : false
//name = c : mod = 2
// isPublic : false
// isProtected : false
// isPrivate : true
//name = a : mod = 1
// isPublic : true
// isProtected : false
// isPrivate : false
abstract String getName ()
このオブジェクトによって表される実行可能要素の名前を返します。
public class Foo {
public Foo() {
}
public void func() {
}
}
final var cls = Foo.class;
final Executable constructor = cls.getDeclaredConstructor();
System.out.println(constructor); // public Foo()
System.out.println(constructor.getName()); // Foo
final Executable func = cls.getDeclaredMethod("func");
System.out.println(func); // public void Foo.func()
System.out.println(func.getName()); // func
abstract Annotation[][] getParameterAnnotations ()
このオブジェクトによって表されるExecutableの仮パラメータの注釈を表すAnnotationの配列の配列を、宣言順に返します。
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.PARAMETER)
public @interface Foo {
}
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.PARAMETER)
public @interface Bar {
}
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.PARAMETER)
public @interface Baz {
}
public class A {
public void func(@Foo @Bar int num, @Baz String str) {
}
}
final Executable executable = A.class.getDeclaredMethod("func", int.class, String.class);
System.out.println(executable);
System.out.println("-- annotations --");
for (final var a : executable.getParameterAnnotations()) {
System.out.println(Arrays.toString(a));
}
// 結果
// ↓
//public void A.func(int,java.lang.String)
//-- annotations --
//[@Foo(), @Bar()]
//[@Baz()]
int getParameterCount ()
このオブジェクトによって表される実行可能要素の仮パラメータ(明示的に宣言されているか、暗黙的に宣言されているか、そのいずれでもないかに関係なく)の数を返します。
public class Foo {
public void aaa() {
}
public void bbb(int num) {
}
public void ccc(int num, String str) {
}
}
final var cls = Foo.class;
System.out.println("-- methods --");
for (final Executable executable : cls.getDeclaredMethods()) {
System.out.println(executable.getName() + " : count = " + executable.getParameterCount());
}
// 結果
// ↓
//-- methods --
//aaa : count = 0
//bbb : count = 1
//ccc : count = 2
Parameter[] getParameters ()
このオブジェクトによって表される基礎となる実行可能ファイルへのすべてのパラメータを表すParameterオブジェクトの配列を返します。
public class Foo {
public void func(int num, String str) {
}
}
final Executable executable = Foo.class.getDeclaredMethod("func", int.class, String.class);
System.out.println(executable);
System.out.println("-- parameters --");
for (final var param : executable.getParameters()) {
System.out.println(param);
}
// 結果
// ↓
//public void Foo.func(int,java.lang.String)
//-- parameters --
//int arg0
//java.lang.String arg1
abstract Class<?>[] getParameterTypes ()
このオブジェクトによって表される実行可能ファイルの仮パラメータ型を宣言順で表すClassオブジェクトの配列を返します。
このメソッドの使用例は、getGenericParameterTypes() にまとめて記載しました。
そちらのAPI使用例をご参照ください。
abstract TypeVariable<?>[] getTypeParameters ()
GenericDeclarationオブジェクトによって表されるジェネリック宣言で宣言された型変数を表すTypeVariableオブジェクトの配列を宣言順に返します。
public class Foo {
public <T, U> void func(T t, U u) {
}
}
final Executable executable = Foo.class.getDeclaredMethod("func", Object.class, Object.class);
System.out.println(executable);
System.out.println("-- parameters --");
for (final var param : executable.getTypeParameters()) {
System.out.println(param);
}
// 結果
// ↓
//public void Foo.func(java.lang.Object,java.lang.Object)
//-- parameters --
//T
//U
boolean isSynthetic ()
この実行可能要素が合成構造である場合はtrueを返し、そうでない場合はfalseを返します。
import java.lang.reflect.Executable;
public class IsSyntheticTest {
interface Foo<T> {
void aaa(T value);
}
class Bar implements Foo<String> {
@Override
public void aaa(String value) {
}
}
public static void main(String[] args) {
final var cls = Bar.class;
for (final Executable executable : cls.getDeclaredMethods()) {
System.out.println(executable);
System.out.println(executable.isSynthetic());
}
// 結果
// ↓
//public void IsSyntheticTest$Bar.aaa(java.lang.String)
//false
//public void IsSyntheticTest$Bar.aaa(java.lang.Object)
//true
}
}
boolean isVarArgs ()
この実行可能要素が可変数の引数を取るように宣言されていた場合はtrueを返し、そうでない場合はfalseを返します。
public class Foo {
public void aaa(int num) {
}
public void bbb(int num, String... args) {
}
}
final Executable executable = Foo.class.getDeclaredMethod("aaa", int.class);
System.out.println(executable); // public void Foo.aaa(int)
System.out.println(executable.isVarArgs()); // false
final Executable executable = Foo.class.getDeclaredMethod("bbb", int.class, String[].class);
System.out.println(executable); // public void Foo.bbb(int,java.lang.String[])
System.out.println(executable.isVarArgs()); // true
abstract String toGenericString ()
型パラメータを含む、このExecutableを記述する文字列を返します。
public class Foo<T> {
public Foo(int num) {
}
public void func(T value) {
}
}
final Executable executable = Foo.class.getDeclaredConstructor(int.class);
final var ret1 = executable.toGenericString();
System.out.println(ret1); // public Foo(int)
final var ret2 = executable.toString();
System.out.println(ret2); // public Foo(int)
final Executable executable = Foo.class.getDeclaredMethod("func", Object.class);
final var ret1 = executable.toGenericString();
System.out.println(ret1); // public void Foo.func(T)
final var ret2 = executable.toString();
System.out.println(ret2); // public void Foo.func(java.lang.Object)
AccessibleObjectで宣言されたメソッド
canAccess, getAnnotations, getDeclaredAnnotation, getDeclaredAnnotations, getDeclaredAnnotationsByType, isAccessible, isAnnotationPresent, setAccessible, setAccessible, trySetAccessible
「Java API 使用例 : AccessibleObject」をご参照ください。
AnnotatedElementで宣言されたメソッド
getAnnotations, getDeclaredAnnotation, getDeclaredAnnotationsByType, isAnnotationPresent
「Java API 使用例 : AnnotatedElement」をご参照ください。