Dataflow to here: better filter presentation

GitOrigin-RevId: f44d19a0c847c7f6c13e438e5f33b8626eaaaab7
This commit is contained in:
Tagir Valeev
2020-05-15 06:47:19 +00:00
committed by intellij-monorepo-bot
parent 7b3ce3097a
commit 27b724aef3
4 changed files with 75 additions and 43 deletions
@@ -281,7 +281,7 @@ public class DataflowExceptionAnalysisProvider implements ExceptionAnalysisProvi
private @Nullable AnAction createAction(@Nullable Analysis analysis) {
if (analysis == null) return null;
String text = getPresentationText(analysis.myDfType, analysis.myAnchor.getType());
String text = JavaDfaSliceValueFilter.getPresentationText(analysis.myDfType, analysis.myAnchor.getType());
if (text.isEmpty()) return null;
return new AnAction(null, JavaBundle.message("action.dfa.from.stacktrace.text", analysis.myAnchor.getText(), text), null) {
@Override
@@ -296,38 +296,6 @@ public class DataflowExceptionAnalysisProvider implements ExceptionAnalysisProvi
};
}
private static String getPresentationText(DfType type, @Nullable PsiType psiType) {
if (type instanceof DfIntegralType) {
// chop 'int' or 'long' prefix
return ((DfIntegralType)type).getRange().getPresentationText(psiType);
}
if (type instanceof DfConstantType) {
return type.toString();
}
if (type instanceof DfReferenceType) {
DfReferenceType stripped = ((DfReferenceType)type).dropNullability();
DfaNullability nullability = ((DfReferenceType)type).getNullability();
TypeConstraint constraint = ((DfReferenceType)type).getConstraint();
if (constraint.getPresentationText(psiType).isEmpty()) {
stripped = stripped.dropTypeConstraint();
}
String constraintText = stripped.toString();
if (nullability == DfaNullability.NOT_NULL) {
if (constraintText.isEmpty()) {
return "not-null";
}
return constraintText + " (not-null)";
}
else if (nullability != DfaNullability.NULL) {
if (constraintText.isEmpty()) {
return "";
}
return "null or " + constraintText;
}
}
return type.toString();
}
private static @Nullable Analysis fromCondition(@Nullable PsiExpression cond) {
cond = PsiUtil.skipParenthesizedExprDown(cond);
if (cond == null) return null;
@@ -3,10 +3,14 @@ package com.intellij.slicer;
import com.intellij.codeInsight.Nullability;
import com.intellij.codeInspection.dataFlow.CommonDataflow;
import com.intellij.codeInspection.dataFlow.DfaNullability;
import com.intellij.codeInspection.dataFlow.TypeConstraint;
import com.intellij.codeInspection.dataFlow.rangeSet.LongRangeSet;
import com.intellij.codeInspection.dataFlow.types.*;
import com.intellij.psi.*;
import com.intellij.psi.util.TypeConversionUtil;
import com.siyeh.InspectionGadgetsBundle;
import com.siyeh.ig.psiutils.JavaPsiMathUtil;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -72,9 +76,66 @@ public class JavaDfaSliceValueFilter implements SliceValueFilter {
@Override
public @NotNull String toString() {
if (myDfType == DfTypes.TOP) {
return InspectionGadgetsBundle.message("none");
}
return myDfType.toString();
}
@Override
public @NotNull @Nls String getPresentationText(@NotNull PsiElement element) {
if (element instanceof PsiLiteralExpression ||
element instanceof PsiExpression && JavaPsiMathUtil.getNumberFromLiteral((PsiExpression)element) != null) {
return "";
}
if (element instanceof PsiNewExpression && ((PsiNewExpression)element).isArrayCreation()) {
return "";
}
return getPresentationText(myDfType, getElementType(element));
}
private @Nullable static PsiType getElementType(@NotNull PsiElement element) {
if (element instanceof PsiExpression) {
return ((PsiExpression)element).getType();
}
if (element instanceof PsiVariable) {
return ((PsiVariable)element).getType();
}
return null;
}
static String getPresentationText(@NotNull DfType type, @Nullable PsiType psiType) {
if (type == DfTypes.TOP) {
return "";
}
if (type instanceof DfIntegralType) {
LongRangeSet psiRange = LongRangeSet.fromType(psiType);
LongRangeSet dfRange = ((DfIntegralType)type).getRange();
if (dfRange.contains(psiRange)) return "";
// chop 'int' or 'long' prefix
return dfRange.getPresentationText(psiType);
}
if (type instanceof DfConstantType) {
return type.toString();
}
if (type instanceof DfReferenceType) {
DfReferenceType stripped = ((DfReferenceType)type).dropNullability();
DfaNullability nullability = ((DfReferenceType)type).getNullability();
TypeConstraint constraint = ((DfReferenceType)type).getConstraint();
if (constraint.getPresentationText(psiType).isEmpty()) {
stripped = stripped.dropTypeConstraint();
}
String constraintText = stripped.toString();
if (nullability == DfaNullability.NOT_NULL) {
if (constraintText.isEmpty()) {
return "not-null";
}
return constraintText + " (not-null)";
}
else if (nullability != DfaNullability.NULL) {
if (constraintText.isEmpty()) {
return "";
}
return "null or " + constraintText;
}
}
return type.toString();
}
}
@@ -91,10 +91,12 @@ class SliceUsageCellRenderer extends SliceUsageCellRendererBase {
}
SliceValueFilter filter = sliceUsage.params.valueFilter;
SliceUsage parent = sliceUsage.getParent();
if (parent == null || !Objects.equals(parent.params.valueFilter, filter)) {
String message = filter == null ?
LangBundle.message("slice.analysis.title.no.filter") :
LangBundle.message("slice.analysis.title.filter", filter);
SliceValueFilter parentFilter = parent == null ? null : parent.params.valueFilter;
String filterText = filter == null ? "" : filter.getPresentationText(sliceUsage.getElement());
String parentFilterText = parentFilter == null || parent.getElement() == null ? "" :
parentFilter.getPresentationText(parent.getElement());
if (!filterText.isEmpty() && !filterText.equals(parentFilterText)) {
String message = LangBundle.message("slice.analysis.title.filter", filterText);
append(" " + message, SimpleTextAttributes.GRAY_ATTRIBUTES);
}
}
@@ -11,9 +11,10 @@ public interface SliceValueFilter {
* @return true if this element passes the filter
*/
boolean allowed(@NotNull PsiElement element);
/**
* @param element to which the filter is applied
* @return String representation
*/
@NotNull @Nls String toString();
@NotNull @Nls String getPresentationText(@NotNull PsiElement element);
}