我正在尝试编写一个检查样式的规则,如果名称通过某个正则表达式,则该方法将验证方法的返回类型。
例如:如果方法名称包含Foo
,我想确保返回类型不是Bar
。
通行证:
public Fizz testFoo() {}
public Optional<Fizz> testFoo2() {}
失败:
public Bar testFoo() {}
public Optional<Bar> fooTest() {}
我不了解有关问题的信息,但可能是此代码的共同之处
Method[] methods = this.getClass().getMethods();
for (Method method : methods) {
if(method.getName().contains("Foo")){
if(!method.getReturnType().getClass().getName().contains("Foo")){
throw new Exception("Wrong return type!");
}
}
}