我有一个通用接口和一个实现它的类:
import java.util.Arrays;
interface Interface<T> {
void doSomething(T element);
}
class StringImpl implements Interface<String> {
@Override
public void doSomething(String element) {
System.out.println("StringImpl: doSomething");
}
}
public class Main {
public static void main(String... args) {
System.out.println(Arrays.toString(StringImpl.class.getDeclaredMethods()));
}
}
结果是
[public void com.ra.StringImpl.doSomething(java.lang.String),
public void com.ra.StringImpl.doSomething(java.lang.Object)]
但事实上,我只想要实施版本:
public void com.ra.StringImpl.doSomething(java.lang.String)
你有任何方便的方法来实现它吗?
答案 0 :(得分:27)
过滤掉bridge methods:
Method[] methods = Arrays.stream(StringImpl.class.getDeclaredMethods())
.filter(m -> !m.isBridge())
.toArray(Method[]::new);