From b1987b0b02930d211c7be51e8fe27cfc99858c81 Mon Sep 17 00:00:00 2001 From: wuhaoyang <2507865306@qq.com> Date: Wed, 10 Jan 2024 18:16:01 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=87=86=E5=88=99=EF=BC=9A?= =?UTF-8?q?=E5=9C=A8=E6=9E=84=E5=BB=BA=E8=B7=AF=E5=BE=84=E5=90=8D=E5=89=8D?= =?UTF-8?q?=E5=AF=B9=E8=BE=93=E5=85=A5=E6=95=B0=E6=8D=AE=E8=BF=9B=E8=A1=8C?= =?UTF-8?q?=E9=AA=8C=E8=AF=81=EF=BC=8C=E7=A1=AE=E4=BF=9D=E5=A4=96=E9=83=A8?= =?UTF-8?q?=E8=BE=93=E5=85=A5=E4=BB=85=E5=8C=85=E5=90=AB=E5=85=81=E8=AE=B8?= =?UTF-8?q?=E6=9E=84=E6=88=90=E8=B7=AF=E5=BE=84=E5=90=8D=E7=9A=84=E5=AD=97?= =?UTF-8?q?=E7=AC=A6=E6=88=96=E9=99=90=E5=88=B6=E5=85=81=E8=AE=B8=E8=AE=BF?= =?UTF-8?q?=E9=97=AE=E7=9A=84=E7=9B=AE=E5=BD=95.=E5=8F=96=E6=B6=88?= =?UTF-8?q?=E4=BA=86=E6=A3=80=E6=B5=8B=E5=8F=82=E6=95=B0=E7=B1=BB=E5=9E=8B?= =?UTF-8?q?=E9=99=90=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../rules/checkers/PathAndKeywordCheck.java | 29 +++++++++---------- .../src/test/files/PathAndKeywordCheck.java | 6 ++-- 2 files changed, 17 insertions(+), 18 deletions(-) diff --git a/sonar-keyware-plugins-java/src/main/java/com/keyware/sonar/java/rules/checkers/PathAndKeywordCheck.java b/sonar-keyware-plugins-java/src/main/java/com/keyware/sonar/java/rules/checkers/PathAndKeywordCheck.java index 31038a1..61ab5a3 100644 --- a/sonar-keyware-plugins-java/src/main/java/com/keyware/sonar/java/rules/checkers/PathAndKeywordCheck.java +++ b/sonar-keyware-plugins-java/src/main/java/com/keyware/sonar/java/rules/checkers/PathAndKeywordCheck.java @@ -50,23 +50,22 @@ public class PathAndKeywordCheck extends IssuableSubscriptionVisitor { MethodTree methodTree = (MethodTree) parent; methodTree.parameters().forEach(parameter -> { if (parameter.type() != null) { - String parameterType = parameter.type().symbolType().name(); - if (parameterType.equals("String")) { // 这里假设你关心的参数类型是 String - String className = newClassTree.symbolType().name(); - if (TARGET_CLASS_NAMES.contains(className)) { - // 获取构造方法的参数 - List arguments = newClassTree.arguments(); - if (!arguments.isEmpty()) { - ExpressionTree firstArgument = arguments.get(0); - String constructorArgument = getArgumentValue(firstArgument); + String parameterType = parameter.type().symbolType().fullyQualifiedName(); + System.out.println(parameterType); + String className = newClassTree.symbolType().name(); + if (TARGET_CLASS_NAMES.contains(className)) { + // 获取构造方法的参数 + List arguments = newClassTree.arguments(); + if (!arguments.isEmpty()) { + ExpressionTree firstArgument = arguments.get(0); + String constructorArgument = getArgumentValue(firstArgument); - // 获取方法的入参名称 - String parameterName = parameter.simpleName().name(); + // 获取方法的入参名称 + String parameterName = parameter.simpleName().name(); - if (constructorArgument != null && constructorArgument.equals(parameterName)) { - System.out.println("避免在参数中使用 " + className + " 对象的构造方法的参数:" + parameterName); - reportIssue(newClassTree, "避免在参数中使用禁止的关键字"); - } + if (constructorArgument != null && constructorArgument.equals(parameterName)) { + System.out.println("避免在参数中使用 " + className + " 对象的构造方法的参数:" + parameterName); + reportIssue(newClassTree, "避免在参数中使用禁止的关键字"); } } } diff --git a/sonar-keyware-plugins-java/src/test/files/PathAndKeywordCheck.java b/sonar-keyware-plugins-java/src/test/files/PathAndKeywordCheck.java index 3759f31..5a2109f 100644 --- a/sonar-keyware-plugins-java/src/test/files/PathAndKeywordCheck.java +++ b/sonar-keyware-plugins-java/src/test/files/PathAndKeywordCheck.java @@ -2,9 +2,9 @@ class PathAndKeywordCheckRule { - public void getParameter(String arg) { + public void getParameter(int arg,String brg,float crg) { URL url = new URL(arg);// Noncompliant {{避免在参数中使用禁止的关键字}} - URI url = new URI(arg);// Noncompliant {{避免在参数中使用禁止的关键字}} - File url = new File(arg);// Noncompliant {{避免在参数中使用禁止的关键字}} + URI url = new URI(brg);// Noncompliant {{避免在参数中使用禁止的关键字}} + File url = new File(crg);// Noncompliant {{避免在参数中使用禁止的关键字}} } } \ No newline at end of file