检测是否在Java中的接口中声明了方法

rip*_*234 7 java reflection methods

帮助我使这个方法更加坚实:

 /**
  * Check if the method is declared in the interface.
  * Assumes the method was obtained from a concrete class that 
  * implements the interface, and return true if the method overrides
  * a method from the interface.
  */
 public static boolean isDeclaredInInterface(Method method, Class<?> interfaceClass) {
     for (Method methodInInterface : interfaceClass.getMethods())
     {
         if (methodInInterface.getName().equals(method.getName()))
             return true;
     }
     return false;
 }
Run Code Online (Sandbox Code Playgroud)

Yis*_*hai 10

这个怎么样:

try {
    interfaceClass.getMethod(method.getName(), method.getParameterTypes());
    return true;
} catch (NoSuchMethodException e) {
    return false;
}
Run Code Online (Sandbox Code Playgroud)

  • @Anon,作为规则不,但如果这是API给你的,那么你就是.如何确定String是否可以解析为整数? (4认同)
  • 您不想将异常用作正常控制逻辑的一部分。 (2认同)