From d63822e0cfbf0af721a87af39e9b45689d2ad145 Mon Sep 17 00:00:00 2001 From: Sergey Evdokimov Date: Sat, 25 Oct 2014 17:37:10 +0400 Subject: [PATCH] Added GridAnnotationInspection that check formatting of annotations. --- META-INF/plugin.xml | 6 ++ .../GridAnnotation.html | 6 ++ .../inspection/GridAnnotationInspection.java | 97 +++++++++++++++++++ 3 files changed, 109 insertions(+) create mode 100644 src/inspectionDescriptions/GridAnnotation.html create mode 100644 src/org/gridgain/inspection/GridAnnotationInspection.java diff --git a/META-INF/plugin.xml b/META-INF/plugin.xml index ced5948..6fa3dfd 100644 --- a/META-INF/plugin.xml +++ b/META-INF/plugin.xml @@ -46,6 +46,12 @@ enabledByDefault="true" /> + + org.gridgain.intention.GridGetterSetterGenerator Getter Setter diff --git a/src/inspectionDescriptions/GridAnnotation.html b/src/inspectionDescriptions/GridAnnotation.html new file mode 100644 index 0000000..d529c51 --- /dev/null +++ b/src/inspectionDescriptions/GridAnnotation.html @@ -0,0 +1,6 @@ + + +Checks annotation formatting. + + + \ No newline at end of file diff --git a/src/org/gridgain/inspection/GridAnnotationInspection.java b/src/org/gridgain/inspection/GridAnnotationInspection.java new file mode 100644 index 0000000..4e289ad --- /dev/null +++ b/src/org/gridgain/inspection/GridAnnotationInspection.java @@ -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"); + } + } + }; + } +}