[java-refactoring] IDEA-368385 Refactor introduce field added NotNull annotation

GitOrigin-RevId: c695550e832b648d31097d85ee887358551e95ac
This commit is contained in:
Tagir Valeev
2025-03-03 18:40:14 +00:00
committed by intellij-monorepo-bot
parent fe862bbd0d
commit a8ae2217c2
6 changed files with 43 additions and 11 deletions
@@ -67,15 +67,15 @@ public final class CommonJavaRefactoringUtil {
return type;
}
public static PsiType getTypeByExpression(PsiExpression expr, final PsiElementFactory factory) {
public static @Nullable PsiType getTypeByExpression(@Nullable PsiExpression expr, @NotNull final PsiElementFactory factory) {
PsiType type = RefactoringChangeUtil.getTypeByExpression(expr);
if (PsiTypes.nullType().equals(type)) {
ExpectedTypeInfo[] infos = ExpectedTypesProvider.getExpectedTypes(expr, false);
if (infos.length > 0) {
type = infos[0].getType();
if (type instanceof PsiPrimitiveType) {
if (type instanceof PsiPrimitiveType primitiveType) {
type = infos.length > 1 && !(infos[1].getType() instanceof PsiPrimitiveType) ? infos[1].getType()
: ((PsiPrimitiveType)type).getBoxedType(expr);
: primitiveType.getBoxedType(expr);
}
}
else {
@@ -83,7 +83,7 @@ public final class CommonJavaRefactoringUtil {
}
}
return type;
return type == null ? null : PsiTypesUtil.removeExternalAnnotations(type);
}
@Contract("null, _ -> null")
@@ -591,7 +591,7 @@ public final class CommonJavaRefactoringUtil {
}
ExpectedTypeInfo[] expectedTypes = ExpectedTypesProvider.getExpectedTypes(expr, false);
if (expectedTypes.length == 1 || (isFunctionalType || detectConjunct) && expectedTypes.length > 0 ) {
if (typeByExpression != null && Arrays.stream(expectedTypes).anyMatch(typeInfo -> typeByExpression.isAssignableFrom(typeInfo.getType()))) {
if (typeByExpression != null && ContainerUtil.exists(expectedTypes, typeInfo -> typeByExpression.isAssignableFrom(typeInfo.getType()))) {
return type;
}
type = expectedTypes[0].getType();
@@ -9,6 +9,7 @@ import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.psi.util.PsiUtil;
import com.intellij.util.IncorrectOperationException;
import com.intellij.util.JavaPsiConstructorUtil;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -17,8 +18,10 @@ import java.util.Objects;
public final class RefactoringChangeUtil {
private static final Logger LOG = Logger.getInstance(RefactoringChangeUtil.class);
public static PsiType getTypeByExpression(PsiExpression expr) {
PsiType type = expr != null ? expr.getType() : null;
@Contract("null -> null")
public static PsiType getTypeByExpression(@Nullable PsiExpression expr) {
if (expr == null) return null;
PsiType type = expr.getType();
if (type == null) {
if (expr instanceof PsiArrayInitializerExpression) {
PsiExpression[] initializers = ((PsiArrayInitializerExpression)expr).getInitializers();
@@ -0,0 +1,7 @@
import java.util.*;
class X {
void test() {
Set.<caret>of(1, 2);
}
}
@@ -0,0 +1,10 @@
import java.util.*;
class X {
private Set<Integer> integers;
void test() {
integers = Set.of(1, 2);
}
}
@@ -15,11 +15,11 @@
*/
class Test {
private String s;
private String x;
void simpleMethod() {
s = "";
System.out.println(s);
System.out.println(s);
x = "";
System.out.println(x);
System.out.println(x);
}
}
@@ -6,11 +6,19 @@ import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiExpression;
import com.intellij.psi.PsiLocalVariable;
import com.intellij.refactoring.introduceField.IntroduceFieldHandler;
import com.intellij.testFramework.LightProjectDescriptor;
import org.jetbrains.annotations.NotNull;
import static com.intellij.testFramework.fixtures.LightJavaCodeInsightFixtureTestCase.JAVA_21;
public class InplaceIntroduceFieldTest extends AbstractJavaInplaceIntroduceTest {
private static final String BASE_PATH = "/refactoring/inplaceIntroduceField/";
@Override
protected @NotNull LightProjectDescriptor getProjectDescriptor() {
return JAVA_21;
}
public void testAnchor() {
doTest(null);
}
@@ -70,6 +78,10 @@ public class InplaceIntroduceFieldTest extends AbstractJavaInplaceIntroduceTest
public void testStatementsBeforeSuper() {
doTest(introducer -> introducer.setReplaceAllOccurrences(true));
}
public void testNoExternalTypeAnnotations() {
doTest(null);
}
@Override
protected String getBasePath() {