vendredi 13 octobre 2023

Checking if a method is declared in an interface

I want to check if a method of a class is declared in an interface.

AInterface.java:

public interface AInterface {
    Object hi();
}

AClass.java:

public class AClass implements AInterface {
    public String hi() {
        return "hello!";
    }
}

Main.java:

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;

public class Main {

    public static void main(String[] args) {
        for (Method md : AClass.class.getDeclaredMethods()) {
            System.out.println("Declaring class: " + md.getDeclaringClass().getName());
            System.out.println("Return type: " + md.getReturnType().getName());
            System.out.println("Name: " + md.getName());
            System.out.println();
        }
    }

}

md.getDeclaringClass() returns the same class for both methods. So how do I check which one is in the class?





Aucun commentaire:

Enregistrer un commentaire