mirror of
https://github.com/alibaba/p3c.git
synced 2025-10-15 23:53:58 +00:00
Merge branch 'master' of https://github.com/alibaba/p3c
This commit is contained in:
@@ -11,7 +11,7 @@ Help -> Install New Software...
|
|||||||
|
|
||||||

|

|
||||||
|
|
||||||
`注意:有同学反映插件扫描会触发JPA插件启动后台线程执行不明任务,如果不需要的话卸载掉JPA插件即可,目前尚未发现原因`
|
注意:有同学反映插件扫描会触发很多 "JPA Java Change Event Handler (Waiting)" 的任务,这个是Eclipse的一个[bug](https://bugs.eclipse.org/bugs/show_bug.cgi?id=387455),因为插件在扫描的时候会对文件进行标记,所以触发了JPA的任务。卸载JPA插件,或者尝试升级到最新版的Eclipse。附:[JPA project Change Event Handler问题解决](https://my.oschina.net/cimu/blog/278724)
|
||||||
|
|
||||||
|
|
||||||
## 插件使用
|
## 插件使用
|
||||||
@@ -23,7 +23,7 @@ Help -> Install New Software...
|
|||||||
* long或者Long初始赋值时,必须使用大写的L,不能是小写的l)
|
* long或者Long初始赋值时,必须使用大写的L,不能是小写的l)
|
||||||
* Object的equals方法容易抛空指针异常,应使用常量或确定有值的对象来调用equals。
|
* Object的equals方法容易抛空指针异常,应使用常量或确定有值的对象来调用equals。
|
||||||
|
|
||||||
目前不支持代码实时检测,需要手动触发,希望更多的人加入进来一起把咱们的插件做得越来越来,尽量提升研发的使用体验。
|
目前不支持代码实时检测,需要手动触发,希望更多的人加入进来一起把咱们的插件做得越来越好,尽量提升研发的使用体验。
|
||||||
|
|
||||||
|
|
||||||
### 代码扫描
|
### 代码扫描
|
||||||
|
@@ -17,8 +17,10 @@ package com.alibaba.p3c.idea.inspection
|
|||||||
|
|
||||||
import com.alibaba.p3c.idea.config.P3cConfig
|
import com.alibaba.p3c.idea.config.P3cConfig
|
||||||
import com.alibaba.p3c.idea.pmd.AliPmdProcessor
|
import com.alibaba.p3c.idea.pmd.AliPmdProcessor
|
||||||
|
import com.alibaba.p3c.idea.util.DocumentUtils.calculateLineStart
|
||||||
import com.alibaba.p3c.idea.util.DocumentUtils.calculateRealOffset
|
import com.alibaba.p3c.idea.util.DocumentUtils.calculateRealOffset
|
||||||
import com.alibaba.p3c.idea.util.ProblemsUtils
|
import com.alibaba.p3c.idea.util.ProblemsUtils
|
||||||
|
import com.alibaba.p3c.pmd.lang.java.rule.comment.RemoveCommentedCodeRule
|
||||||
import com.beust.jcommander.internal.Lists
|
import com.beust.jcommander.internal.Lists
|
||||||
import com.google.common.cache.Cache
|
import com.google.common.cache.Cache
|
||||||
import com.google.common.cache.CacheBuilder
|
import com.google.common.cache.CacheBuilder
|
||||||
@@ -39,9 +41,11 @@ import java.util.concurrent.TimeUnit
|
|||||||
* @author caikang
|
* @author caikang
|
||||||
* @date 2016/12/13
|
* @date 2016/12/13
|
||||||
*/
|
*/
|
||||||
class AliPmdInspectionInvoker(private val psiFile: PsiFile,
|
class AliPmdInspectionInvoker(
|
||||||
|
private val psiFile: PsiFile,
|
||||||
private val manager: InspectionManager,
|
private val manager: InspectionManager,
|
||||||
private val rule: Rule) {
|
private val rule: Rule
|
||||||
|
) {
|
||||||
val logger = Logger.getInstance(javaClass)
|
val logger = Logger.getInstance(javaClass)
|
||||||
|
|
||||||
private var violations: List<RuleViolation> = emptyList()
|
private var violations: List<RuleViolation> = emptyList()
|
||||||
@@ -64,15 +68,21 @@ class AliPmdInspectionInvoker(private val psiFile: PsiFile,
|
|||||||
val virtualFile = LocalFileSystem.getInstance().findFileByPath(rv.filename) ?: continue
|
val virtualFile = LocalFileSystem.getInstance().findFileByPath(rv.filename) ?: continue
|
||||||
val psiFile = PsiManager.getInstance(manager.project).findFile(virtualFile) ?: continue
|
val psiFile = PsiManager.getInstance(manager.project).findFile(virtualFile) ?: continue
|
||||||
val document = FileDocumentManager.getInstance().getDocument(virtualFile) ?: continue
|
val document = FileDocumentManager.getInstance().getDocument(virtualFile) ?: continue
|
||||||
val offset = calculateRealOffset(document, rv.beginLine, rv.beginColumn)
|
|
||||||
val endOffset = calculateRealOffset(document, rv.endLine, rv.endColumn)
|
val offsets = if (rv.rule.name == RemoveCommentedCodeRule::class.java.simpleName) {
|
||||||
|
Offsets(calculateLineStart(document, rv.beginLine),
|
||||||
|
calculateLineStart(document, rv.endLine + 1) - 1)
|
||||||
|
} else {
|
||||||
|
Offsets(calculateRealOffset(document, rv.beginLine, rv.beginColumn),
|
||||||
|
calculateRealOffset(document, rv.endLine, rv.endColumn))
|
||||||
|
}
|
||||||
val errorMessage = if (isOnTheFly) {
|
val errorMessage = if (isOnTheFly) {
|
||||||
rv.description
|
rv.description
|
||||||
} else {
|
} else {
|
||||||
"${rv.description} (line ${rv.beginLine})"
|
"${rv.description} (line ${rv.beginLine})"
|
||||||
}
|
}
|
||||||
val problemDescriptor = ProblemsUtils.createProblemDescriptorForPmdRule(psiFile, manager,
|
val problemDescriptor = ProblemsUtils.createProblemDescriptorForPmdRule(psiFile, manager,
|
||||||
isOnTheFly, rv.rule.name, errorMessage, offset, endOffset, rv.beginLine) ?: continue
|
isOnTheFly, rv.rule.name, errorMessage, offsets.start, offsets.end, rv.beginLine) ?: continue
|
||||||
problemDescriptors.add(problemDescriptor)
|
problemDescriptors.add(problemDescriptor)
|
||||||
}
|
}
|
||||||
return problemDescriptors.toTypedArray()
|
return problemDescriptors.toTypedArray()
|
||||||
@@ -130,3 +140,4 @@ class AliPmdInspectionInvoker(private val psiFile: PsiFile,
|
|||||||
}
|
}
|
||||||
|
|
||||||
data class FileRule(val filePath: String, val ruleName: String)
|
data class FileRule(val filePath: String, val ruleName: String)
|
||||||
|
data class Offsets(val start: Int, val end: Int)
|
@@ -36,6 +36,14 @@ object DocumentUtils {
|
|||||||
return lineOffset + calculateRealColumn(document, line, pmdColumn)
|
return lineOffset + calculateRealColumn(document, line, pmdColumn)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun calculateLineStart(document: Document, line: Int): Int {
|
||||||
|
val maxLine = document.lineCount
|
||||||
|
if (maxLine < line) {
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
return document.getLineStartOffset(line - 1)
|
||||||
|
}
|
||||||
|
|
||||||
fun calculateRealColumn(document: Document, line: Int, pmdColumn: Int): Int {
|
fun calculateRealColumn(document: Document, line: Int, pmdColumn: Int): Int {
|
||||||
var realColumn = pmdColumn - 1
|
var realColumn = pmdColumn - 1
|
||||||
val minusSize = PMD_TAB_SIZE - 1
|
val minusSize = PMD_TAB_SIZE - 1
|
||||||
|
@@ -15,6 +15,7 @@
|
|||||||
*/
|
*/
|
||||||
package com.alibaba.p3c.idea.util
|
package com.alibaba.p3c.idea.util
|
||||||
|
|
||||||
|
import com.alibaba.p3c.pmd.lang.java.rule.comment.AvoidCommentBehindStatementRule
|
||||||
import com.intellij.codeInspection.InspectionManager
|
import com.intellij.codeInspection.InspectionManager
|
||||||
import com.intellij.codeInspection.LocalQuickFix
|
import com.intellij.codeInspection.LocalQuickFix
|
||||||
import com.intellij.codeInspection.ProblemDescriptor
|
import com.intellij.codeInspection.ProblemDescriptor
|
||||||
@@ -39,7 +40,8 @@ import com.intellij.psi.impl.source.tree.ElementType
|
|||||||
6
|
6
|
||||||
*/
|
*/
|
||||||
object ProblemsUtils {
|
object ProblemsUtils {
|
||||||
private val highlightLineRules = setOf("AvoidCommentBehindStatement")
|
private val highlightLineRules = setOf(AvoidCommentBehindStatementRule::class.java.simpleName)
|
||||||
|
|
||||||
fun createProblemDescriptorForPmdRule(psiFile: PsiFile, manager: InspectionManager, isOnTheFly: Boolean,
|
fun createProblemDescriptorForPmdRule(psiFile: PsiFile, manager: InspectionManager, isOnTheFly: Boolean,
|
||||||
ruleName: String, desc: String, start: Int, end: Int,
|
ruleName: String, desc: String, start: Int, end: Int,
|
||||||
checkLine: Int = 0,
|
checkLine: Int = 0,
|
||||||
|
@@ -16,7 +16,8 @@ version plugin_version
|
|||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
compile group: 'org.freemarker', name: 'freemarker', version: '2.3.25-incubating'
|
compile group: 'org.freemarker', name: 'freemarker', version: '2.3.25-incubating'
|
||||||
compile 'com.alibaba.p3c.idea:p3c-common:1.0.0'
|
//compile 'com.alibaba.p3c.idea:p3c-common:1.0.0'
|
||||||
|
compile project(':p3c-common')
|
||||||
compile group: 'org.javassist', name: 'javassist', version: '3.21.0-GA'
|
compile group: 'org.javassist', name: 'javassist', version: '3.21.0-GA'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -204,7 +204,7 @@ Positive example:
|
|||||||
// please refer to the pseudo-code as follows
|
// please refer to the pseudo-code as follows
|
||||||
boolean existed = (file.open(fileName, "w") != null) && (...) || (...);
|
boolean existed = (file.open(fileName, "w") != null) && (...) || (...);
|
||||||
if (existed) {
|
if (existed) {
|
||||||
...
|
//...
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -212,7 +212,7 @@ Positive example:
|
|||||||
|
|
||||||
```java
|
```java
|
||||||
if ((file.open(fileName, "w") != null) && (...) || (...)) {
|
if ((file.open(fileName, "w") != null) && (...) || (...)) {
|
||||||
...
|
// ...
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@@ -41,6 +41,7 @@ public class NodeUtils {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* TODO optimize
|
* TODO optimize
|
||||||
|
*
|
||||||
* @param expression expression
|
* @param expression expression
|
||||||
* @return true if wrapper type
|
* @return true if wrapper type
|
||||||
*/
|
*/
|
||||||
|
@@ -42,7 +42,7 @@
|
|||||||
<![CDATA[
|
<![CDATA[
|
||||||
Positive example:
|
Positive example:
|
||||||
String KEY_PRE = "Id#taobao_1";
|
String KEY_PRE = "Id#taobao_1";
|
||||||
if(key.equals(KEY_PRE)){
|
if (KEY_PRE.equals(key)) {
|
||||||
//...
|
//...
|
||||||
}
|
}
|
||||||
]]>
|
]]>
|
||||||
|
@@ -49,7 +49,7 @@
|
|||||||
<![CDATA[
|
<![CDATA[
|
||||||
Negative example:
|
Negative example:
|
||||||
if ((file.open(fileName, "w") != null) && (...) || (...)) {
|
if ((file.open(fileName, "w") != null) && (...) || (...)) {
|
||||||
...
|
// ...
|
||||||
}
|
}
|
||||||
]]>
|
]]>
|
||||||
</example>
|
</example>
|
||||||
@@ -58,7 +58,7 @@ Negative example:
|
|||||||
Positive example:
|
Positive example:
|
||||||
boolean existed = (file.open(fileName, "w") != null) && (...) || (...);
|
boolean existed = (file.open(fileName, "w") != null) && (...) || (...);
|
||||||
if (existed) {
|
if (existed) {
|
||||||
...
|
//...
|
||||||
}
|
}
|
||||||
]]>
|
]]>
|
||||||
</example>
|
</example>
|
||||||
|
@@ -34,7 +34,7 @@
|
|||||||
Integer a = 235;
|
Integer a = 235;
|
||||||
Integer b = 235;
|
Integer b = 235;
|
||||||
if (a.equals(b)) {
|
if (a.equals(b)) {
|
||||||
//相等
|
// code
|
||||||
}
|
}
|
||||||
]]>
|
]]>
|
||||||
</example>
|
</example>
|
||||||
@@ -112,7 +112,7 @@
|
|||||||
|
|
||||||
<example>
|
<example>
|
||||||
<![CDATA[
|
<![CDATA[
|
||||||
反例:
|
Negative example:
|
||||||
String result;
|
String result;
|
||||||
for (String string : tagNameList) {
|
for (String string : tagNameList) {
|
||||||
result = result + string;
|
result = result + string;
|
||||||
@@ -121,7 +121,7 @@
|
|||||||
</example>
|
</example>
|
||||||
<example>
|
<example>
|
||||||
<![CDATA[
|
<![CDATA[
|
||||||
正例:
|
Positive example:
|
||||||
StringBuilder stringBuilder = new StringBuilder();
|
StringBuilder stringBuilder = new StringBuilder();
|
||||||
for (String string : tagNameList) {
|
for (String string : tagNameList) {
|
||||||
stringBuilder.append(string);
|
stringBuilder.append(string);
|
||||||
|
@@ -104,7 +104,6 @@ Negative example:
|
|||||||
if (delCondition) {
|
if (delCondition) {
|
||||||
it.remove();
|
it.remove();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
]]>
|
]]>
|
||||||
</example>
|
</example>
|
||||||
|
Reference in New Issue
Block a user