diff --git a/java/java-analysis-impl/src/com/intellij/codeInspection/bytecodeAnalysis/Analysis.java b/java/java-analysis-impl/src/com/intellij/codeInspection/bytecodeAnalysis/Analysis.java index 01993d5e9dd0..61d018f2f5ae 100644 --- a/java/java-analysis-impl/src/com/intellij/codeInspection/bytecodeAnalysis/Analysis.java +++ b/java/java-analysis-impl/src/com/intellij/codeInspection/bytecodeAnalysis/Analysis.java @@ -28,6 +28,8 @@ import org.jetbrains.org.objectweb.asm.tree.analysis.Frame; import java.util.*; +import static com.intellij.codeInspection.bytecodeAnalysis.Direction.*; + class AbstractValues { static final class ParamValue extends BasicValue { ParamValue(Type tp) { @@ -274,10 +276,8 @@ abstract class Analysis { } for (int i = 0; i < args.length; i++) { BasicValue value; - if (direction instanceof InOut && ((InOut)direction).paramIndex == i) { - value = new AbstractValues.ParamValue(args[i]); - } - else if (direction instanceof In && ((In)direction).paramIndex == i) { + if (direction instanceof InOut && ((InOut)direction).paramIndex == i || + direction instanceof In && ((In)direction).paramIndex == i) { value = new AbstractValues.ParamValue(args[i]); } else { 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 84a5851e847d..068ee5c06acf 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 @@ -28,6 +28,7 @@ import java.security.NoSuchAlgorithmException; import java.util.*; import static com.intellij.codeInspection.bytecodeAnalysis.ProjectBytecodeAnalysis.LOG; +import static com.intellij.codeInspection.bytecodeAnalysis.Direction.*; /** * @author lambdamix @@ -272,8 +273,9 @@ public class BytecodeAnalysisConverter { return null; } + private static int mkDirectionKey(Direction dir) { - if (dir instanceof Out) { + if (dir == Out) { return 0; } else if (dir instanceof In) { In in = (In)dir; @@ -288,7 +290,7 @@ public class BytecodeAnalysisConverter { @NotNull private static Direction extractDirection(int directionKey) { if (directionKey == 0) { - return new Out(); + return Out; } else { int paramId = directionKey / 8; @@ -344,7 +346,7 @@ public class BytecodeAnalysisConverter { continue; } Direction direction = extractDirection(key.dirKey); - if (value == Value.NotNull && direction instanceof Out && methodKey.equals(key)) { + if (value == Value.NotNull && direction == Out && methodKey.equals(key)) { notNulls.add(key); } else if (direction instanceof InOut) { 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 316307896467..a6f64da6194d 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 @@ -32,6 +32,7 @@ import java.security.MessageDigest; import java.util.*; import static com.intellij.codeInspection.bytecodeAnalysis.ProjectBytecodeAnalysis.LOG; +import static com.intellij.codeInspection.bytecodeAnalysis.Direction.*; /** * @author lambdamix @@ -125,7 +126,7 @@ public class ClassDataIndexer implements DataIndexer".equals(methodNode.name); - Key primaryKey = new Key(method, new Out(), stable); + Key primaryKey = new Key(method, Out, stable); if (argumentTypes.length == 0 && !isInterestingResult) { return Pair.create(primaryKey, EMPTY_EQUATIONS); } @@ -215,12 +216,12 @@ public class ClassDataIndexer implements DataIndexer compute() { if (origins.getValue() != null) { try { - return new InOutAnalysis(richControlFlow, new Out(), origins.getValue(), stable).analyze(); + return new InOutAnalysis(richControlFlow, Out, origins.getValue(), stable).analyze(); } catch (AnalyzerException ignored) { } } - return new Equation(new Key(method, new Out(), stable), FINAL_TOP); + return new Equation(new Key(method, Out, stable), FINAL_TOP); } }; @@ -344,7 +345,7 @@ public class ClassDataIndexer implements DataIndexer> result = new ArrayList>(argumentTypes.length * 3 + 1); if (isReferenceResult) { - result.add(new Equation(new Key(method, new Out(), stable), FINAL_TOP)); + result.add(new Equation(new Key(method, Out, stable), FINAL_TOP)); } for (int i = 0; i < argumentTypes.length; i++) { Type argType = argumentTypes[i]; 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 645c00260af1..10763d0c798f 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 @@ -34,6 +34,7 @@ import java.util.Set; import static com.intellij.codeInspection.bytecodeAnalysis.AbstractValues.*; import static org.jetbrains.org.objectweb.asm.Opcodes.*; +import static com.intellij.codeInspection.bytecodeAnalysis.Direction.*; final class ParamKey { final Method method; @@ -219,7 +220,7 @@ final class CombinedSingleAnalysis { } } if (ASMUtils.isReferenceType(call.getType())) { - keys.add(new Key(call.method, new Out(), call.stableCall)); + keys.add(new Key(call.method, Out, call.stableCall)); } if (keys.isEmpty()) { result = new Final(Value.Top); @@ -234,7 +235,7 @@ final class CombinedSingleAnalysis { } final Equation outContractEquation(boolean stable) { - final Key key = new Key(method, new Out(), stable); + final Key key = new Key(method, Out, stable); final Result result; if (exception) { result = new Final(Value.Bot); @@ -253,7 +254,7 @@ final class CombinedSingleAnalysis { } else if (returnValue instanceof CombinedCall) { CombinedCall call = (CombinedCall)returnValue; - Key callKey = new Key(call.method, new Out(), call.stableCall); + Key callKey = new Key(call.method, Out, call.stableCall); Set keys = new SingletonSet(callKey); result = new Pending(new SingletonSet>(new Product(Value.Top, keys))); } diff --git a/java/java-analysis-impl/src/com/intellij/codeInspection/bytecodeAnalysis/Contracts.java b/java/java-analysis-impl/src/com/intellij/codeInspection/bytecodeAnalysis/Contracts.java index c382148abb05..c9b230e2d719 100644 --- a/java/java-analysis-impl/src/com/intellij/codeInspection/bytecodeAnalysis/Contracts.java +++ b/java/java-analysis-impl/src/com/intellij/codeInspection/bytecodeAnalysis/Contracts.java @@ -34,6 +34,7 @@ import java.util.Set; import static com.intellij.codeInspection.bytecodeAnalysis.AbstractValues.*; import static org.jetbrains.org.objectweb.asm.Opcodes.*; +import static com.intellij.codeInspection.bytecodeAnalysis.Direction.*; class InOutAnalysis extends Analysis> { @@ -436,7 +437,7 @@ class InOutInterpreter extends BasicInterpreter { } } if (isRefRetType) { - keys.add(new Key(method, new Out(), stable)); + keys.add(new Key(method, Out, stable)); } if (!keys.isEmpty()) { return new CallResultValue(retType, keys); @@ -444,7 +445,7 @@ class InOutInterpreter extends BasicInterpreter { } else if (isRefRetType) { HashSet keys = new HashSet(); - keys.add(new Key(method, new Out(), stable)); + keys.add(new Key(method, Out, stable)); return new CallResultValue(retType, keys); } } 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 55a842af684c..74e805a31492 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,103 +52,97 @@ enum Value { Bot, NotNull, Null, True, False, Top } -interface Direction {} +interface Direction { + final class In implements Direction { + static final int NOT_NULL = 0; + static final int NULLABLE = 1; + final int paramIndex; + final int nullityMask; -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, int nullityMask) { + this.paramIndex = paramIndex; + this.nullityMask = nullityMask; + } - In(int paramIndex, int nullityMask) { - this.paramIndex = paramIndex; - this.nullityMask = nullityMask; + @Override + public String toString() { + return "In " + paramIndex; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + 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 31 * paramIndex + nullityMask; + } + + public int paramId() { + return paramIndex; + } } - @Override - public String toString() { - return "In " + paramIndex; + final class InOut implements Direction { + final int paramIndex; + final Value inValue; + + InOut(int paramIndex, Value inValue) { + this.paramIndex = paramIndex; + this.inValue = inValue; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + InOut inOut = (InOut)o; + + if (paramIndex != inOut.paramIndex) return false; + if (inValue != inOut.inValue) return false; + + return true; + } + + @Override + public int hashCode() { + int result = paramIndex; + result = 31 * result + inValue.ordinal(); + return result; + } + + @Override + public String toString() { + return "InOut " + paramIndex + " " + inValue.toString(); + } + + public int paramId() { + return paramIndex; + } + + public int valueId() { + return inValue.ordinal(); + } } - @Override - public boolean equals(Object o) { - if (this == o) return true; - 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; - } + Direction Out = new Direction() { + @Override + public String toString() { + return "Out"; + } - @Override - public int hashCode() { - return 31*paramIndex + nullityMask; - } - - public int paramId() { - return paramIndex; - } - -} - -final class InOut implements Direction { - final int paramIndex; - final Value inValue; - - InOut(int paramIndex, Value inValue) { - this.paramIndex = paramIndex; - this.inValue = inValue; - } - - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - - InOut inOut = (InOut) o; - - if (paramIndex != inOut.paramIndex) return false; - if (inValue != inOut.inValue) return false; - - return true; - } - - @Override - public int hashCode() { - int result = paramIndex; - result = 31 * result + inValue.ordinal(); - return result; - } - - @Override - public String toString() { - return "InOut " + paramIndex + " " + inValue.toString(); - } - - public int paramId() { - return paramIndex; - } - - public int valueId() { - return inValue.ordinal(); - } -} - -final class Out implements Direction { - @Override - public String toString() { - return "Out"; - } - - @Override - public int hashCode() { - return 1; - } - - @Override - public boolean equals(Object obj) { - return obj instanceof Out; - } + @Override + public int hashCode() { + return 1; + } + }; } final class Key { 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 a7c25782d48b..69f6384bc31d 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 @@ -37,6 +37,7 @@ import static com.intellij.codeInspection.bytecodeAnalysis.AbstractValues.Instan import static com.intellij.codeInspection.bytecodeAnalysis.AbstractValues.ParamValue; import static com.intellij.codeInspection.bytecodeAnalysis.PResults.*; import static org.jetbrains.org.objectweb.asm.Opcodes.*; +import static com.intellij.codeInspection.bytecodeAnalysis.Direction.*; abstract class PResults { // SoP = sum of products 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 aa44951961ad..cafc57d0b373 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 @@ -39,6 +39,8 @@ import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.*; +import static com.intellij.codeInspection.bytecodeAnalysis.Direction.*; + /** * @author lambdamix */ @@ -172,7 +174,7 @@ public class ProjectBytecodeAnalysis { public static HKey getKey(@NotNull PsiModifierListOwner owner, MessageDigest md) { LOG.assertTrue(owner instanceof PsiCompiledElement, owner); if (owner instanceof PsiMethod) { - return BytecodeAnalysisConverter.psiKey((PsiMethod)owner, new Out(), md); + return BytecodeAnalysisConverter.psiKey((PsiMethod)owner, Out, md); } if (owner instanceof PsiParameter) { PsiElement parent = owner.getParent(); diff --git a/java/java-tests/testSrc/com/intellij/codeInspection/bytecodeAnalysis/BytecodeAnalysisTest.java b/java/java-tests/testSrc/com/intellij/codeInspection/bytecodeAnalysis/BytecodeAnalysisTest.java index 2cbf3fbdfd6c..abae78e2a8e5 100644 --- a/java/java-tests/testSrc/com/intellij/codeInspection/bytecodeAnalysis/BytecodeAnalysisTest.java +++ b/java/java-tests/testSrc/com/intellij/codeInspection/bytecodeAnalysis/BytecodeAnalysisTest.java @@ -184,14 +184,13 @@ public class BytecodeAnalysisTest extends JavaCodeInsightFixtureTestCase { } private void checkCompoundId(Method method, PsiMethod psiMethod, boolean noKey) throws IOException { - Direction direction = new Out(); System.out.println(); System.out.println(method.internalClassName); System.out.println(method.methodName); System.out.println(method.methodDesc); - HKey psiKey = BytecodeAnalysisConverter.psiKey(psiMethod, direction, myMessageDigest); + HKey psiKey = BytecodeAnalysisConverter.psiKey(psiMethod, Direction.Out, myMessageDigest); if (noKey) { assertTrue(null == psiKey); return; @@ -199,7 +198,7 @@ public class BytecodeAnalysisTest extends JavaCodeInsightFixtureTestCase { else { assertFalse(null == psiKey); } - HKey asmKey = BytecodeAnalysisConverter.asmKey(new Key(method, direction, true), myMessageDigest); + HKey asmKey = BytecodeAnalysisConverter.asmKey(new Key(method, Direction.Out, true), myMessageDigest); Assert.assertEquals(asmKey, psiKey); }