修复:单元测试时部分规则ID找不到的问题

wuhaoyang
Guo XIn 10 months ago
parent c47ab1eacb
commit 219eb4ab20
  1. 52
      sonar-keyware-plugins-java/src/main/java/com/keyware/sonar/java/rules/checkers/RedirectUrlChecker.java
  2. 2
      sonar-keyware-plugins-java/src/main/java/com/keyware/sonar/java/rules/checkers/UploadFileVerifyChecker.java

@ -38,7 +38,7 @@ public class RedirectUrlChecker extends IssuableSubscriptionVisitor {
// 判断方法的返回节点的类型为RedirectView 或 String 类型
if ("RedirectView".equals(methodTree.returnType().toString())) {
// 传递上下文,和方法的参数列表
new RedirectViewCheckVisitor(this, parameters).check(block);
block.accept(new RedirectViewCheckVisitor(this, parameters));
} else if ("String".equals(methodTree.returnType().toString())) {
checkByStringType(block, parameters);
}
@ -110,7 +110,7 @@ public class RedirectUrlChecker extends IssuableSubscriptionVisitor {
}
static class RedirectViewCheckVisitor extends IssuableSubscriptionVisitor {
static class RedirectViewCheckVisitor extends BaseTreeVisitor {
private final RedirectUrlChecker checker;
// 方法的参数列表
private final List<? extends VariableTree> methodParameters;
@ -120,40 +120,28 @@ public class RedirectUrlChecker extends IssuableSubscriptionVisitor {
this.methodParameters = parameters;
}
public void check(Tree block) {
this.scanTree(block);
}
@Override
public List<Tree.Kind> nodesToVisit() {
// 订阅new class和 函数调用的节点
var nodeType = new Tree.Kind[]{Tree.Kind.NEW_CLASS, Tree.Kind.METHOD_INVOCATION};
return Arrays.asList(nodeType);
public void visitNewClass(NewClassTree classTree) {
// 判断是否为RedirectView,如果是,则判断是否有参数,如果有参数,则判断参数的类型是否由方法传递进来的
String name = classTree.identifier().toString();
if ("RedirectView".equals(name)) {
if (classTree.arguments().size() > 0) {
// 获取第一个参数语法树节点
ExpressionTree argNode = classTree.arguments().get(0);
checkArgs(argNode, classTree);
}
}
}
@Override
public void visitNode(Tree tree) {
if (tree.is(Tree.Kind.NEW_CLASS)) {
NewClassTree classTree = (NewClassTree) tree;
// 判断是否为RedirectView,如果是,则判断是否有参数,如果有参数,则判断参数的类型是否由方法传递进来的
String name = classTree.identifier().toString();
if ("RedirectView".equals(name)) {
if (classTree.arguments().size() > 0) {
// 获取第一个参数语法树节点
ExpressionTree argNode = classTree.arguments().get(0);
checkArgs(argNode, tree);
}
}
} else {
MethodInvocationTree invocationTree = (MethodInvocationTree) tree;
ExpressionTree expressionTree = invocationTree.methodSelect();
if (expressionTree instanceof MemberSelectExpressionTree) {
MemberSelectExpressionTree member = (MemberSelectExpressionTree) expressionTree;
if (member.expression().symbolType().is("RedirectView")
&& "setUrl".equals(member.identifier().name())) {
ExpressionTree argNode = invocationTree.arguments().get(0);
checkArgs(argNode, tree);
}
public void visitMethodInvocation(MethodInvocationTree invocationTree) {
ExpressionTree expressionTree = invocationTree.methodSelect();
if (expressionTree instanceof MemberSelectExpressionTree) {
MemberSelectExpressionTree member = (MemberSelectExpressionTree) expressionTree;
if (member.expression().symbolType().is("RedirectView")
&& "setUrl".equals(member.identifier().name())) {
ExpressionTree argNode = invocationTree.arguments().get(0);
checkArgs(argNode, invocationTree);
}
}
}

@ -20,7 +20,7 @@ import java.util.List;
* @date 2024/1/8
*/
@Rule(key = "UploadFileVerify")
@Rule(key = "UploadFileVerifyChecker")
public class UploadFileVerifyChecker extends IssuableSubscriptionVisitor {
private final String value = "程序设计时,应以“白名单”方式限制允许用户上传的文件的类型";

Loading…
Cancel
Save