sonar扫描kotlin 代码报错 Cannot analyse 'XXXXServiceImpl.kt' with 'KtChecksVisitor': null

  • 问题现象:
// sonar 扫描控制台报错,有可能会导致代码覆盖率等指标不准确
16
:56:47.623 ERROR: Cannot analyse 'XXXXServiceImpl.kt' with 'KtChecksVisitor': null java.lang.NullPointerException: null at org.sonarsource.kotlin.checks.ConditionalStructure.<init>(AbstractBranchDuplication.kt:62) at org.sonarsource.kotlin.checks.AbstractBranchDuplication.visitIfExpression(AbstractBranchDuplication.kt:40) at org.sonarsource.kotlin.checks.AbstractBranchDuplication.visitIfExpression(AbstractBranchDuplication.kt:29) at org.jetbrains.kotlin.psi.KtIfExpression.accept(KtIfExpression.java:33) at org.sonarsource.kotlin.visiting.KtChecksVisitor$visit$1$1$1.invoke(KtChecksVisitor.kt:39) at org.sonarsource.kotlin.visiting.KtChecksVisitor$visit$1$1$1.invoke(KtChecksVisitor.kt:34) at org.sonarsource.kotlin.plugin.MetricsUtilsKt.measureDuration(MetricsUtils.kt:26) at org.sonarsource.kotlin.visiting.KtChecksVisitor.visit(KtChecksVisitor.kt:34) at org.sonarsource.kotlin.visiting.KotlinFileVisitor.scan(KotlinFileVisitor.kt:28) at org.sonarsource.kotlin.plugin.KotlinSensor$visitFile$1.invoke(KotlinSensor.kt:230) at org.sonarsource.kotlin.plugin.KotlinSensor$visitFile$1.invoke(KotlinSensor.kt:229) at org.sonarsource.kotlin.plugin.MetricsUtilsKt.measureDuration(MetricsUtils.kt:26) at org.sonarsource.kotlin.plugin.KotlinSensor.visitFile(KotlinSensor.kt:229) at org.sonarsource.kotlin.plugin.KotlinSensor.analyseFile(KotlinSensor.kt:217) at org.sonarsource.kotlin.plugin.KotlinSensor.access$analyseFile(KotlinSensor.kt:64) at org.sonarsource.kotlin.plugin.KotlinSensor$analyseFiles$1.invoke(KotlinSensor.kt:192) at org.sonarsource.kotlin.plugin.KotlinSensor$analyseFiles$1.invoke(KotlinSensor.kt:191) at org.sonarsource.kotlin.plugin.MetricsUtilsKt.measureDuration(MetricsUtils.kt:26) at org.sonarsource.kotlin.plugin.KotlinSensor.analyseFiles(KotlinSensor.kt:191) at org.sonarsource.kotlin.plugin.KotlinSensor.execute(KotlinSensor.kt:119) at org.sonar.scanner.sensor.AbstractSensorWrapper.analyse(AbstractSensorWrapper.java:64) at org.sonar.scanner.sensor.ModuleSensorsExecutor.execute(ModuleSensorsExecutor.java:88) at org.sonar.scanner.sensor.ModuleSensorsExecutor.lambda$execute$1(ModuleSensorsExecutor.java:61) at org.sonar.scanner.sensor.ModuleSensorsExecutor.withModuleStrategy(ModuleSensorsExecutor.java:79) at org.sonar.scanner.sensor.ModuleSensorsExecutor.execute(ModuleSensorsExecutor.java:61) at org.sonar.scanner.scan.SpringModuleScanContainer.doAfterStart(SpringModuleScanContainer.java:82) at org.sonar.core.platform.SpringComponentContainer.startComponents(SpringComponentContainer.java:188) at org.sonar.core.platform.SpringComponentContainer.execute(SpringComponentContainer.java:167) at org.sonar.scanner.scan.SpringProjectScanContainer.scan(SpringProjectScanContainer.java:403) at org.sonar.scanner.scan.SpringProjectScanContainer.scanRecursively(SpringProjectScanContainer.java:399) at org.sonar.scanner.scan.SpringProjectScanContainer.doAfterStart(SpringProjectScanContainer.java:368) at org.sonar.core.platform.SpringComponentContainer.startComponents(SpringComponentContainer.java:188) at org.sonar.core.platform.SpringComponentContainer.execute(SpringComponentContainer.java:167) at org.sonar.scanner.bootstrap.SpringGlobalContainer.doAfterStart(SpringGlobalContainer.java:137) at org.sonar.core.platform.SpringComponentContainer.startComponents(SpringComponentContainer.java:188) at org.sonar.core.platform.SpringComponentContainer.execute(SpringComponentContainer.java:167) at org.sonar.batch.bootstrapper.Batch.doExecute(Batch.java:72) at org.sonar.batch.bootstrapper.Batch.execute(Batch.java:66) at org.sonarsource.scanner.api.internal.batch.BatchIsolatedLauncher.execute(BatchIsolatedLauncher.java:46) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:566) at org.sonarsource.scanner.api.internal.IsolatedLauncherProxy.invoke(IsolatedLauncherProxy.java:60) at com.sun.proxy.$Proxy0.execute(Unknown Source) at org.sonarsource.scanner.api.EmbeddedScanner.doExecute(EmbeddedScanner.java:189) at org.sonarsource.scanner.api.EmbeddedScanner.execute(EmbeddedScanner.java:138) at org.sonarsource.scanner.cli.Main.execute(Main.java:112) at org.sonarsource.scanner.cli.Main.execute(Main.java:75) at org.sonarsource.scanner.cli.Main.main(Main.java:61)
  • 问题原因分析:
 // 问题代码片段
override fun xxxxTravel(xxx: XXXDTO) { if (xxx.type == "1" || xxx.type == "2" || xxx.type == "3" || xxx.type == "4" ) else { return } }
// sonar扫描 识别不了 if(xx)else{} 语法
// sonar kotlin源码
//
解析条件分支,ifTree,这个ifTree是null

 

  • 问题修复
override fun xxxxTravel(xxx: XXXDTO) {
        val types = listOf("1", "2", "3", "4")
        if (!types.contains(xxx.type)) {
            return
        }
    }

 

posted @ 2023-04-07 15:35  start逍遥  阅读(98)  评论(0编辑  收藏  举报