Implement base class for UAST-based inspections

This commit is contained in:
Dmitry Jemerov
2017-02-17 19:59:32 +01:00
parent be0894b76e
commit 546374f073
4 changed files with 221 additions and 4 deletions

View File

@@ -0,0 +1,115 @@
/*
* Copyright 2000-2017 JetBrains s.r.o.
*
* 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 com.intellij.codeInspection;
import com.intellij.openapi.util.Condition;
import com.intellij.openapi.util.Conditions;
import com.intellij.psi.*;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.uast.UastVisitorAdapter;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.uast.UClass;
import org.jetbrains.uast.UField;
import org.jetbrains.uast.UFile;
import org.jetbrains.uast.UMethod;
import org.jetbrains.uast.visitor.AbstractUastVisitor;
public abstract class AbstractBaseUastLocalInspectionTool extends LocalInspectionTool {
private static final Condition<PsiElement> PROBLEM_ELEMENT_CONDITION = Conditions.and(Conditions.instanceOf(PsiFile.class, PsiClass.class, PsiMethod.class, PsiField.class), Conditions.notInstanceOf(PsiTypeParameter.class));
/**
* Override this to report problems at method level.
*
* @param method to check.
* @param manager InspectionManager to ask for ProblemDescriptors from.
* @param isOnTheFly true if called during on the fly editor highlighting. Called from Inspect Code action otherwise.
* @return {@code null} if no problems found or not applicable at method level.
*/
@Nullable
public ProblemDescriptor[] checkMethod(@NotNull UMethod method, @NotNull InspectionManager manager, boolean isOnTheFly) {
return null;
}
/**
* Override this to report problems at class level.
*
* @param aClass to check.
* @param manager InspectionManager to ask for ProblemDescriptors from.
* @param isOnTheFly true if called during on the fly editor highlighting. Called from Inspect Code action otherwise.
* @return {@code null} if no problems found or not applicable at class level.
*/
@Nullable
public ProblemDescriptor[] checkClass(@NotNull UClass aClass, @NotNull InspectionManager manager, boolean isOnTheFly) {
return null;
}
/**
* Override this to report problems at field level.
*
* @param field to check.
* @param manager InspectionManager to ask for ProblemDescriptors from.
* @param isOnTheFly true if called during on the fly editor highlighting. Called from Inspect Code action otherwise.
* @return {@code null} if no problems found or not applicable at field level.
*/
@Nullable
public ProblemDescriptor[] checkField(@NotNull UField field, @NotNull InspectionManager manager, boolean isOnTheFly) {
return null;
}
@Override
@NotNull
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, final boolean isOnTheFly) {
return new UastVisitorAdapter(new AbstractUastVisitor() {
@Override
public boolean visitClass(UClass node) {
addDescriptors(checkClass(node, holder.getManager(), isOnTheFly));
return true;
}
@Override
public boolean visitMethod(UMethod node) {
addDescriptors(checkMethod(node, holder.getManager(), isOnTheFly));
return true;
}
@Override
public boolean visitField(UField node) {
addDescriptors(checkField(node, holder.getManager(), isOnTheFly));
return true;
}
@Override
public boolean visitFile(UFile node) {
addDescriptors(checkFile(node.getPsi(), holder.getManager(), isOnTheFly));
return true;
}
private void addDescriptors(final ProblemDescriptor[] descriptors) {
if (descriptors != null) {
for (ProblemDescriptor descriptor : descriptors) {
holder.registerProblem(descriptor);
}
}
}
});
}
@Override
public PsiNamedElement getProblemElement(final PsiElement psiElement) {
return (PsiNamedElement)PsiTreeUtil.findFirstParent(psiElement, PROBLEM_ELEMENT_CONDITION);
}
}

View File

@@ -0,0 +1,42 @@
/*
* Copyright 2000-2017 JetBrains s.r.o.
*
* 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 com.intellij.uast;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiElementVisitor;
import org.jetbrains.uast.UElement;
import org.jetbrains.uast.UastContextKt;
import org.jetbrains.uast.visitor.UastVisitor;
/**
* @author yole
*/
public class UastVisitorAdapter extends PsiElementVisitor {
private final UastVisitor myUastVisitor;
public UastVisitorAdapter(UastVisitor visitor) {
myUastVisitor = visitor;
}
@Override
public void visitElement(PsiElement element) {
super.visitElement(element);
UElement uElement = UastContextKt.toUElement(element);
if (uElement != null) {
uElement.accept(myUastVisitor);
}
}
}

View File

@@ -0,0 +1,56 @@
/*
* Copyright 2000-2017 JetBrains s.r.o.
*
* 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.jetbrains.idea.devkit.inspections;
import com.intellij.codeInspection.AbstractBaseUastLocalInspectionTool;
import com.intellij.codeInspection.ProblemsHolder;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.ModuleUtilCore;
import com.intellij.psi.PsiElementVisitor;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.idea.devkit.module.PluginModuleType;
import org.jetbrains.idea.devkit.util.PsiUtil;
/**
* @author yole
*/
public abstract class DevKitUastInspectionBase extends AbstractBaseUastLocalInspectionTool {
@NotNull
@Override
public final PsiElementVisitor buildVisitor(@NotNull ProblemsHolder holder, boolean isOnTheFly) {
return isAllowed(holder) ? buildInternalVisitor(holder, isOnTheFly) : PsiElementVisitor.EMPTY_VISITOR;
}
protected boolean isAllowed(ProblemsHolder holder) {
if (PsiUtil.isIdeaProject(holder.getProject())) {
return true;
}
Module module = ModuleUtilCore.findModuleForPsiElement(holder.getFile());
if (PluginModuleType.isPluginModuleOrDependency(module)) {
return true;
}
// always run in tests
return ApplicationManager.getApplication().isUnitTestMode();
}
protected PsiElementVisitor buildInternalVisitor(@NotNull ProblemsHolder holder, boolean isOnTheFly) {
return super.buildVisitor(holder, isOnTheFly);
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2000-2016 JetBrains s.r.o.
* Copyright 2000-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -21,19 +21,23 @@ import com.intellij.codeInspection.ProblemDescriptor;
import com.intellij.codeInspection.ProblemHighlightType;
import com.intellij.openapi.components.ProjectComponent;
import com.intellij.openapi.project.Project;
import com.intellij.psi.*;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiField;
import com.intellij.psi.PsiModifier;
import com.intellij.psi.PsiReference;
import com.intellij.psi.util.InheritanceUtil;
import com.intellij.util.containers.ContainerUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.idea.devkit.util.ExtensionPointLocator;
import org.jetbrains.uast.UClass;
import java.util.List;
public class StatefulEpInspection extends DevKitInspectionBase {
public class StatefulEpInspection extends DevKitUastInspectionBase {
@Nullable
@Override
public ProblemDescriptor[] checkClass(@NotNull PsiClass psiClass, @NotNull InspectionManager manager, boolean isOnTheFly) {
public ProblemDescriptor[] checkClass(@NotNull UClass psiClass, @NotNull InspectionManager manager, boolean isOnTheFly) {
PsiField[] fields = psiClass.getFields();
if (fields.length == 0) return super.checkClass(psiClass, manager, isOnTheFly);