Создание правила сонара:
Класс для проверки:
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(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());
}
}
}
Но expression.symbolType () и identifier.symbol ()всегда возвращайте неизвестный тип / неизвестный символ при тестировании правила с помощью модульного теста или теста Gradle.Sysout печатает:! UnknownSymbol!
Есть ли причина, по которой он не возвращает тип (ReadWriteLock, Lock, ..)?