mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
reduce code duplication
GitOrigin-RevId: f8a80566c9b60cd08d5cbf4d3cf1c5d37e147488
This commit is contained in:
committed by
intellij-monorepo-bot
parent
e6de8d8c64
commit
525c976b5c
+12
-45
@@ -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";
|
||||
}
|
||||
|
||||
}
|
||||
+12
-44
@@ -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
|
||||
|
||||
+62
@@ -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));
|
||||
}
|
||||
}
|
||||
-47
@@ -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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user