mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-01-04 17:20:55 +07:00
[java-generation] IDEA-377098 Custom setter generator fails with custom generic type CustomClass<ID, T> in IDE 2025.2
- check types (cherry picked from commit 38de5f89ab0d7b6874e40f213334af73eca3bbb2) IJ-CR-174524 GitOrigin-RevId: 62b19f0a1087edcf8c0980fe3b84f7408119768e
This commit is contained in:
committed by
intellij-monorepo-bot
parent
5ba949ceaa
commit
7b4c1421cb
@@ -841,8 +841,7 @@ public final class GenerateMembersUtil {
|
||||
psiParameter.getTypeElement().replace(typeElementFromText);
|
||||
NullableNotNullManager.getInstance(project).copyNullableOrNotNullAnnotation(field, annotationTarget);
|
||||
}
|
||||
else {
|
||||
PsiMethod psiMethod = (PsiMethod)annotationTarget;
|
||||
else if (annotationTarget instanceof PsiMethod psiMethod) {
|
||||
PsiTypeElement returnTypeElement = psiMethod.getReturnTypeElement();
|
||||
if (returnTypeElement != null && returnTypeElement.getType().getCanonicalText(false).equals(fieldType.getCanonicalText(false))) {
|
||||
PsiTypeElement typeElementFromText = factory.createTypeElementFromText(type, psiMethod);
|
||||
|
||||
@@ -14,19 +14,22 @@ import com.intellij.psi.PsiModifier;
|
||||
import com.intellij.psi.PsiTypes;
|
||||
import com.intellij.psi.codeStyle.JavaCodeStyleSettings;
|
||||
import com.intellij.psi.impl.light.LightFieldBuilder;
|
||||
import com.intellij.testFramework.PlatformTestUtil;
|
||||
import com.intellij.testFramework.ServiceContainerUtil;
|
||||
import com.intellij.testFramework.fixtures.LightJavaCodeInsightFixtureTestCase;
|
||||
import com.intellij.util.NotNullFunction;
|
||||
import com.intellij.util.VisibilityUtil;
|
||||
import com.intellij.util.ui.UIUtil;
|
||||
import com.siyeh.ig.style.UnqualifiedFieldAccessInspection;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.java.generate.template.TemplateResource;
|
||||
import org.jetbrains.java.generate.template.TemplatesManager;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
public class GenerateGetterSetterTest extends LightJavaCodeInsightFixtureTestCase {
|
||||
|
||||
public void testDoNotStripIsOfNonBooleanFields() {
|
||||
myFixture.addClass("class YesNoRAMField {}");
|
||||
myFixture.configureByText("a.java", """
|
||||
@@ -466,7 +469,7 @@ public class GenerateGetterSetterTest extends LightJavaCodeInsightFixtureTestCas
|
||||
return members;
|
||||
}
|
||||
}.invoke(getProject(), myFixture.getEditor(), myFixture.getFile());
|
||||
UIUtil.dispatchAllInvocationEvents();
|
||||
PlatformTestUtil.dispatchAllInvocationEventsInIdeEventQueue();
|
||||
myFixture.checkResult("""
|
||||
class A { \s
|
||||
private String myName;
|
||||
@@ -502,7 +505,7 @@ public class GenerateGetterSetterTest extends LightJavaCodeInsightFixtureTestCas
|
||||
return allAnnotations ? new GetterSetterGenerationOptions(true) : super.getOptions();
|
||||
}
|
||||
}.invoke(getProject(), myFixture.getEditor(), myFixture.getFile());
|
||||
UIUtil.dispatchAllInvocationEvents();
|
||||
PlatformTestUtil.dispatchAllInvocationEventsInIdeEventQueue();
|
||||
}
|
||||
|
||||
public void testStaticOrThisSetterWithSameNameParameter() {
|
||||
@@ -559,6 +562,101 @@ public class GenerateGetterSetterTest extends LightJavaCodeInsightFixtureTestCas
|
||||
doRecordAccessorTest(PsiModifier.PRIVATE);
|
||||
}
|
||||
|
||||
public void testIncorrectParameterMethod() {
|
||||
TemplatesManager setterTemplatesManager = SetterTemplatesManager.getInstance();
|
||||
TemplateResource template = setterTemplatesManager.getDefaultTemplate();
|
||||
Disposer.register(getTestRootDisposable(), () -> {
|
||||
setterTemplatesManager.setDefaultTemplate(template);
|
||||
});
|
||||
|
||||
myFixture.addClass(
|
||||
"""
|
||||
package foo;
|
||||
|
||||
public class CustomClass<ID, T> {
|
||||
public T getValue() {
|
||||
return null;
|
||||
}
|
||||
|
||||
public void setValue(T value) {
|
||||
|
||||
}
|
||||
}
|
||||
""");
|
||||
|
||||
myFixture.configureByText("Sample.java",
|
||||
"""
|
||||
package foo;
|
||||
|
||||
public class Sample<ID, T> {
|
||||
private CustomClass<ID, T> fieldName;
|
||||
|
||||
<caret>
|
||||
}
|
||||
""");
|
||||
|
||||
TemplateResource custom = new TemplateResource("Custom",
|
||||
"""
|
||||
#set($paramName = $helper.getParamName($field, $project))
|
||||
#if($field.modifierStatic)
|
||||
static ##
|
||||
#end
|
||||
#if ($field.type.contains("CustomClass"))
|
||||
#set($returnType = $StringUtil.trimEnd($StringUtil.substringAfter($field.type, ","), ">"))
|
||||
#else
|
||||
#set($returnType = $field.type)
|
||||
#end
|
||||
$class.name $StringUtil.sanitizeJavaIdentifier($helper.getPropertyName($field, $project))(final $returnType $paramName) {
|
||||
#if ($field.name == $paramName)
|
||||
#if (!$field.modifierStatic)
|
||||
this.##
|
||||
#else
|
||||
$classname.##
|
||||
#end
|
||||
#end
|
||||
#if ($field.type.contains("CustomClass"))
|
||||
${field.name}.setValue($paramName)##
|
||||
#else
|
||||
$field.name = $paramName##
|
||||
#end
|
||||
;
|
||||
return this;
|
||||
}
|
||||
""", false);
|
||||
setterTemplatesManager.addTemplate(custom);
|
||||
setterTemplatesManager.setDefaultTemplate(custom);
|
||||
new GenerateSetterHandler() {
|
||||
@Override
|
||||
protected ClassMember[] chooseMembers(ClassMember[] members,
|
||||
boolean allowEmptySelection,
|
||||
boolean copyJavadocCheckbox,
|
||||
Project project,
|
||||
@Nullable Editor editor) {
|
||||
return members;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected @NotNull GetterSetterGenerationOptions getOptions() {
|
||||
return super.getOptions();
|
||||
}
|
||||
}.invoke(getProject(), myFixture.getEditor(), myFixture.getFile());
|
||||
PlatformTestUtil.dispatchAllInvocationEventsInIdeEventQueue();
|
||||
|
||||
myFixture.checkResult(
|
||||
"""
|
||||
package foo;
|
||||
|
||||
public class Sample<ID, T> {
|
||||
private CustomClass<ID, T> fieldName;
|
||||
|
||||
public Sample fieldName(T fieldName) {
|
||||
this.fieldName.setValue(fieldName);
|
||||
return this;
|
||||
}
|
||||
}
|
||||
""");
|
||||
}
|
||||
|
||||
private void doRecordAccessorTest(String visibility) {
|
||||
JavaCodeStyleSettings.getInstance(getProject()).VISIBILITY = visibility;
|
||||
myFixture.configureByText("a.java", """
|
||||
@@ -602,6 +700,6 @@ public class GenerateGetterSetterTest extends LightJavaCodeInsightFixtureTestCas
|
||||
return allAnnotations ? new GetterSetterGenerationOptions(true) : super.getOptions();
|
||||
}
|
||||
}.invoke(getProject(), myFixture.getEditor(), myFixture.getFile());
|
||||
UIUtil.dispatchAllInvocationEvents();
|
||||
PlatformTestUtil.dispatchAllInvocationEventsInIdeEventQueue();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user