I’m developing a new custom rule and want to test it with org.sonar.java.checks.verifier.CheckVerifier. This parses this test file:
class Person implements java.io.Serializable {
private String name;
@Overwrite
public String toString() {
org.apache.commons.lang3.builder.ToStringBuilder toStringBuilder =
new org.apache.commons.lang3.builder.ToStringBuilder()
.append("name", name);
String result = toStringBuilder.build();
return result;
}
}
When I execute the test, I get the following error message for the toStringBuilder variable:
WARN Unresolved imports/types have been detected during analysis. Enable DEBUG mode to see them.
The custom rule is invoked, anyhow. But when I determine the fully quailified name of the toStringBuilder VariableTree, it shows me Recovered#typeBindingLorg/apache;0
I couldn’t find out how I could package paths in that test file, nor how I could activate the debug mode for the CheckVerifier. Can anybody give me a hint?
Hi Quentin, unfortunately this didn’t help. What is even weirder: if I use this as an instance variable of the class, the ToStringBuilder is resolved correctly, see the 3rd line:
class Person implements java.io.Serializable {
private String name;
private org.apache.commons.lang3.builder.ToStringBuilder toStringBuilder;
@Overwrite
public String toString() {
org.apache.commons.lang3.builder.ToStringBuilder toStringBuilderInMethod =
new org.apache.commons.lang3.builder.ToStringBuilder()
.append("name", name);
String result = toStringBuilderInMethod.build();
return result;
}
The variable “toStringBuilderInMethod” still has the class name
I had a look again, I’m unsure what is happening exactly, but here are a few hints:
I noticed that the code you provided does not compile (at least with commons-lang3, version 3.12.0). Even if you will be able to parse the code fine, you should make sure the tests files you are providing are compiling. If I add an argument to new org.apache.commons.lang3.builder.ToStringBuilder() → ToStringBuilder(new Object()), the warning goes away.
As a general observation, as long as the warning that you already noticed is present:
WARN Unresolved imports/types have been detected during analysis. Enable DEBUG mode to see them.