[java] Preserve type annotations when generating getters and setters

Fixes IDEA-196894 Generating accessors should insert nullability annotations automatically when corresponding fields have them

GitOrigin-RevId: a24d1288e4b3c82f0baa6216b6d84e9b5da29b76
This commit is contained in:
Tagir Valeev
2024-02-23 10:34:48 +01:00
committed by intellij-monorepo-bot
parent db04ce1b62
commit 8aff1fed71
2 changed files with 40 additions and 2 deletions

View File

@@ -167,7 +167,9 @@ public class GenerateGetterSetterTest extends LightJavaCodeInsightFixtureTestCas
}
public void testNullableStuff() {
myFixture.addClass("package org.jetbrains.annotations;\n" + "public @interface NotNull {}");
myFixture.addClass("""
package org.jetbrains.annotations;
public @interface NotNull {}""");
myFixture.configureByText("a.java", """
class Foo {
@org.jetbrains.annotations.NotNull
@@ -197,6 +199,42 @@ public class GenerateGetterSetterTest extends LightJavaCodeInsightFixtureTestCas
""");
}
public void testNullableStuffTypeUse() {
myFixture.addClass("""
package org.jetbrains.annotations;
import java.lang.annotation.ElementType;
import java.lang.annotation.Target;
@Target(ElementType.TYPE_USE)
public @interface NotNull {}""");
myFixture.configureByText("a.java", """
class Foo {
@org.jetbrains.annotations.NotNull
private String myName;
<caret>
}
""");
generateGetter();
generateSetter();
myFixture.checkResult("""
import org.jetbrains.annotations.NotNull;
class Foo {
@org.jetbrains.annotations.NotNull
private String myName;
public void setMyName(@NotNull String myName) {
this.myName = myName;
}
public @NotNull String getMyName() {
return myName;
}
}
""");
}
public void testLombokGeneratedFieldsWithoutContainingFile() {
ServiceContainerUtil.registerExtension(ApplicationManager.getApplication(), GenerateAccessorProviderRegistrar.EP_NAME,
new NotNullFunction<PsiClass, Collection<EncapsulatableClassMember>>() {

View File

@@ -167,7 +167,7 @@ public final class ElementFactory {
// type names
element.setTypeName(PsiAdapter.getTypeClassName(type));
element.setTypeQualifiedName(PsiAdapter.getTypeQualifiedClassName(type));
element.setType(type.getCanonicalText());
element.setType(type.getCanonicalText(true));
// arrays, collections and maps types
if (PsiAdapter.isObjectArrayType(type)) {