Sonar Rule: Symbol and SymbolType always unknown


(Markus) #1

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,…) ?