@@ -15,8 +15,7 @@ import com.intellij.openapi.project.Project
1515import com.intellij.psi.*
1616import com.intellij.psi.PsiPrimitiveType.*
1717import com.intellij.psi.javadoc.PsiDocComment
18- import com.wuhao.code.check.Messages
19- import com.wuhao.code.check.getPsiElementFactory
18+ import com.wuhao.code.check.*
2019import com.wuhao.code.check.inspection.CodeFormatInspection
2120import com.wuhao.code.check.inspection.fix.ExtractToVariableFix
2221import com.wuhao.code.check.inspection.fix.JavaBlockCommentFix
@@ -59,32 +58,40 @@ class JavaCodeFormatVisitor(holder: ProblemsHolder) : BaseCodeFormatVisitor(hold
5958 if (element.parent is PsiExpressionList
6059 && element.text != " 0"
6160 && element.text != " 0L" && element.type in PRIMITIVE_TYPES ) {
62- holder.registerProblem(element, " 不得直接使用数字作为方法参数 " ,
61+ holder.registerProblem(element, " 不允许直接使用数字作为方法参数 " ,
6362 ProblemHighlightType .GENERIC_ERROR ,
6463 ExtractToVariableFix ())
6564 }
6665 }
6766 is PsiMethodCallExpression -> {
6867 // 使用日志输入代替System.out
6968 if (element.text.startsWith(" System.out" ) || element.text.startsWith(" System.err" )) {
70- holder.registerProblem(element, " 使用日志向控制台输出" , ProblemHighlightType .GENERIC_ERROR , object : LocalQuickFix {
69+ if (element.ancestorOfType<PsiMethod >() == null
70+ || ! element.ancestorsOfType<PsiMethod >().any { func ->
71+ func.annotations.any { annotation ->
72+ annotation.qualifiedName == JUNIT_TEST_ANNOTATION_CLASS_NAME
73+ }
74+ }
75+ ) {
76+ holder.registerProblem(element, " 使用日志向控制台输出" , ProblemHighlightType .GENERIC_ERROR , object : LocalQuickFix {
7177
72- override fun getFamilyName (): String {
73- return " 替换为日志输出"
74- }
78+ override fun getFamilyName (): String {
79+ return " 替换为日志输出"
80+ }
7581
76- override fun applyFix (project : Project , descriptor : ProblemDescriptor ) {
77- val el = descriptor.endElement
78- val factory = getPsiElementFactory(element)
79- if (el.firstChild is PsiReferenceExpression ) {
80- if (el.firstChild.text.startsWith(" System.out.print" )) {
81- el.firstChild.replace(factory.createExpressionFromText(" LOG.info" , null ))
82- } else if (el.firstChild.text.startsWith(" System.err.print" )) {
83- el.firstChild.replace(factory.createExpressionFromText(" LOG.error" , null ))
82+ override fun applyFix (project : Project , descriptor : ProblemDescriptor ) {
83+ val el = descriptor.endElement
84+ val factory = getPsiElementFactory(element)
85+ if (el.firstChild is PsiReferenceExpression ) {
86+ if (el.firstChild.text.startsWith(" System.out.print" )) {
87+ el.firstChild.replace(factory.createExpressionFromText(" LOG.info" , null ))
88+ } else if (el.firstChild.text.startsWith(" System.err.print" )) {
89+ el.firstChild.replace(factory.createExpressionFromText(" LOG.error" , null ))
90+ }
8491 }
8592 }
86- }
87- })
93+ })
94+ }
8895 }
8996 }
9097 is PsiMethod -> {
0 commit comments