创建声纳规则:
要检查的班级:
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());
}
}
}
但是在使用单元测试或gradle测试测试规则时,expression.symbolType()和identifier.symbol()确实总是返回未知类型/未知符号。 sysout显示:!unknownSymbol!
是否有任何原因不返回该类型(ReadWriteLock,Lock等)?