diff --git a/java/java-impl/src/com/intellij/slicer/DataflowExceptionAnalysisProvider.java b/java/java-impl/src/com/intellij/slicer/DataflowExceptionAnalysisProvider.java index 0ef2b1da35d6..dab40adfa16d 100644 --- a/java/java-impl/src/com/intellij/slicer/DataflowExceptionAnalysisProvider.java +++ b/java/java-impl/src/com/intellij/slicer/DataflowExceptionAnalysisProvider.java @@ -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; diff --git a/java/java-impl/src/com/intellij/slicer/JavaDfaSliceValueFilter.java b/java/java-impl/src/com/intellij/slicer/JavaDfaSliceValueFilter.java index 566593d14810..20a931c7cf1c 100644 --- a/java/java-impl/src/com/intellij/slicer/JavaDfaSliceValueFilter.java +++ b/java/java-impl/src/com/intellij/slicer/JavaDfaSliceValueFilter.java @@ -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(); + } } diff --git a/java/java-impl/src/com/intellij/slicer/SliceUsageCellRenderer.java b/java/java-impl/src/com/intellij/slicer/SliceUsageCellRenderer.java index 08531794fbe2..ed25274ffead 100644 --- a/java/java-impl/src/com/intellij/slicer/SliceUsageCellRenderer.java +++ b/java/java-impl/src/com/intellij/slicer/SliceUsageCellRenderer.java @@ -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); } } diff --git a/platform/lang-impl/src/com/intellij/slicer/SliceValueFilter.java b/platform/lang-impl/src/com/intellij/slicer/SliceValueFilter.java index 6f4d80408f74..e19cfa029f95 100644 --- a/platform/lang-impl/src/com/intellij/slicer/SliceValueFilter.java +++ b/platform/lang-impl/src/com/intellij/slicer/SliceValueFilter.java @@ -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); }