dfa should box primitive values coerced into Object (IDEA-111445)

This commit is contained in:
peter
2013-08-02 15:55:03 +02:00
parent fbfe7021de
commit ee51eccbf3
4 changed files with 22 additions and 1 deletions
@@ -1074,7 +1074,7 @@ class ControlFlowAnalyzer extends JavaElementVisitor {
if (TypeConversionUtil.isPrimitiveAndNotNull(expectedType) && TypeConversionUtil.isPrimitiveWrapper(exprType)) {
addInstruction(new MethodCallInstruction(expression, MethodCallInstruction.MethodType.UNBOXING, expectedType));
}
else if (TypeConversionUtil.isPrimitiveWrapper(expectedType) && TypeConversionUtil.isPrimitiveAndNotNull(exprType)) {
else if (TypeConversionUtil.isAssignableFromPrimitiveWrapper(expectedType) && TypeConversionUtil.isPrimitiveAndNotNull(exprType)) {
addInstruction(new MethodCallInstruction(expression, MethodCallInstruction.MethodType.BOXING, expectedType));
}
else if (exprType != expectedType &&
@@ -29,6 +29,7 @@ import com.intellij.util.containers.HashMap;
import gnu.trove.THashMap;
import gnu.trove.THashSet;
import gnu.trove.TObjectIntHashMap;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -1202,10 +1203,20 @@ public class TypeConversionUtil {
public static boolean isPrimitiveWrapper(String typeName) {
return PRIMITIVE_WRAPPER_TYPES.contains(typeName);
}
@Contract("null -> false")
public static boolean isAssignableFromPrimitiveWrapper(final PsiType type) {
if (type == null) return false;
return isPrimitiveWrapper(type) ||
type.equalsToText(CommonClassNames.JAVA_LANG_OBJECT) ||
type.equalsToText(CommonClassNames.JAVA_LANG_NUMBER);
}
@Contract("null -> false")
public static boolean isPrimitiveWrapper(final PsiType type) {
return type != null && isPrimitiveWrapper(type.getCanonicalText());
}
@Contract("null -> false")
public static boolean isComposite(final PsiType type) {
return type instanceof PsiDisjunctionType || type instanceof PsiIntersectionType;
}
@@ -1256,10 +1267,12 @@ public class TypeConversionUtil {
return PsiType.getJavaLangObject(typeParameter.getManager(), typeParameter.getResolveScope());
}
@Contract("null -> null")
public static PsiType erasure(@Nullable PsiType type) {
return erasure(type, PsiSubstitutor.EMPTY);
}
@Contract("null, _ -> null")
public static PsiType erasure(@Nullable final PsiType type, @NotNull final PsiSubstitutor beforeSubstitutor) {
if (type == null) return null;
return type.accept(new PsiTypeVisitor<PsiType>() {
@@ -0,0 +1,7 @@
class X {
public static void main(String[] args) {
int primitive = 1024;
Object object = primitive;
System.out.println(primitive == object);
}
}
@@ -279,4 +279,5 @@ public class DataFlowInspectionTest extends LightCodeInsightFixtureTestCase {
public void testContractAnnotation() { doTest(); }
public void testBoxingImpliesNotNull() { doTest(); }
public void testLargeIntegersAreNotEqualWhenBoxed() { doTest(); }
}