Skip to content

Commit

Permalink
Added GridAnnotationInspection that check formatting of annotations.
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergey Evdokimov committed Oct 25, 2014
1 parent 6ac15bc commit d63822e
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 0 deletions.
6 changes: 6 additions & 0 deletions META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@
enabledByDefault="true"
/>

<localInspection implementationClass="org.gridgain.inspection.GridAnnotationInspection"
displayName="Annotation formatting"
language="JAVA"
groupName="GridGain"
enabledByDefault="true"/>

<intentionAction>
<className>org.gridgain.intention.GridGetterSetterGenerator</className>
<category>Getter Setter</category>
Expand Down
6 changes: 6 additions & 0 deletions src/inspectionDescriptions/GridAnnotation.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<html>
<body>
Checks annotation formatting.
<!-- tooltip end -->
</body>
</html>
97 changes: 97 additions & 0 deletions src/org/gridgain/inspection/GridAnnotationInspection.java
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");
}
}
};
}
}

0 comments on commit d63822e

Please sign in to comment.