Java : Executable with Examples

Executable (Java SE 18 & JDK 18) API Examples.
You will find code examples on most Executable methods.


Summary

A shared superclass for the common functionality of Method and Constructor.

Class diagram

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

DECLARED, PUBLIC

Please see the link below.

Methods

AnnotatedType[] getAnnotatedExceptionTypes ()

Returns an array of AnnotatedType objects that represent the use of types to specify the declared exceptions of the method/constructor represented by this Executable.

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 ()

Returns an array of AnnotatedType objects that represent the use of types to specify formal parameter types of the method/constructor represented by this Executable.

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 ()

Returns an AnnotatedType object that represents the use of a type to specify the receiver type of the method/constructor represented by this Executable object.

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 ()

Returns an AnnotatedType object that represents the use of a type to specify the return type of the method/constructor represented by this Executable.

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)

Returns this element's annotation for the specified type if such an annotation is present, else 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)

Returns annotations that are associated with this element.

@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 ()

Returns annotations that are directly present on this element.

@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 ()

Returns the Class object representing the class or interface that declares the executable represented by this object.

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 ()

Returns an array of Class objects that represent the types of exceptions declared to be thrown by the underlying executable represented by this object.

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 ()

Returns an array of Type objects that represent the exceptions declared to be thrown by this executable object.

Please see getExceptionTypes().

Type[] getGenericParameterTypes ()

Returns an array of Type objects that represent the formal parameter types, in declaration order, of the executable represented by this object.

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 ()

Returns the Java language modifiers for the executable represented by this object.

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 ()

Returns the name of the executable represented by this object.

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 ()

Returns an array of arrays of Annotations that represent the annotations on the formal parameters, in declaration order, of the Executable represented by this object.

@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 ()

Returns the number of formal parameters (whether explicitly declared or implicitly declared or neither) for the executable represented by this object.

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 ()

Returns an array of Parameter objects representing all the parameters to the underlying executable represented by this object.

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 ()

Returns an array of Class objects that represent the formal parameter types, in declaration order, of the executable represented by this object.

Please see getGenericParameterTypes().

abstract TypeVariable<?>[] getTypeParameters ()

Returns an array of TypeVariable objects that represent the type variables declared by the generic declaration represented by this GenericDeclaration object, in declaration order.

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 ()

Returns true if this executable is a synthetic construct; returns false otherwise.

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 ()

Returns true if this executable was declared to take a variable number of arguments; returns false otherwise.

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 ()

Returns a string describing this Executable, including any type parameters.

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.


Related posts

To top of page