From 89b362ef965f775cdd05f07017bef39bb9e68d7c Mon Sep 17 00:00:00 2001 From: Alexey Kudravtsev Date: Thu, 12 Nov 2015 15:27:15 +0300 Subject: [PATCH] IDEA-146946 --- .../codeInspection/dataFlow/DfaUtil.java | 8 ++--- .../dataFlow/ValuableDataFlowRunner.java | 34 +++++++++---------- .../slice/backward/TryCatchFinally.java | 17 ++++++++++ .../intellij/slicer/SliceBackwardTest.java | 16 +++------ 4 files changed, 42 insertions(+), 33 deletions(-) create mode 100644 java/java-tests/testData/codeInsight/slice/backward/TryCatchFinally.java diff --git a/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/DfaUtil.java b/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/DfaUtil.java index ae54ff7817cb..fa6208115eb0 100644 --- a/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/DfaUtil.java +++ b/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/DfaUtil.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2009 JetBrains s.r.o. + * Copyright 2000-2015 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. @@ -70,7 +70,7 @@ public class DfaUtil { @Override public Result> compute() { final ValuableInstructionVisitor visitor = new ValuableInstructionVisitor(); - RunnerResult runnerResult = new ValuableDataFlowRunner(codeBlock).analyzeMethod(codeBlock, visitor); + RunnerResult runnerResult = new ValuableDataFlowRunner().analyzeMethod(codeBlock, visitor); return Result.create(runnerResult == RunnerResult.OK ? visitor.myResults : null, codeBlock); } }); @@ -242,11 +242,11 @@ public class DfaUtil { final ValuableDataFlowRunner.ValuableDfaVariableState curState = (ValuableDataFlowRunner.ValuableDfaVariableState)memState.getVariableState(var); final FList curValue = curState.myConcatenation; final FList nextValue; - if (type == JavaTokenType.PLUSEQ && !prevValue.isEmpty()) { + if (type == JavaTokenType.PLUSEQ && !prevValue.isEmpty() && rightValue != null) { nextValue = prevValue.prepend(rightValue); } else { - nextValue = curValue.isEmpty() ? curValue.prepend(rightValue) : curValue; + nextValue = curValue.isEmpty() && rightValue != null ? curValue.prepend(rightValue) : curValue; } memState.setVariableState(var, curState.withExpression(nextValue)); } diff --git a/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/ValuableDataFlowRunner.java b/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/ValuableDataFlowRunner.java index bf78faf17b66..51daae3aa476 100644 --- a/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/ValuableDataFlowRunner.java +++ b/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/ValuableDataFlowRunner.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2009 JetBrains s.r.o. + * Copyright 2000-2015 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. @@ -20,7 +20,6 @@ import com.intellij.codeInspection.dataFlow.value.DfaPsiType; import com.intellij.codeInspection.dataFlow.value.DfaValue; import com.intellij.codeInspection.dataFlow.value.DfaValueFactory; import com.intellij.codeInspection.dataFlow.value.DfaVariableValue; -import com.intellij.psi.PsiElement; import com.intellij.psi.PsiExpression; import com.intellij.util.containers.FList; import org.jetbrains.annotations.NotNull; @@ -31,33 +30,31 @@ import java.util.Set; /** * @author Gregory.Shrago */ -public class ValuableDataFlowRunner extends DataFlowRunner { - - protected ValuableDataFlowRunner(PsiElement block) { - super(); - } - +class ValuableDataFlowRunner extends DataFlowRunner { + @NotNull @Override protected DfaMemoryState createMemoryState() { return new MyDfaMemoryState(getFactory()); } static class MyDfaMemoryState extends DfaMemoryStateImpl { - private MyDfaMemoryState(final DfaValueFactory factory) { + private MyDfaMemoryState(@NotNull DfaValueFactory factory) { super(factory); } - MyDfaMemoryState(DfaMemoryStateImpl toCopy) { + private MyDfaMemoryState(@NotNull DfaMemoryStateImpl toCopy) { super(toCopy); } + @NotNull @Override public DfaMemoryStateImpl createCopy() { return new MyDfaMemoryState(this); } + @NotNull @Override - protected DfaVariableState createVariableState(DfaVariableValue var) { + protected DfaVariableState createVariableState(@NotNull DfaVariableValue var) { return new ValuableDfaVariableState(var); } @@ -68,35 +65,38 @@ public class ValuableDataFlowRunner extends DataFlowRunner { } static class ValuableDfaVariableState extends DfaVariableState { - final DfaValue myValue; + private final DfaValue myValue; @NotNull final FList myConcatenation; - private ValuableDfaVariableState(final DfaVariableValue psiVariable) { + private ValuableDfaVariableState(@NotNull DfaVariableValue psiVariable) { super(psiVariable); myValue = null; myConcatenation = FList.emptyList(); } private ValuableDfaVariableState(Set instanceofValues, - Set notInstanceofValues, - Nullness nullability, DfaValue value, @NotNull FList concatenation) { + Set notInstanceofValues, + Nullness nullability, DfaValue value, + @NotNull FList concatenation) { super(instanceofValues, notInstanceofValues, nullability); myValue = value; myConcatenation = concatenation; } + @NotNull @Override - protected DfaVariableState createCopy(Set instanceofValues, Set notInstanceofValues, Nullness nullability) { + protected DfaVariableState createCopy(@NotNull Set instanceofValues, @NotNull Set notInstanceofValues, @NotNull Nullness nullability) { return new ValuableDfaVariableState(instanceofValues, notInstanceofValues, nullability, myValue, myConcatenation); } + @NotNull @Override public DfaVariableState withValue(@Nullable final DfaValue value) { if (value == myValue) return this; return new ValuableDfaVariableState(myInstanceofValues, myNotInstanceofValues, myNullability, value, myConcatenation); } - public ValuableDfaVariableState withExpression(@NotNull final FList concatenation) { + ValuableDfaVariableState withExpression(@NotNull final FList concatenation) { if (concatenation == myConcatenation) return this; return new ValuableDfaVariableState(myInstanceofValues, myNotInstanceofValues, myNullability, myValue, concatenation); } diff --git a/java/java-tests/testData/codeInsight/slice/backward/TryCatchFinally.java b/java/java-tests/testData/codeInsight/slice/backward/TryCatchFinally.java new file mode 100644 index 000000000000..df38d98395f4 --- /dev/null +++ b/java/java-tests/testData/codeInsight/slice/backward/TryCatchFinally.java @@ -0,0 +1,17 @@ +package x; + +class X { + void f(Throwable p) { + Throwable error = null; + + try { + f(p); + } + catch (Throwable e) { + error = e; + } + finally { + f(error); + } + } +} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/slicer/SliceBackwardTest.java b/java/java-tests/testSrc/com/intellij/slicer/SliceBackwardTest.java index 8d8638dc02d4..edfa73b4a5ce 100644 --- a/java/java-tests/testSrc/com/intellij/slicer/SliceBackwardTest.java +++ b/java/java-tests/testSrc/com/intellij/slicer/SliceBackwardTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2014 JetBrains s.r.o. + * Copyright 2000-2015 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. @@ -17,23 +17,14 @@ package com.intellij.slicer; import com.intellij.analysis.AnalysisScope; import com.intellij.codeInsight.daemon.impl.HighlightInfo; -import com.intellij.openapi.command.WriteCommandAction; -import com.intellij.openapi.editor.Document; -import com.intellij.openapi.editor.Editor; -import com.intellij.openapi.editor.LogicalPosition; import com.intellij.openapi.editor.RangeMarker; -import com.intellij.openapi.fileEditor.FileEditorManager; -import com.intellij.openapi.progress.ProgressManager; -import com.intellij.openapi.util.text.StringUtil; import com.intellij.psi.PsiDocumentManager; import com.intellij.psi.PsiElement; -import com.intellij.psi.PsiFile; -import com.intellij.util.CommonProcessors; import com.intellij.util.containers.IntArrayList; -import gnu.trove.THashMap; import gnu.trove.TIntObjectHashMap; -import java.util.*; +import java.util.Collection; +import java.util.Map; /** * @author cdr @@ -89,4 +80,5 @@ public class SliceBackwardTest extends SliceTestCase { public void testVarArgsPartial() throws Exception { doTest();} public void testListTrackToArray() throws Exception { doTest();} + public void testTryCatchFinally() throws Exception { doTest();} }