java global inspections: replace usages of uast as psi

This commit is contained in:
Dmitry Batkovich
2018-11-21 16:51:49 +03:00
parent c864e9f95f
commit 41e56ebf20
4 changed files with 25 additions and 30 deletions
@@ -1,14 +1,10 @@
// Copyright 2000-2018 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 com.intellij.codeInspection.reference;
import com.intellij.lang.jvm.JvmClass;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiElement;
import com.intellij.util.ObjectUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.uast.UClass;
import org.jetbrains.uast.UElement;
import java.util.List;
import java.util.Set;
@@ -54,14 +50,14 @@ public interface RefClass extends RefJavaElement {
boolean isLocalClass();
@SuppressWarnings("DeprecatedIsStillUsed")
@SuppressWarnings({"DeprecatedIsStillUsed", "unused"})
@Deprecated
default boolean isSelfInheritor(PsiClass psiClass) {
throw new UnsupportedOperationException();
}
default boolean isSelfInheritor(UClass uClass) {
return isSelfInheritor((PsiClass) uClass);
default boolean isSelfInheritor(@NotNull UClass uClass) {
return isSelfInheritor(uClass.getJavaPsi());
}
@Override
@@ -221,7 +221,7 @@ public class RefClassImpl extends RefJavaElementImpl implements RefClass {
}
@Override
public boolean isSelfInheritor(UClass uClass) {
public boolean isSelfInheritor(@NotNull UClass uClass) {
return isSelfInheritor(uClass, new ArrayList<>());
}
@@ -38,10 +38,7 @@ import gnu.trove.THashSet;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.TestOnly;
import org.jetbrains.uast.UClass;
import org.jetbrains.uast.UDeclaration;
import org.jetbrains.uast.UExpression;
import org.jetbrains.uast.UMethod;
import org.jetbrains.uast.*;
import javax.swing.*;
import java.awt.*;
@@ -254,8 +251,8 @@ public class UnusedDeclarationInspection extends UnusedDeclarationInspectionBase
if (refElement instanceof RefClass) {
UClass uClass = ((RefClass)refElement).getUastElement();
if (uClass != null) {
for (PsiClassInitializer initializer : uClass.getInitializers()) {
findUnusedVariables(initializer.getBody(), refElement, uClass);
for (UClassInitializer initializer : uClass.getInitializers()) {
findUnusedLocalVariables(initializer.getUastBody(), refElement);
}
}
}
@@ -264,24 +261,25 @@ public class UnusedDeclarationInspection extends UnusedDeclarationInspectionBase
if (element instanceof UMethod) {
UExpression body = ((UMethod)element).getUastBody();
if (body != null) {
//TODO resolve
findUnusedVariables((PsiCodeBlock)body.getJavaPsi(), refElement, element);
findUnusedLocalVariables(body, refElement);
}
}
}
}
private void findUnusedVariables(PsiCodeBlock body, RefElement refElement, PsiElement element) {
private void findUnusedLocalVariables(UExpression body, RefElement refElement) {
if (body == null) return;
PsiCodeBlock bodySourcePsi = ObjectUtils.tryCast(body.getSourcePsi(), PsiCodeBlock.class);
if (bodySourcePsi == null) return;
Tools tools = myTools.get(getShortName());
if (tools.isEnabled(element)) {
InspectionToolWrapper toolWrapper = tools.getInspectionTool(element);
if (tools.isEnabled(bodySourcePsi)) {
InspectionToolWrapper toolWrapper = tools.getInspectionTool(bodySourcePsi);
InspectionToolPresentation presentation = myContext.getPresentation(toolWrapper);
if (((UnusedDeclarationInspection)toolWrapper.getTool()).getSharedLocalInspectionTool().LOCAL_VARIABLE) {
List<CommonProblemDescriptor> descriptors = new ArrayList<>();
final Set<PsiVariable> usedVariables = new THashSet<>();
List<DefUseUtil.Info> unusedDefs = DefUseUtil.getUnusedDefs(body, usedVariables);
List<DefUseUtil.Info> unusedDefs = DefUseUtil.getUnusedDefs(bodySourcePsi, usedVariables);
if (unusedDefs != null && !unusedDefs.isEmpty()) {
@@ -297,7 +295,8 @@ public class UnusedDeclarationInspection extends UnusedDeclarationInspectionBase
}
}
body.accept(new JavaRecursiveElementWalkingVisitor() {
bodySourcePsi.accept(new JavaRecursiveElementWalkingVisitor() {
@Override
public void visitClass(PsiClass aClass) { }
@@ -21,11 +21,9 @@ import com.intellij.codeInspection.HTMLJavaHTMLComposer;
import com.intellij.codeInspection.InspectionsBundle;
import com.intellij.codeInspection.reference.*;
import com.intellij.psi.*;
import com.intellij.util.ObjectUtils;
import com.intellij.xml.util.XmlStringUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.uast.UDeclaration;
import org.jetbrains.uast.UField;
import org.jetbrains.uast.UMethod;
@@ -58,7 +56,7 @@ public class HTMLJavaHTMLComposerImpl extends HTMLJavaHTMLComposer {
@Override
public void appendClassExtendsImplements(StringBuffer buf, RefClass refClass) {
if (refClass.getBaseClasses().size() > 0) {
HTMLComposerImpl.appendHeading(buf, InspectionsBundle.message("inspection.export.results.extends.implements"));
HTMLComposer.appendHeading(buf, InspectionsBundle.message("inspection.export.results.extends.implements"));
myComposer.startList(buf);
for (RefClass refBase : refClass.getBaseClasses()) {
myComposer.appendListItem(buf, refBase);
@@ -71,10 +69,10 @@ public class HTMLJavaHTMLComposerImpl extends HTMLJavaHTMLComposer {
public void appendDerivedClasses(StringBuffer buf, RefClass refClass) {
if (refClass.getSubClasses().size() > 0) {
if (refClass.isInterface()) {
HTMLComposerImpl.appendHeading(buf, InspectionsBundle.message("inspection.export.results.extended.implemented"));
HTMLComposer.appendHeading(buf, InspectionsBundle.message("inspection.export.results.extended.implemented"));
}
else {
HTMLComposerImpl.appendHeading(buf, InspectionsBundle.message("inspection.export.results.extended"));
HTMLComposer.appendHeading(buf, InspectionsBundle.message("inspection.export.results.extended"));
}
myComposer.startList(buf);
@@ -88,7 +86,7 @@ public class HTMLJavaHTMLComposerImpl extends HTMLJavaHTMLComposer {
@Override
public void appendLibraryMethods(StringBuffer buf, RefClass refClass) {
if (refClass.getLibraryMethods().size() > 0) {
HTMLComposerImpl.appendHeading(buf, InspectionsBundle.message("inspection.export.results.overrides.library.methods"));
HTMLComposer.appendHeading(buf, InspectionsBundle.message("inspection.export.results.overrides.library.methods"));
myComposer.startList(buf);
for (RefMethod refMethod : refClass.getLibraryMethods()) {
@@ -141,7 +139,7 @@ public class HTMLJavaHTMLComposerImpl extends HTMLJavaHTMLComposer {
public void appendShortName(final RefEntity refElement, final StringBuffer buf) {
if (refElement instanceof RefJavaElement) {
String modifier = ((RefJavaElement)refElement).getAccessModifier();
if (modifier != null && modifier != PsiModifier.PACKAGE_LOCAL) {
if (modifier != PsiModifier.PACKAGE_LOCAL) {
buf.append(modifier);
buf.append(HTMLComposerImpl.NBSP);
}
@@ -330,8 +328,10 @@ public class HTMLJavaHTMLComposerImpl extends HTMLJavaHTMLComposer {
buf.append(HTMLComposerImpl.A_CLOSING);
if (refElement instanceof RefMethod) {
UMethod psiMethod = (UMethod)((RefMethod)refElement).getUastElement();
appendMethodParameters(buf, psiMethod, false);
PsiMethod psiMethod = (PsiMethod)((RefMethod)refElement).getUastElement().getJavaPsi();
if (psiMethod != null) {
appendMethodParameters(buf, psiMethod, false);
}
}
buf.append(HTMLComposerImpl.CODE_CLOSING);