Skip to content

Commit c263816

Browse files
committed
包含在junit的test方法中的print语句将不再提示错误
1 parent 3472f85 commit c263816

4 files changed

Lines changed: 58 additions & 21 deletions

File tree

src/com/wuhao/code/check/CommonConstants.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ import com.intellij.psi.PsiElement
88
import com.intellij.psi.impl.PsiElementFactoryImpl
99
import com.intellij.psi.impl.PsiManagerEx
1010

11+
12+
/**
13+
* junit的Test注解的类名称
14+
*/
15+
const val JUNIT_TEST_ANNOTATION_CLASS_NAME = "org.junit.Test"
1116
/**
1217
* 默认的vue项目模板git地址
1318
*/

src/com/wuhao/code/check/Ext.kt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,21 @@ inline fun <reified T> PsiElement.ancestorOfType(): T? {
1717
return el as T?
1818
}
1919

20+
/**
21+
* 获取指定类型的所有的祖先元素
22+
*/
23+
inline fun <reified T> PsiElement.ancestorsOfType(): ArrayList<T> {
24+
val result = arrayListOf<T>()
25+
var el: PsiElement? = this.parent
26+
while (el != null) {
27+
if (el is T) {
28+
result.add(el)
29+
}
30+
el = el.parent
31+
}
32+
return result
33+
}
34+
2035
/**
2136
* 获取psi元素的所有祖先元素,按距离从近到远
2237
*/

src/com/wuhao/code/check/inspection/visitor/JavaCodeFormatVisitor.kt

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ import com.intellij.openapi.project.Project
1515
import com.intellij.psi.*
1616
import com.intellij.psi.PsiPrimitiveType.*
1717
import 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.*
2019
import com.wuhao.code.check.inspection.CodeFormatInspection
2120
import com.wuhao.code.check.inspection.fix.ExtractToVariableFix
2221
import 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 -> {

src/com/wuhao/code/check/inspection/visitor/KotlinCodeFormatVisitor.kt

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,10 @@ import com.intellij.lang.Language
1212
import com.intellij.openapi.project.Project
1313
import com.intellij.psi.PsiElement
1414
import com.intellij.psi.impl.source.tree.LeafPsiElement
15-
import com.wuhao.code.check.Messages
16-
import com.wuhao.code.check.hasDocComment
15+
import com.wuhao.code.check.*
1716
import com.wuhao.code.check.inspection.CodeFormatInspection
1817
import com.wuhao.code.check.inspection.fix.KotlinCommentQuickFix
19-
import com.wuhao.code.check.isFirstLevelProperty
18+
import org.jetbrains.kotlin.asJava.toLightAnnotation
2019
import org.jetbrains.kotlin.idea.KotlinLanguage
2120
import org.jetbrains.kotlin.idea.refactoring.getLineCount
2221
import org.jetbrains.kotlin.kdoc.psi.api.KDoc
@@ -70,8 +69,19 @@ class KotlinCodeFormatVisitor(holder: ProblemsHolder) : BaseCodeFormatVisitor(ho
7069
}
7170
is KtReferenceExpression -> {
7271
// 使用日志输入代替System.out
72+
7373
if (element.text == "println") {
74-
holder.registerProblem(element, "使用日志向控制台输出", ProblemHighlightType.GENERIC_ERROR)
74+
if (element.ancestorOfType<KtFunction>() == null
75+
|| !element.ancestorsOfType<KtFunction>().any { func ->
76+
func.annotationEntries.map { annoEntry ->
77+
annoEntry.toLightAnnotation()
78+
}.any { lightAnnotation ->
79+
lightAnnotation?.qualifiedName == JUNIT_TEST_ANNOTATION_CLASS_NAME
80+
}
81+
}
82+
) {
83+
holder.registerProblem(element, "使用日志向控制台输出", ProblemHighlightType.GENERIC_ERROR)
84+
}
7585
}
7686
}
7787
is LeafPsiElement -> {

0 commit comments

Comments
 (0)