How to obtain the constant defined in the interface to which the method belongs in the method of the interface implementation class;

how to get the constant defined in the interface to which the method belongs in the method of the interface implementation class;

public interface RequireValidator {
    Integer errno = 456;
    Boolean required();
}

public interface TypeValidator {
    Integer errno = 123;
    Boolean isInteger();
}

public class Validator implements RequireValidator, TypeValidator  {
    @Override
    public Boolean require() {
        // 456
        return null;
    }

    @Override
    public String isInteger() {
        // 123;
        return null;
    }
}

as above, how do I get the corresponding value at the comment? ask for answers!

my idea is as follows:

  1. get all the interfaces implemented by the class;
  2. iterate through the list of interfaces to find the interface that defines the method;
  3. then directly access errno ;
  4. through the identified interface.
The question of

is how to determine whether the interface defines the current method?

there is a better way to achieve, please point out, I rookie, ask for help!

Mar.21,2021

you can try your method first, using

String methodName = Thread.currentThread().getStackTrace()[1].getMethodName();

get the name of the current method inside the method.


give me an example.

  

Thank you, God. I'm mainly afraid of changes such as subsequent interface names and method partitions, so that you don't have to modify

in the implementation.
Menu