-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added GridAnnotationInspection that check formatting of annotations.
- Loading branch information
Sergey Evdokimov
committed
Oct 25, 2014
1 parent
6ac15bc
commit d63822e
Showing
3 changed files
with
109 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<html> | ||
<body> | ||
Checks annotation formatting. | ||
<!-- tooltip end --> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
/* @java.file.header */ | ||
|
||
/* _________ _____ __________________ _____ | ||
* __ ____/___________(_)______ /__ ____/______ ____(_)_______ | ||
* _ / __ __ ___/__ / _ __ / _ / __ _ __ `/__ / __ __ \ | ||
* / /_/ / _ / _ / / /_/ / / /_/ / / /_/ / _ / _ / / / | ||
* \____/ /_/ /_/ \_,__/ \____/ \__,_/ /_/ /_/ /_/ | ||
*/ | ||
|
||
package org.gridgain.inspection; | ||
|
||
import com.intellij.codeInspection.*; | ||
import com.intellij.openapi.project.*; | ||
import com.intellij.psi.*; | ||
import com.intellij.psi.impl.source.tree.*; | ||
import com.intellij.psi.tree.*; | ||
import com.intellij.psi.util.*; | ||
import org.jetbrains.annotations.*; | ||
|
||
import java.util.*; | ||
|
||
/** | ||
* | ||
*/ | ||
public class GridAnnotationInspection extends BaseJavaLocalInspectionTool { | ||
/** {@inheritDoc} */ | ||
@Deprecated | ||
@NotNull @Override public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, boolean isOnTheFly) { | ||
return new JavaElementVisitor() { | ||
@Override public void visitMethod(PsiMethod method) { | ||
super.visitMethod(method); | ||
|
||
PsiModifierList modifierList = method.getModifierList(); | ||
|
||
if (modifierList == null) | ||
return; | ||
|
||
for (PsiElement e = modifierList.getFirstChild(); e != null; e = e.getNextSibling()) { | ||
if (!(e instanceof PsiAnnotation)) | ||
continue; | ||
|
||
PsiAnnotation ann = (PsiAnnotation)e; | ||
|
||
String name = ann.getQualifiedName(); | ||
|
||
if (Nullable.class.getName().equals(name)) | ||
checkSameLineWithMethodName(ann); | ||
else if (Override.class.getName().equals(name)) | ||
checkSameLineWithMethodName(ann); | ||
else { | ||
if (!hasLineBreak(ann, true) || !hasLineBreak(ann, false)) { | ||
holder.registerProblem(ann, "Annotation @" + ann.getNameReferenceElement().getReferenceName() | ||
+ " must be on the separated line"); | ||
} | ||
} | ||
} | ||
} | ||
|
||
private boolean hasLineBreak(@NotNull PsiElement element, boolean forward) { | ||
PsiElement e = element; | ||
|
||
while (true) { | ||
PsiElement next = forward ? e.getNextSibling() : e.getPrevSibling(); | ||
|
||
if (next == null) { | ||
e = e.getParent(); | ||
|
||
if (e == null) | ||
return true; | ||
|
||
continue; | ||
} | ||
|
||
e = next; | ||
|
||
if (e.getTextLength() == 0) | ||
continue; | ||
|
||
if (e instanceof PsiWhiteSpace || e instanceof PsiComment) { | ||
if (e.textContains('\n')) | ||
return true; | ||
} | ||
else { | ||
return false; | ||
} | ||
} | ||
} | ||
|
||
private void checkSameLineWithMethodName(@NotNull PsiAnnotation ann) { | ||
if (ann != null && hasLineBreak(ann, true)) { | ||
holder.registerProblem(ann, "Annotation @" + ann.getNameReferenceElement().getReferenceName() | ||
+ " must be on the same line with the method name"); | ||
} | ||
} | ||
}; | ||
} | ||
} |