Java : Executable with Examples
Executable (Java SE 18 & JDK 18) API Examples.
You will find code examples on most Executable methods.
Summary
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)
Fields declared in Member
Methods
AnnotatedType[] getAnnotatedExceptionTypes ()
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);
}
// Result
// ↓
//public void Foo.func() throws java.io.IOException,java.lang.InterruptedException
//-- types --
//java.io.IOException
//java.lang.InterruptedException
AnnotatedType[] getAnnotatedParameterTypes ()
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);
}
// Result
// ↓
//public void Foo.func(int,java.lang.String)
//-- types --
//int
//java.lang.String
AnnotatedType getAnnotatedReceiverType ()
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 ()
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)
@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());
}
// Result
// ↓
//-- 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);
}
// Result
// ↓
//-- annotations --
//@Foo()
//@Bar()
abstract Class<?> getDeclaringClass ()
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 ()
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);
}
// Result
// ↓
//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 ()
Please see getExceptionTypes().
Type[] getGenericParameterTypes ()
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);
}
// Result
// ↓
//public void Foo.func(int,java.lang.Object)
//-- getParameterTypes --
//int
//class java.lang.Object
//-- getGenericParameterTypes --
//int
//T
abstract int getModifiers ()
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));
}
// Result
// ↓
//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 ()
@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));
}
// Result
// ↓
//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());
}
// Result
// ↓
//-- methods --
//aaa : count = 0
//bbb : count = 1
//ccc : count = 2
Parameter[] getParameters ()
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);
}
// Result
// ↓
//public void Foo.func(int,java.lang.String)
//-- parameters --
//int arg0
//java.lang.String arg1
abstract Class<?>[] getParameterTypes ()
Please see getGenericParameterTypes().
abstract TypeVariable<?>[] getTypeParameters ()
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);
}
// Result
// ↓
//public void Foo.func(java.lang.Object,java.lang.Object)
//-- parameters --
//T
//U
boolean isSynthetic ()
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());
}
// Result
// ↓
//public void IsSyntheticTest$Bar.aaa(java.lang.String)
//false
//public void IsSyntheticTest$Bar.aaa(java.lang.Object)
//true
}
}
boolean isVarArgs ()
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 ()
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)
Methods declared in AccessibleObject
canAccess, getAnnotations, getDeclaredAnnotation, getDeclaredAnnotations, getDeclaredAnnotationsByType, isAccessible, isAnnotationPresent, setAccessible, setAccessible, trySetAccessible
Please see the link below.
Methods declared in AnnotatedElement
getAnnotations, getDeclaredAnnotation, getDeclaredAnnotationsByType, isAnnotationPresent
Please see the link below.