Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SonarQube - "The class '...' could not be matched to its original source file."

Within my SonarQube Analysis I can find the following error:

WARN: The class 'com.mycompany.bla.BlaTest' could not be matched to its original source file. It might be a dynamically generated class.
WARN: Failed to load the class file metadata
org.sonar.plugins.findbugs.resource.ClassMetadataLoadingException: ASM failed to load classfile metadata
    at org.sonar.plugins.findbugs.resource.DebugExtensionExtractor.getDebugExtFromClass(DebugExtensionExtractor.java:49)
    at org.sonar.plugins.findbugs.resource.ByteCodeResourceLocator.extractSmapLocation(ByteCodeResourceLocator.java:166)
    at org.sonar.plugins.findbugs.FindbugsSensor.execute(FindbugsSensor.java:171)
    at org.sonar.scanner.sensor.SensorWrapper.analyse(SensorWrapper.java:53)
    at org.sonar.scanner.phases.SensorsExecutor.executeSensor(SensorsExecutor.java:88)
    at org.sonar.scanner.phases.SensorsExecutor.execute(SensorsExecutor.java:82)
    at org.sonar.scanner.phases.SensorsExecutor.execute(SensorsExecutor.java:68)
    at org.sonar.scanner.phases.AbstractPhaseExecutor.execute(AbstractPhaseExecutor.java:88)
    at org.sonar.scanner.scan.ModuleScanContainer.doAfterStart(ModuleScanContainer.java:180)
    at org.sonar.core.platform.ComponentContainer.startComponents(ComponentContainer.java:135)
    at org.sonar.core.platform.ComponentContainer.execute(ComponentContainer.java:121)
    at org.sonar.scanner.scan.ProjectScanContainer.scan(ProjectScanContainer.java:302)
    at org.sonar.scanner.scan.ProjectScanContainer.scanRecursively(ProjectScanContainer.java:297)
    at org.sonar.scanner.scan.ProjectScanContainer.scanRecursively(ProjectScanContainer.java:295)
    at org.sonar.scanner.scan.ProjectScanContainer.doAfterStart(ProjectScanContainer.java:271)
    at org.sonar.core.platform.ComponentContainer.startComponents(ComponentContainer.java:135)
    at org.sonar.core.platform.ComponentContainer.execute(ComponentContainer.java:121)
    at org.sonar.scanner.task.ScanTask.execute(ScanTask.java:48)
    at org.sonar.scanner.task.TaskContainer.doAfterStart(TaskContainer.java:84)
    at org.sonar.core.platform.ComponentContainer.startComponents(ComponentContainer.java:135)
    at org.sonar.core.platform.ComponentContainer.execute(ComponentContainer.java:121)
    at org.sonar.scanner.bootstrap.GlobalContainer.executeTask(GlobalContainer.java:121)
    at org.sonar.batch.bootstrapper.Batch.doExecuteTask(Batch.java:116)
    at org.sonar.batch.bootstrapper.Batch.execute(Batch.java:71)
    at org.sonarsource.scanner.api.internal.batch.BatchIsolatedLauncher.execute(BatchIsolatedLauncher.java:46)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    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:171)
    at org.sonarsource.scanner.api.EmbeddedScanner.execute(EmbeddedScanner.java:128)
    at org.sonarsource.scanner.cli.Main.execute(Main.java:111)
    at org.sonarsource.scanner.cli.Main.execute(Main.java:75)
    at org.sonarsource.scanner.cli.Main.main(Main.java:61)
Caused by: java.lang.IllegalArgumentException: null
    at org.objectweb.asm.ClassReader.<init>(ClassReader.java:160)
    at org.objectweb.asm.ClassReader.<init>(ClassReader.java:143)
    at org.objectweb.asm.ClassReader.<init>(ClassReader.java:418)
    at org.sonar.plugins.findbugs.resource.DebugExtensionExtractor.getDebugExtFromClass(DebugExtensionExtractor.java:43)
... 35 common frames omitted

I found out that the error occurs on a Unit-Test File where the source is located under "testsrc/com/mycompany/...". The compiled testclass is located under "classes"

So the Project structure is:

myProject
    |- src
    |- testsrc
    |- classes

Within my sonar-project.properties I have the following configuration:

sonar.tests=testsrc
sonar.sources=src

What would be the correct solution here? Exclude Test-Classes from being analyzed by FindBugs or add the testsrc folder to the analyzed sources?

like image 651
Christoph Forster Avatar asked Nov 07 '22 02:11

Christoph Forster


1 Answers

The Trick was to add a findbugsexclude.xml to sonarqube:

sonar.findbugs.excludesFilters=findbugsexclude.xml

and to Exclude all Test classes and Inner Classes within Test-Classes:

<?xml version="1.0" encoding="UTF-8"?>
<FindBugsFilter>
    <Match>
        <Class name="~.*Test$"/>
    </Match>
    <Match>
        <Class name="~.*Test\$.*$"/>
    </Match>
</FindBugsFilter>
like image 84
Christoph Forster Avatar answered Dec 03 '22 05:12

Christoph Forster