SonarLint 未拾取在匿名类的“实例初始化块”内抛出的异常

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

当我有这样的Java代码时:

@Test
void testExplainJavaS1130_WithAnonymousInstanceInitilizer() throws Exception {
    var something = new IllegalStateException() {
        private static final long serialVersionUID = 1L;

        {
            // This is what is throwing but Java:S1130 does not see it
            throwingMethod();
        }

        private void throwingMethod() throws Exception {
            throw new Exception("On purpose for testing");
        }
    };
    assertNotNull(something);
}

我从 SonarLint 收到此消息:

Remove the declaration of thrown exception 'java.lang.Exception', as it cannot be thrown from method's body.

现在我可以“停用规则 java:S1130”,但它通常会很好地帮助我。所以我希望 SonarLint 系统能够拾取“实例初始化块”内部抛出的实际异常。我如何刺激 SonarLint 做到这一点?

我可以接受在测试范围内改变这条规则,任何帮助也将受到欢迎。

我正在使用“SonarLint for Eclipse:10.9.1.82333”。

PS 这确实与我仍然喜欢使用的旧

jmock
系统有关。

java junit eclipse-plugin sonarlint jmock
1个回答
0
投票

作文CE代码

var some = new IllegalStateException() { 私有静态最终长serialVersionUID = 1L;

{
    try {
        throwingMethod();
    } catch (Exception e) {
        throw new RuntimeException("Exception during instance initialization", e);
    }
}

private void throwingMethod() throws Exception {
    throw new Exception("On purpose for testing");
}

};

© www.soinside.com 2019 - 2024. All rights reserved.