当我有这样的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
系统有关。
作文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");
}
};