From 525c976b5c934977b503cc87fda0468a86287e30 Mon Sep 17 00:00:00 2001 From: Anna Kozlova Date: Thu, 19 Dec 2019 19:43:46 +0100 Subject: [PATCH] reduce code duplication GitOrigin-RevId: f8a80566c9b60cd08d5cbf4d3cf1c5d37e147488 --- .../validation/InjectionNotApplicable.java | 57 ++++------------- .../PatternAnnotationNotApplicable.java | 56 ++++------------- ...ractAnnotationNotApplicableInspection.java | 62 +++++++++++++++++++ .../intelliLang/util/RemoveAnnotationFix.java | 47 -------------- 4 files changed, 86 insertions(+), 136 deletions(-) create mode 100644 plugins/IntelliLang/java-support/org/intellij/plugins/intelliLang/util/AbstractAnnotationNotApplicableInspection.java delete mode 100644 plugins/IntelliLang/java-support/org/intellij/plugins/intelliLang/util/RemoveAnnotationFix.java diff --git a/plugins/IntelliLang/java-support/org/intellij/plugins/intelliLang/inject/java/validation/InjectionNotApplicable.java b/plugins/IntelliLang/java-support/org/intellij/plugins/intelliLang/inject/java/validation/InjectionNotApplicable.java index ba533ca01f7d..1722bdbb2685 100644 --- a/plugins/IntelliLang/java-support/org/intellij/plugins/intelliLang/inject/java/validation/InjectionNotApplicable.java +++ b/plugins/IntelliLang/java-support/org/intellij/plugins/intelliLang/inject/java/validation/InjectionNotApplicable.java @@ -15,60 +15,27 @@ */ package org.intellij.plugins.intelliLang.inject.java.validation; -import com.intellij.codeInsight.AnnotationUtil; -import com.intellij.codeInspection.LocalInspectionTool; -import com.intellij.codeInspection.ProblemsHolder; -import com.intellij.psi.*; -import com.intellij.psi.util.PsiTreeUtil; +import com.intellij.openapi.project.Project; +import com.intellij.psi.PsiType; import org.intellij.plugins.intelliLang.Configuration; +import org.intellij.plugins.intelliLang.util.AbstractAnnotationNotApplicableInspection; import org.intellij.plugins.intelliLang.util.PsiUtilEx; -import org.intellij.plugins.intelliLang.util.RemoveAnnotationFix; -import org.jetbrains.annotations.NotNull; -import static com.intellij.codeInsight.AnnotationUtil.CHECK_EXTERNAL; - -public class InjectionNotApplicable extends LocalInspectionTool { +public class InjectionNotApplicable extends AbstractAnnotationNotApplicableInspection { @Override - @NotNull - public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, boolean isOnTheFly) { - return new JavaElementVisitor() { - final String annotationName = Configuration.getProjectInstance(holder.getProject()).getAdvancedConfiguration().getLanguageAnnotationClass(); - - @Override - public void visitAnnotation(PsiAnnotation annotation) { - final String name = annotation.getQualifiedName(); - if (annotationName.equals(name)) { - checkAnnotation(annotation, holder); - } - else if (name != null) { - final PsiClass psiClass = JavaPsiFacade.getInstance(annotation.getProject()).findClass(name, annotation.getResolveScope()); - if (psiClass != null && AnnotationUtil.isAnnotated(psiClass, annotationName, CHECK_EXTERNAL)) { - checkAnnotation(annotation, holder); - } - } - } - }; + protected String getAnnotationName(Project project) { + return Configuration.getProjectInstance(project).getAdvancedConfiguration().getLanguageAnnotationClass(); } - private void checkAnnotation(PsiAnnotation annotation, ProblemsHolder holder) { - final PsiModifierListOwner owner = PsiTreeUtil.getParentOfType(annotation, PsiModifierListOwner.class); - if (owner instanceof PsiVariable) { - final PsiType type = ((PsiVariable)owner).getType(); - if (!PsiUtilEx.isStringOrStringArray(type)) { - registerProblem(annotation, holder); - } - } - else if (owner instanceof PsiMethod) { - final PsiType type = ((PsiMethod)owner).getReturnType(); - if (type == null || !PsiUtilEx.isStringOrStringArray(type)) { - registerProblem(annotation, holder); - } - } + @Override + protected boolean isTypeApplicable(PsiType type) { + return type == null || !PsiUtilEx.isStringOrStringArray(type); } - private void registerProblem(PsiAnnotation annotation, ProblemsHolder holder) { - holder.registerProblem(annotation, "Language Injection is only applicable to elements of type String", new RemoveAnnotationFix(this)); + @Override + protected String getDescriptionTemplate() { + return "Language Injection is only applicable to elements of type String"; } } \ No newline at end of file diff --git a/plugins/IntelliLang/java-support/org/intellij/plugins/intelliLang/pattern/PatternAnnotationNotApplicable.java b/plugins/IntelliLang/java-support/org/intellij/plugins/intelliLang/pattern/PatternAnnotationNotApplicable.java index 795772c4eb9b..1e6e0411225a 100644 --- a/plugins/IntelliLang/java-support/org/intellij/plugins/intelliLang/pattern/PatternAnnotationNotApplicable.java +++ b/plugins/IntelliLang/java-support/org/intellij/plugins/intelliLang/pattern/PatternAnnotationNotApplicable.java @@ -15,61 +15,29 @@ */ package org.intellij.plugins.intelliLang.pattern; -import com.intellij.codeInsight.AnnotationUtil; -import com.intellij.codeInspection.LocalInspectionTool; -import com.intellij.codeInspection.ProblemsHolder; -import com.intellij.psi.*; -import com.intellij.psi.util.PsiTreeUtil; +import com.intellij.openapi.project.Project; +import com.intellij.psi.PsiType; import org.intellij.plugins.intelliLang.Configuration; +import org.intellij.plugins.intelliLang.util.AbstractAnnotationNotApplicableInspection; import org.intellij.plugins.intelliLang.util.PsiUtilEx; -import org.intellij.plugins.intelliLang.util.RemoveAnnotationFix; import org.jetbrains.annotations.NonNls; import org.jetbrains.annotations.NotNull; -import static com.intellij.codeInsight.AnnotationUtil.CHECK_EXTERNAL; - -public class PatternAnnotationNotApplicable extends LocalInspectionTool { +public class PatternAnnotationNotApplicable extends AbstractAnnotationNotApplicableInspection { @Override - @NotNull - public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, boolean isOnTheFly) { - return new JavaElementVisitor() { - final String annotationName = Configuration.getProjectInstance(holder.getProject()).getAdvancedConfiguration().getPatternAnnotationClass(); - - @Override - public void visitAnnotation(PsiAnnotation annotation) { - final String name = annotation.getQualifiedName(); - if (annotationName.equals(name)) { - checkAnnotation(annotation, holder); - } - else if (name != null) { - final PsiClass psiClass = JavaPsiFacade.getInstance(annotation.getProject()).findClass(name, annotation.getResolveScope()); - if (psiClass != null && AnnotationUtil.isAnnotated(psiClass, annotationName, CHECK_EXTERNAL)) { - checkAnnotation(annotation, holder); - } - } - } - }; + protected String getAnnotationName(Project project) { + return Configuration.getProjectInstance(project).getAdvancedConfiguration().getPatternAnnotationClass(); } - private void checkAnnotation(PsiAnnotation annotation, ProblemsHolder holder) { - final PsiModifierListOwner owner = PsiTreeUtil.getParentOfType(annotation, PsiModifierListOwner.class); - if (owner instanceof PsiVariable) { - final PsiType type = ((PsiVariable)owner).getType(); - if (!PsiUtilEx.isString(type)) { - registerProblem(annotation, holder); - } - } - else if (owner instanceof PsiMethod) { - final PsiType type = ((PsiMethod)owner).getReturnType(); - if (type != null && !PsiUtilEx.isString(type)) { - registerProblem(annotation, holder); - } - } + @Override + protected boolean isTypeApplicable(PsiType type) { + return type != null && !PsiUtilEx.isString(type); } - private void registerProblem(PsiAnnotation annotation, ProblemsHolder holder) { - holder.registerProblem(annotation, "Pattern Annotation is only applicable to elements of type String", new RemoveAnnotationFix(this)); + @Override + protected String getDescriptionTemplate() { + return "Pattern Annotation is only applicable to elements of type String"; } @Override diff --git a/plugins/IntelliLang/java-support/org/intellij/plugins/intelliLang/util/AbstractAnnotationNotApplicableInspection.java b/plugins/IntelliLang/java-support/org/intellij/plugins/intelliLang/util/AbstractAnnotationNotApplicableInspection.java new file mode 100644 index 000000000000..978a41a6069c --- /dev/null +++ b/plugins/IntelliLang/java-support/org/intellij/plugins/intelliLang/util/AbstractAnnotationNotApplicableInspection.java @@ -0,0 +1,62 @@ +// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. +package org.intellij.plugins.intelliLang.util; + +import com.intellij.codeInsight.AnnotationUtil; +import com.intellij.codeInspection.LocalInspectionTool; +import com.intellij.codeInspection.ProblemsHolder; +import com.intellij.codeInspection.RemoveAnnotationQuickFix; +import com.intellij.openapi.project.Project; +import com.intellij.psi.*; +import com.intellij.psi.util.PsiTreeUtil; +import org.jetbrains.annotations.NotNull; + +import static com.intellij.codeInsight.AnnotationUtil.CHECK_EXTERNAL; + +public abstract class AbstractAnnotationNotApplicableInspection extends LocalInspectionTool { + protected abstract String getAnnotationName(Project project); + protected abstract boolean isTypeApplicable(PsiType type); + protected abstract String getDescriptionTemplate(); + + + @Override + @NotNull + public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, boolean isOnTheFly) { + return new JavaElementVisitor() { + final String annotationName = getAnnotationName(holder.getProject()); + + @Override + public void visitAnnotation(PsiAnnotation annotation) { + final String name = annotation.getQualifiedName(); + if (annotationName.equals(name)) { + checkAnnotation(annotation, holder); + } + else if (name != null) { + final PsiClass psiClass = JavaPsiFacade.getInstance(annotation.getProject()).findClass(name, annotation.getResolveScope()); + if (psiClass != null && AnnotationUtil.isAnnotated(psiClass, annotationName, CHECK_EXTERNAL)) { + checkAnnotation(annotation, holder); + } + } + } + }; + } + + private void checkAnnotation(PsiAnnotation annotation, ProblemsHolder holder) { + final PsiModifierListOwner owner = PsiTreeUtil.getParentOfType(annotation, PsiModifierListOwner.class); + if (owner instanceof PsiVariable) { + final PsiType type = ((PsiVariable)owner).getType(); + if (isTypeApplicable(type)) { + registerProblem(annotation, holder); + } + } + else if (owner instanceof PsiMethod) { + final PsiType type = ((PsiMethod)owner).getReturnType(); + if (isTypeApplicable(type)) { + registerProblem(annotation, holder); + } + } + } + + private void registerProblem(PsiAnnotation annotation, ProblemsHolder holder) { + holder.registerProblem(annotation, getDescriptionTemplate(), new RemoveAnnotationQuickFix(annotation, null)); + } +} diff --git a/plugins/IntelliLang/java-support/org/intellij/plugins/intelliLang/util/RemoveAnnotationFix.java b/plugins/IntelliLang/java-support/org/intellij/plugins/intelliLang/util/RemoveAnnotationFix.java deleted file mode 100644 index 910e449e32e0..000000000000 --- a/plugins/IntelliLang/java-support/org/intellij/plugins/intelliLang/util/RemoveAnnotationFix.java +++ /dev/null @@ -1,47 +0,0 @@ -/* - * Copyright 2006 Sascha Weinreuter - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.intellij.plugins.intelliLang.util; - -import com.intellij.codeInspection.LocalInspectionTool; -import com.intellij.codeInspection.LocalQuickFix; -import com.intellij.codeInspection.ProblemDescriptor; -import com.intellij.openapi.project.Project; -import org.jetbrains.annotations.NotNull; - -public class RemoveAnnotationFix implements LocalQuickFix { - private final LocalInspectionTool myTool; - - public RemoveAnnotationFix(LocalInspectionTool tool) { - myTool = tool; - } - - @Override - @NotNull - public String getName() { - return "Remove Annotation"; - } - - @Override - @NotNull - public String getFamilyName() { - return myTool.getGroupDisplayName(); - } - - @Override - public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) { - descriptor.getPsiElement().delete(); - } -}