Java checkstyle:如果方法名称包含Y,则检查返回类型是否为X

问题描述 投票:1回答:1

我正在尝试编写一个检查样式的规则,如果名称通过某个正则表达式,则该方法将验证方法的返回类型。

例如:如果方法名称包含Foo,我想确保返回类型不是Bar

通行证:

public Fizz testFoo() {}

public Optional<Fizz> testFoo2() {}

失败:

public Bar testFoo() {}

public Optional<Bar> fooTest() {}
java checkstyle
1个回答
0
投票

我不了解有关问题的信息,但可能是此代码的共同之处

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!");  
                }
            }
        }
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.