[java-dfa] Properly process boxing of generic parameter in constructor

Fixes IDEA-286477 Primitive type considered null in generic

GitOrigin-RevId: f9e8a5eae5505511d84258b8a1bd67deafe8b8a3
This commit is contained in:
Tagir Valeev
2022-01-13 12:45:34 +00:00
committed by intellij-monorepo-bot
parent 69e6fc61af
commit 0f431159c9
3 changed files with 23 additions and 1 deletions
@@ -2011,6 +2011,10 @@ public class ControlFlowAnalyzer extends JavaElementVisitor {
private @Nullable PsiMethod pushConstructorArguments(PsiConstructorCall call) {
PsiExpressionList args = call.getArgumentList();
PsiMethod ctr = call.resolveConstructor();
PsiSubstitutor substitutor = PsiSubstitutor.EMPTY;
if (call instanceof PsiNewExpression) {
substitutor = call.resolveMethodGenerics().getSubstitutor();
}
if (args != null) {
PsiExpression[] arguments = args.getExpressions();
PsiParameter[] parameters = ctr == null ? null : ctr.getParameterList().getParameters();
@@ -2018,7 +2022,7 @@ public class ControlFlowAnalyzer extends JavaElementVisitor {
PsiExpression argument = arguments[i];
argument.accept(this);
if (parameters != null && i < parameters.length) {
generateBoxingUnboxingInstructionFor(argument, parameters[i].getType());
generateBoxingUnboxingInstructionFor(argument, substitutor.substitute(parameters[i].getType()));
}
}
foldVarArgs(call, parameters);
@@ -0,0 +1,17 @@
class Prop<T> {
private final T defaultValue;
private final Key key;
public Prop(Key key, T defaultValue) {
assert defaultValue != null;
this.key = key;
this.defaultValue = defaultValue;
}
public static void main(String[] args) {
Prop<Integer> prop = new Prop<>(Key.EXAMPLE, 1);
}
}
enum Key {EXAMPLE}
@@ -717,4 +717,5 @@ public class DataFlowInspectionTest extends DataFlowInspectionTestCase {
public void testStringBuilderLengthReturn() { doTest(); }
public void testEqualsTwoFields() { doTest();}
public void testPureMethodReadsMutableArray() { doTest(); }
public void testBoxingInConstructorArguments() { doTest(); }
}