From e88755962baee152571a8db9568af03d675ff4e3 Mon Sep 17 00:00:00 2001 From: Ilya Klyuchnikov Date: Wed, 20 Aug 2014 12:23:34 +0400 Subject: [PATCH] keys to distinguish @NotNull and @Nullable parameters analyses --- .../BytecodeAnalysisConverter.java | 11 ++-- .../bytecodeAnalysis/ClassDataIndexer.java | 6 +- .../bytecodeAnalysis/Combined.java | 4 +- .../codeInspection/bytecodeAnalysis/Data.java | 56 +++---------------- .../bytecodeAnalysis/Parameters.java | 10 ++-- .../ProjectBytecodeAnalysis.java | 2 +- 6 files changed, 27 insertions(+), 62 deletions(-) diff --git a/java/java-analysis-impl/src/com/intellij/codeInspection/bytecodeAnalysis/BytecodeAnalysisConverter.java b/java/java-analysis-impl/src/com/intellij/codeInspection/bytecodeAnalysis/BytecodeAnalysisConverter.java index bc041fb0dc73..c1c9a91b25d0 100644 --- a/java/java-analysis-impl/src/com/intellij/codeInspection/bytecodeAnalysis/BytecodeAnalysisConverter.java +++ b/java/java-analysis-impl/src/com/intellij/codeInspection/bytecodeAnalysis/BytecodeAnalysisConverter.java @@ -277,10 +277,11 @@ public class BytecodeAnalysisConverter { return 0; } else if (dir instanceof In) { In in = (In)dir; - return 8 * in.paramId() + 1; + // nullity mask is 0/1 + return 8 * in.paramId() + 1 + in.nullityMask; } else { InOut inOut = (InOut)dir; - return 8 * inOut.paramId() + 2 + inOut.valueId(); + return 8 * inOut.paramId() + 3 + inOut.valueId(); } } @@ -292,11 +293,11 @@ public class BytecodeAnalysisConverter { else { int paramId = directionKey / 8; int subDirection = directionKey % 8; - if (subDirection == 1) { - return new In(paramId); + if (subDirection <= 2) { + return new In(paramId, subDirection - 1); } else { - return new InOut(paramId, Value.values()[subDirection - 2]); + return new InOut(paramId, Value.values()[subDirection - 3]); } } } diff --git a/java/java-analysis-impl/src/com/intellij/codeInspection/bytecodeAnalysis/ClassDataIndexer.java b/java/java-analysis-impl/src/com/intellij/codeInspection/bytecodeAnalysis/ClassDataIndexer.java index b978ec624360..9af4ec3ac534 100644 --- a/java/java-analysis-impl/src/com/intellij/codeInspection/bytecodeAnalysis/ClassDataIndexer.java +++ b/java/java-analysis-impl/src/com/intellij/codeInspection/bytecodeAnalysis/ClassDataIndexer.java @@ -232,13 +232,13 @@ public class ClassDataIndexer implements DataIndexer notNullParamEquation = new NonNullInAnalysis(richControlFlow, new In(i), stable).analyze(); + Equation notNullParamEquation = new NonNullInAnalysis(richControlFlow, new In(i, In.NOT_NULL), stable).analyze(); notNullParam = notNullParamEquation.rhs.equals(FINAL_NOT_NULL); result.add(notNullParamEquation); } else { // parameter is not leaking, so it is definitely NOT @NotNull - result.add(new Equation(new Key(method, new In(i), stable), FINAL_TOP)); + result.add(new Equation(new Key(method, new In(i, In.NOT_NULL), stable), FINAL_TOP)); } } if (isReferenceArg && isInterestingResult) { @@ -335,7 +335,7 @@ public class ClassDataIndexer implements DataIndexer(new Key(method, new In(i), stable), FINAL_TOP)); + result.add(new Equation(new Key(method, new In(i, In.NOT_NULL), stable), FINAL_TOP)); } if (isReferenceArg && isInterestingResult) { result.add(new Equation(new Key(method, new InOut(i, Value.Null), stable), FINAL_TOP)); diff --git a/java/java-analysis-impl/src/com/intellij/codeInspection/bytecodeAnalysis/Combined.java b/java/java-analysis-impl/src/com/intellij/codeInspection/bytecodeAnalysis/Combined.java index f747c76bdb4d..d20bbcbd3cac 100644 --- a/java/java-analysis-impl/src/com/intellij/codeInspection/bytecodeAnalysis/Combined.java +++ b/java/java-analysis-impl/src/com/intellij/codeInspection/bytecodeAnalysis/Combined.java @@ -106,7 +106,7 @@ final class CombinedSingleAnalysis { } final Equation notNullParamEquation(int i, boolean stable) { - final Key key = new Key(method, new In(i), stable); + final Key key = new Key(method, new In(i, In.NOT_NULL), stable); final Result result; if (interpreter.dereferenced[i]) { result = new Final(Value.NotNull); @@ -367,7 +367,7 @@ final class CombinedInterpreter extends BasicInterpreter { npKeys = new HashSet(); callDerefs[n] = npKeys; } - npKeys.add(new Key(method, new In(i - shift), stable)); + npKeys.add(new Key(method, new In(i - shift, In.NOT_NULL), stable)); } } } diff --git a/java/java-analysis-impl/src/com/intellij/codeInspection/bytecodeAnalysis/Data.java b/java/java-analysis-impl/src/com/intellij/codeInspection/bytecodeAnalysis/Data.java index 29050f480fbe..55a842af684c 100644 --- a/java/java-analysis-impl/src/com/intellij/codeInspection/bytecodeAnalysis/Data.java +++ b/java/java-analysis-impl/src/com/intellij/codeInspection/bytecodeAnalysis/Data.java @@ -52,20 +52,17 @@ enum Value { Bot, NotNull, Null, True, False, Top } -interface Direction { - static final int OUT_DIRECTION = 0; - static final int IN_DIRECTION = 1; - static final int INOUT_DIRECTION = 2; - int directionId(); - int paramId(); - int valueId(); -} +interface Direction {} final class In implements Direction { + static final int NOT_NULL = 0; + static final int NULLABLE = 1; final int paramIndex; + final int nullityMask; - In(int paramIndex) { + In(int paramIndex, int nullityMask) { this.paramIndex = paramIndex; + this.nullityMask = nullityMask; } @Override @@ -79,28 +76,19 @@ final class In implements Direction { if (o == null || getClass() != o.getClass()) return false; In in = (In) o; if (paramIndex != in.paramIndex) return false; + if (nullityMask != in.nullityMask) return false; return true; } @Override public int hashCode() { - return paramIndex; + return 31*paramIndex + nullityMask; } - @Override - public int directionId() { - return IN_DIRECTION; - } - - @Override public int paramId() { return paramIndex; } - @Override - public int valueId() { - return 0; - } } final class InOut implements Direction { @@ -137,17 +125,10 @@ final class InOut implements Direction { return "InOut " + paramIndex + " " + inValue.toString(); } - @Override - public int directionId() { - return INOUT_DIRECTION; - } - - @Override public int paramId() { return paramIndex; } - @Override public int valueId() { return inValue.ordinal(); } @@ -168,21 +149,6 @@ final class Out implements Direction { public boolean equals(Object obj) { return obj instanceof Out; } - - @Override - public int directionId() { - return OUT_DIRECTION; - } - - @Override - public int paramId() { - return 0; - } - - @Override - public int valueId() { - return 0; - } } final class Key { @@ -219,10 +185,6 @@ final class Key { @Override public String toString() { - return "" + method + ' ' + direction + ' ' + stable; + return method + " " + direction + " " + stable; } } - -class LimitReachedException extends RuntimeException { - -} diff --git a/java/java-analysis-impl/src/com/intellij/codeInspection/bytecodeAnalysis/Parameters.java b/java/java-analysis-impl/src/com/intellij/codeInspection/bytecodeAnalysis/Parameters.java index 33fc60bc6107..946070668753 100644 --- a/java/java-analysis-impl/src/com/intellij/codeInspection/bytecodeAnalysis/Parameters.java +++ b/java/java-analysis-impl/src/com/intellij/codeInspection/bytecodeAnalysis/Parameters.java @@ -607,10 +607,12 @@ class NullableInAnalysis extends Analysis { abstract class NullityInterpreter extends BasicInterpreter { boolean top = false; final boolean nullableAnalysis; + final int nullityMask; private PResult subResult = Identity; - NullityInterpreter(boolean nullableAnalysis) { + NullityInterpreter(boolean nullableAnalysis, int nullityMask) { this.nullableAnalysis = nullableAnalysis; + this.nullityMask = nullityMask; } abstract PResult combine(PResult res1, PResult res2) throws AnalyzerException; @@ -731,7 +733,7 @@ abstract class NullityInterpreter extends BasicInterpreter { Method method = new Method(methodNode.owner, methodNode.name, methodNode.desc); for (int i = shift; i < values.size(); i++) { if (values.get(i) instanceof ParamValue) { - subResult = combine(subResult, new ConditionalNPE(new Key(method, new In(i - shift), stable))); + subResult = combine(subResult, new ConditionalNPE(new Key(method, new In(i - shift, nullityMask), stable))); } } break; @@ -744,7 +746,7 @@ abstract class NullityInterpreter extends BasicInterpreter { class NotNullInterpreter extends NullityInterpreter { NotNullInterpreter() { - super(false); + super(false, In.NOT_NULL); } @Override @@ -756,7 +758,7 @@ class NotNullInterpreter extends NullityInterpreter { class NullableInterpreter extends NullityInterpreter { NullableInterpreter() { - super(true); + super(true, In.NULLABLE); } @Override diff --git a/java/java-analysis-impl/src/com/intellij/codeInspection/bytecodeAnalysis/ProjectBytecodeAnalysis.java b/java/java-analysis-impl/src/com/intellij/codeInspection/bytecodeAnalysis/ProjectBytecodeAnalysis.java index ec58fb83431a..03e2d3afff91 100644 --- a/java/java-analysis-impl/src/com/intellij/codeInspection/bytecodeAnalysis/ProjectBytecodeAnalysis.java +++ b/java/java-analysis-impl/src/com/intellij/codeInspection/bytecodeAnalysis/ProjectBytecodeAnalysis.java @@ -162,7 +162,7 @@ public class ProjectBytecodeAnalysis { PsiElement gParent = parent.getParent(); if (gParent instanceof PsiMethod) { final int index = ((PsiParameterList)parent).getParameterIndex((PsiParameter)owner); - return BytecodeAnalysisConverter.psiKey((PsiMethod)gParent, new In(index), md); + return BytecodeAnalysisConverter.psiKey((PsiMethod)gParent, new In(index, In.NOT_NULL), md); } } }