Skip to content

Commit

Permalink
Java Type Migration: avoid NPE when migrating enum type to something …
Browse files Browse the repository at this point in the history
…else (IDEA-225806)

GitOrigin-RevId: d71d68d9395c93d6346a247c7f207ae840e2fbad
  • Loading branch information
BasLeijdekkers authored and intellij-monorepo-bot committed May 2, 2023
1 parent b856d0e commit 14390e6
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ public void change(@NotNull TypeMigrationUsageInfo usageInfo, @NotNull Consumer<
else if ((element instanceof PsiField || element instanceof PsiLocalVariable) &&
isMultiVariableDeclaration((PsiVariable)element)) {
final PsiTypeElement typeElement = ((PsiVariable)element).getTypeElement();
myVariableMigration.putValue(typeElement, usageInfo);
if (typeElement != null) myVariableMigration.putValue(typeElement, usageInfo);
}
else {
TypeMigrationReplacementUtil.migrateMemberOrVariableType(element, project, getTypeEvaluator().getType(usageInfo));
Expand Down Expand Up @@ -581,7 +581,7 @@ boolean addMigrationRoot(PsiElement element,
return false;
}
final PsiElement resolved = Util.normalizeElement(element);
if (!canBeRoot(resolved, myRules.getSearchScope())) {
if (!canBeRoot(resolved, myRules.getSearchScope()) || resolved instanceof PsiEnumConstant) {
return false;
}
final PsiType originalType = getElementType(resolved);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ public void testEnumConstant() {
doTestFirstParamType("Test", PsiTypes.byteType());
}

public void testMigrateEnumType() {
doTestFieldType("someEnum", PsiTypes.intType());
}

public void testVarargsAndBoxing() {
doTestFieldType("x", PsiTypes.longType());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Types:
PsiField:someEnum : int

Conversions:

New expression type changes:
Fails:
SomeEnum.ENUM_VALUE_1->int
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class Test {
private int someEnum = SomeEnum.ENUM_VALUE_1;
public enum SomeEnum {
ENUM_VALUE_1, ENUM_VALUE_2
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class Test {
private SomeEnum someEnum = SomeEnum.ENUM_VALUE_1;
public enum SomeEnum {
ENUM_VALUE_1, ENUM_VALUE_2
}
}

0 comments on commit 14390e6

Please sign in to comment.