IG: don't lose type annotations when replacing clone return type

GitOrigin-RevId: 4ed18180ecbea99dfeff8a541c7a0b30c794054d
This commit is contained in:
Bas Leijdekkers
2021-04-14 18:44:31 +03:00
committed by intellij-monorepo-bot
parent edfb6b07f7
commit 3ddf2a2a3a
4 changed files with 32 additions and 2 deletions
@@ -65,7 +65,8 @@ public class CloneReturnsClassTypeInspection extends BaseInspection {
if (!(element instanceof PsiTypeElement)) {
return;
}
final PsiElement parent = element.getParent();
final PsiTypeElement typeElement = (PsiTypeElement)element;
final PsiElement parent = typeElement.getParent();
if (!(parent instanceof PsiMethod)) {
return;
}
@@ -96,7 +97,7 @@ public class CloneReturnsClassTypeInspection extends BaseInspection {
commentTracker);
}
});
element.replace(newTypeElement);
typeElement.getFirstChild().replace(newTypeElement.getFirstChild());
}
}
@@ -0,0 +1,14 @@
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
class TypeUseAnnotation implements Cloneable {
public @NonNull TypeUseAnnotation clone() throws CloneNotSupportedException {
return (TypeUseAnnotation) super.clone();
}
}
@Retention(RetentionPolicy.CLASS)
@Target({ElementType.TYPE_USE})
@interface NonNull {}
@@ -0,0 +1,14 @@
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
class TypeUseAnnotation implements Cloneable {
public @NonNull <caret>Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
@Retention(RetentionPolicy.CLASS)
@Target({ElementType.TYPE_USE})
@interface NonNull {}
@@ -18,6 +18,7 @@ public class CloneReturnsClassTypeFixTest extends IGQuickFixesTestCase {
public void testSimple() { doTest("Change return type to 'Simple'"); }
public void testReturnNull() { doTest("Change return type to 'ReturnNull'"); }
public void testTypeUseAnnotation() { doTest("Change return type to 'TypeUseAnnotation'"); }
public void testCast() { doTest("Change return type to 'Cast'"); }
public void testAnonymousClass() { doTest("Change return type to 'B'"); }
public void testNested() { doTest("Change return type to 'Nested'"); }