Sonar Rule: Symbol and SymbolType always unknown

Creating a sonar rule:

Class to check:

import java.util.concurrent.locks.*;

public class SonarTestClass {

public void main() {

ReadWriteLock rwl = new ReentrantReadWriteLock();
Lock writeLock = rwl.writeLock();
writeLock.lock();

StampedLock lock = new StampedLock();
long stampedReadLock = lock.readLock();

ReentrantLock reeLock = new ReentrantLock();
reeLock.lock();


ReadWriteLock rwl1 = new ReentrantReadWriteLock();
Lock writeLock1 = rwl1.writeLock();
writeLock1.lock();
try{
    foo();
} finally {
    writeLock1.unlock();
}

StampedLock stampedLock1 = new StampedLock();
long stampedReadLock1 = stampedLock1.readLock();
try{
    foo();
} finally {
    stampedLock1.unlock(stampedReadLock1);
}

ReentrantLock reeLock1 = new ReentrantLock();
reeLock1.lock();

try{
    foo();
} finally {
    reeLock1.unlock();
}

}

private void foo() {
}

Rule:

@Rule(key = “TSS0008”)
public class LocksReleasedCheck extends IssuableSubscriptionVisitor {

@Override
public List<Tree.Kind> nodesToVisit() {
return ImmutableList.of(Tree.Kind.METHOD_INVOCATION);
}

@Override
public void visitNode(Tree tree) {
MethodInvocationTree mit = (MethodInvocationTree) tree;
if(!mit.methodSelect().is((Tree.Kind.MEMBER_SELECT))) {
return;
}

MemberSelectExpressionTree memberSelectExpressionTree = (MemberSelectExpressionTree) mit.methodSelect();
ExpressionTree expression = memberSelectExpressionTree.expression();
if(expression.is(Tree.Kind.IDENTIFIER)) { //&& expression.symbolType().isSubtypeOf("java.util.concurrent.locks.Lock")){
    IdentifierTree identifier = memberSelectExpressionTree.identifier();
    System.out.println(identifier.symbol());
}
}
}

But expression.symbolType() and identifier.symbol() do always return unkown type/unkown symbol when testing the rule with a unit test or with gradle test. The sysout prints: !unknownSymbol!

Is there any reason it does not return the type (ReadWriteLock, Lock,…) ?

1 Like

Hi Markus, I have exactly the same issue.

I am writing a Sonar Plugin following the documentation I could find.

In my unit tests I get a lot of !unknownSymbol! while getting the variables types (Kind.VARIABLE) in a MethodTree parsing. Only the very classical types like java.lang.String are recognized.
Has anybody a reason for that?

Thanks in advance
Emmanuel

Holy cow! This is probably the same issue I’m encountering too, have anyone figured out a fix?

Hi @remr and @ToanKst

I could solve my problem by getting inspired of: https://github.com/arxes-tolina/sonar-plugins/blob/master/pom.xml
The problem was that Sonar did not had the needed libraries in its classpath at testing time.
In the above mentioned link, take a look at the maven-dependency-plugin section. There you need to include your needed artifacts-dependencies if your problem appears at testing time.

Hope it will help you.
Cheers
Emmanuel

Thanks, I believe that where my issues are too. There are a few missing libraries that prevent the scan from continuing. It’s strange that the plugin is working correctly on SonarQube localhost:9000 though.

18:16:54.614 [main] DEBUG o.s.j.b.loader.SquidClassLoader - .class not found for javax
18:16:54.614 [main] DEBUG o.s.j.b.loader.SquidClassLoader - .class not found for javax.crypto
18:16:54.626 [main] DEBUG o.s.j.b.loader.SquidClassLoader - .class not found for StringBuilder
18:16:54.626 [main] DEBUG o.s.j.b.loader.SquidClassLoader - .class not found for java.lang.package-info
18:16:54.627 [main] DEBUG o.s.j.b.loader.SquidClassLoader - .class not found for java.lang.Synthetic$StringBuilder
18:16:54.627 [main] DEBUG o.s.j.b.loader.SquidClassLoader - .class not found for String
18:16:54.627 [main] DEBUG o.s.j.b.loader.SquidClassLoader - .class not found for java.lang.Synthetic$String
18:16:54.627 [main] DEBUG o.s.j.b.loader.SquidClassLoader - .class not found for String
18:16:54.627 [main] DEBUG o.s.j.b.loader.SquidClassLoader - .class not found for java.lang.Synthetic$String
18:16:54.627 [main] DEBUG o.s.j.b.loader.SquidClassLoader - .class not found for SharedPreferences
18:16:54.627 [main] DEBUG o.s.j.b.loader.SquidClassLoader - .class not found for java.lang.SharedPreferences
18:16:54.627 [main] DEBUG o.s.j.b.loader.SquidClassLoader - .class not found for java.lang.Synthetic$SharedPreferences
18:16:54.627 [main] DEBUG o.s.j.b.loader.SquidClassLoader - .class not found for StringBuilder
18:16:54.628 [main] DEBUG o.s.j.b.loader.SquidClassLoader - .class not found for java.lang.Synthetic$StringBuilder
18:16:54.628 [main] DEBUG o.s.j.b.loader.SquidClassLoader - .class not found for Constants
18:16:54.628 [main] DEBUG o.s.j.b.loader.SquidClassLoader - .class not found for java.lang.Constants
18:16:54.628 [main] DEBUG o.s.j.b.loader.SquidClassLoader - .class not found for java.lang.Synthetic$Constants
18:16:54.629 [main] DEBUG o.s.j.b.loader.SquidClassLoader - .class not found for Context
18:16:54.629 [main] DEBUG o.s.j.b.loader.SquidClassLoader - .class not found for java.lang.Context
18:16:54.629 [main] DEBUG o.s.j.b.loader.SquidClassLoader - .class not found for java.lang.Synthetic$Context