[extract method] IDEA-251837 use type element to annotate method nullability

[extract method] IDEA-251837 fix texts

GitOrigin-RevId: 37da2e7e8f83a4d85a87c644788a66ce6ea1e05f
This commit is contained in:
Alexandr Suhinin
2024-01-12 16:42:18 +00:00
committed by intellij-monorepo-bot
parent b2ffe6b32d
commit d75d619ab9
79 changed files with 84 additions and 162 deletions
@@ -215,7 +215,7 @@ internal fun updateMethodAnnotations(method: PsiMethod, inputParameters: List<In
//TODO use dataoutput.nullability instead
val returnedExpressions = PsiUtil.findReturnStatements(method).mapNotNull(PsiReturnStatement::getReturnValue)
val resultNullability = CodeFragmentAnalyzer.inferNullability(returnedExpressions)
ExtractMethodHelper.addNullabilityAnnotation(method, resultNullability)
ExtractMethodHelper.addNullabilityAnnotation(method.returnTypeElement, resultNullability)
}
val parameters = method.parameterList.parameters
inputParameters
@@ -223,7 +223,7 @@ internal fun updateMethodAnnotations(method: PsiMethod, inputParameters: List<In
.forEach { inputParameter ->
val parameterNullability = CodeFragmentAnalyzer.inferNullability(inputParameter.references)
val parameter = parameters.find { it.name == inputParameter.name }
if (parameter != null) ExtractMethodHelper.addNullabilityAnnotation(parameter, parameterNullability)
if (parameter != null) ExtractMethodHelper.addNullabilityAnnotation(parameter.typeElement, parameterNullability)
}
}
@@ -105,18 +105,17 @@ object ExtractMethodHelper {
return physicalParent ?: throw IllegalArgumentException()
}
fun addNullabilityAnnotation(owner: PsiModifierListOwner, nullability: Nullability) {
val nullabilityManager = NullableNotNullManager.getInstance(owner.project)
fun addNullabilityAnnotation(typeElement: PsiTypeElement?, nullability: Nullability) {
if (typeElement == null) return
val nullabilityManager = NullableNotNullManager.getInstance(typeElement.project)
val annotation = when (nullability) {
Nullability.NOT_NULL -> nullabilityManager.defaultNotNull
Nullability.NULLABLE -> nullabilityManager.defaultNullable
else -> return
}
val target: PsiAnnotationOwner? = if (owner is PsiParameter) owner.typeElement else owner.modifierList
if (target == null) return
val annotationElement = AddAnnotationPsiFix.addPhysicalAnnotationIfAbsent(annotation, PsiNameValuePair.EMPTY_ARRAY, target)
val annotationElement = AddAnnotationPsiFix.addPhysicalAnnotationIfAbsent(annotation, PsiNameValuePair.EMPTY_ARRAY, typeElement)
if (annotationElement != null) {
JavaCodeStyleManager.getInstance(owner.project).shortenClassReferences(annotationElement)
JavaCodeStyleManager.getInstance(typeElement.project).shortenClassReferences(annotationElement)
}
}
@@ -36,8 +36,7 @@ class Test {
}
}
@NotNull
private static String getString(Object o) {
private static @NotNull String getString(Object o) {
String s;
if (o instanceof String s2 && s2.length() == 1) {
s = "1";
@@ -20,8 +20,7 @@ class Test {
return List.of(list, list2);
}
@NotNull
private static List<String> getStrings() {
private static @NotNull List<String> getStrings() {
List<String> list = new ArrayList<>();
list.add("one");
list.add("two");
@@ -7,8 +7,7 @@ public class Test {
System.out.println(s);
}
@NotNull
private static String getString() {
private static @NotNull String getString() {
String s = "42";
return s;
}
@@ -13,8 +13,7 @@ public class Test {
};
}
@Nullable
private static String getString() {
private static @Nullable String getString() {
if (new Random().nextBoolean()) {
System.out.println();
return "one";
@@ -7,8 +7,7 @@ class SomeClass {
System.out.println(s);
}
@NotNull
private static String getString() {
private static @NotNull String getString() {
String s = "42";
return s;
}
@@ -10,8 +10,7 @@ class T {
List<String> list3 = getList1();
}
@NotNull
private static ArrayList<String> getList1() {
private static @NotNull ArrayList<String> getList1() {
return new ArrayList<>();
}
}
@@ -6,8 +6,7 @@ class Test {
String start2 = getSubstring("two");
}
@NotNull
private static String getSubstring(String one) {
private static @NotNull String getSubstring(String one) {
return one.substring(0, 10);
}
}
@@ -5,8 +5,7 @@ public class Test {
String r = getLowerCase(p);
}
@NotNull
private static String getLowerCase(String p) {
private static @NotNull String getLowerCase(String p) {
return p.toLowerCase();
}
@@ -2,8 +2,7 @@ import org.jetbrains.annotations.NotNull;
class Test {
@NotNull
private static String getString() {
private static @NotNull String getString() {
return "One" + "Two";
}
@@ -5,8 +5,7 @@ class Test {
System.out.println(getString() + "three");
}
@NotNull
private static String getString() {
private static @NotNull String getString() {
return "one" + "two";
}
}
@@ -6,8 +6,7 @@ class Test {
System.out.println("one " + getTwo() + " three");
}
@NotNull
private static String getTwo() {
private static @NotNull String getTwo() {
return "two";
}
}
@@ -7,8 +7,7 @@ class Test {
System.out.println(str);
}
@NotNull
private static String getString() {
private static @NotNull String getString() {
final String str = "atata";
do {
System.out.println();
@@ -9,8 +9,7 @@ public class Test {
return 0;
}
@Nullable
private static Result getResult(boolean param) {
private static @Nullable Result getResult(boolean param) {
int x = 0;
int y = 0;
if (param) return null;
@@ -9,8 +9,7 @@ public class Test {
System.out.println("Point(" + result.x() + ", " + result.y() + ")");
}
@NotNull
private static Result getResult() {
private static @NotNull Result getResult() {
int x = 0;
int y = 0;
System.out.println();
@@ -9,8 +9,7 @@ public class Test {
System.out.println("Point(" + x + ", " + result.y() + ")");
}
@NotNull
private static Result getResult() {
private static @NotNull Result getResult() {
int x = 42;
int y = 0;
System.out.println();
@@ -12,8 +12,7 @@ public class Test<R> {
System.out.println("Custom(" + myVariable.t() + ", " + myVariable.r() + ")");
}
@NotNull
private <T extends CharSequence> MyResult<T, R> getTrMyResult(T param) {
private <T extends CharSequence> @NotNull MyResult<T, R> getTrMyResult(T param) {
T t = param;
R r = getR();
System.out.println();
@@ -12,8 +12,7 @@ public class Test<R> {
System.out.println("Custom(" + result.t() + ", " + result.r() + ")");
}
@NotNull
private <T extends CharSequence> Result<T, R> getTrResult(T param) {
private <T extends CharSequence> @NotNull Result<T, R> getTrResult(T param) {
T t = param;
R r = getR();
System.out.println();
@@ -8,8 +8,7 @@ public class Test {
System.out.println("Point(" + result.x + ", " + result.y + ")");
}
@NotNull
private static Result getResult() {
private static @NotNull Result getResult() {
int x = 0;
int y = 0;
System.out.println();
@@ -8,8 +8,7 @@ public class Test {
System.out.println("Point(" + result.x() + ", " + result.y() + ")");
}
@NotNull
private static Result getResult() {
private static @NotNull Result getResult() {
int x = 0;
int y = 0;
System.out.println();
@@ -7,8 +7,7 @@ class SomeClass {
System.out.println(s);
}
@NotNull
private static String getString() {
private static @NotNull String getString() {
var s = "42";
return s;
}
@@ -8,8 +8,7 @@ public class Test {
String third = "Third";
}
@NotNull
private static String getFirst() {
private static @NotNull String getFirst() {
return "First";
}
}
@@ -6,8 +6,7 @@ class X {
r.run();
}
@NotNull
private Runnable getRunnable() {
private @NotNull Runnable getRunnable() {
Runnable r = new Runnable() {
@Override
public void run() {
@@ -6,8 +6,7 @@ class X {
r.run();
}
@NotNull
private static Runnable getRunnable() {
private static @NotNull Runnable getRunnable() {
Runnable r = new Runnable() {
@Override
public void run() {
@@ -10,8 +10,7 @@ public abstract class Test {
System.out.println(integer);
}
@NotNull
private static String renamed() {
private static @NotNull String renamed() {
return "4" + "2";
}
}
@@ -9,8 +9,7 @@ class Test {
return "default";
}
@Nullable
private String newMethod(int z) {
private @Nullable String newMethod(int z) {
if (z > 5) return null;
if (z < 0) return "sample";
return null;
@@ -9,8 +9,7 @@ class X {
}
}
@Nullable
private Object newMethod(Object o) {
private @Nullable Object newMethod(Object o) {
if (o == null) return null;
String x = bar(o);
return x;
@@ -9,8 +9,7 @@ class C {
}
}
@NotNull
private A newMethod() {
private @NotNull A newMethod() {
return A.getInstance();
}
}
@@ -10,8 +10,7 @@ class X {
return null;
}
@Nullable
private static String newMethod(String method, String testName) {
private static @Nullable String newMethod(String method, String testName) {
String strings = method;
if (strings != null && !strings.isEmpty()) {
return strings.substring(0) + testName;
@@ -17,8 +17,7 @@ class X {
return null;
}
@Nullable
private X newMethod(@Nullable String b) {
private @Nullable X newMethod(@Nullable String b) {
if (b != null) {
int x = 1;
return fun1(x);
@@ -6,8 +6,7 @@ class Box {
System.out.println(data);
}
@NotNull
private Data newMethod(String str1, String str2) {
private @NotNull Data newMethod(String str1, String str2) {
return new Data() {
@Override
public String getA() {
@@ -9,8 +9,7 @@ class Test {
System.out.println(s);
}
@NotNull
private String newMethod() throws IOException {
private @NotNull String newMethod() throws IOException {
String s = "result";
new Test().withError();
return s;
@@ -9,8 +9,7 @@ class C {
throw new RuntimeException();
}
@Nullable
private Integer newMethod(int id) {
private @Nullable Integer newMethod(int id) {
for (int n : list) {
if (n == id) {
return n <= 0 ? 0 : n;
@@ -7,8 +7,7 @@ class Test {
return 12;
}
@Nullable
private Integer newMethod() {
private @Nullable Integer newMethod() {
try {
if(cond1) return 0;
else if(cond2) return 1;
@@ -10,8 +10,7 @@ class K {
return 1;
}
@Nullable
private Object newMethod(Object o) {
private @Nullable Object newMethod(Object o) {
if (o == null) return null;
o = new Object();
return o;
@@ -7,8 +7,7 @@ class C {
return null;
}
@Nullable
private Object newMethod() {
private @Nullable Object newMethod() {
for (Object o : new ArrayList<Object>()) {
if (o != null) {
return o;
@@ -19,8 +19,7 @@ class Main {
return result;
}
@Nullable
private static Result newMethod(String name) {
private static @Nullable Result newMethod(String name) {
Result result;
if (name == null) {
result = new Result("Name is null");
@@ -12,8 +12,7 @@ class DoIfWhile {
return null;
}
@Nullable
private String newMethod(boolean b, int x) {
private @Nullable String newMethod(boolean b, int x) {
/*comment*/
if (b) {
String s = bar(x);
@@ -12,8 +12,7 @@ class ElseIf {
return null;
}
@Nullable
private String newMethod(boolean b) {
private @Nullable String newMethod(boolean b) {
if (b) {
String s = bar();
if (s != null) return s;
@@ -10,8 +10,7 @@ class ElseIf {
return null;
}
@Nullable
private String newMethod(boolean b) {
private @Nullable String newMethod(boolean b) {
if (b) {
String s = bar();
if (s != null) return s;
@@ -12,8 +12,7 @@ class DoIfWhile {
return null;
}
@Nullable
private String newMethod(boolean b, int x) {
private @Nullable String newMethod(boolean b, int x) {
/*comment*/
if (b) {
String s = bar(x);
@@ -12,8 +12,7 @@ class ElseIf {
return null;
}
@Nullable
private String newMethod(boolean b) {
private @Nullable String newMethod(boolean b) {
if (b) {
String s = bar();
if (s != null) return s;
@@ -7,8 +7,7 @@ class Test {
System.out.println(code);
}
@Nullable
private Integer newMethod(int x) {
private @Nullable Integer newMethod(int x) {
int code;
if (x == 22) return null;
if (x > 0) {
@@ -10,8 +10,7 @@ class Test {
return "default";
}
@Nullable
private Integer newMethod(boolean condition, String f2) {
private @Nullable Integer newMethod(boolean condition, String f2) {
int x = 42;
if (condition) return null;
if (!condition) return null;
@@ -10,8 +10,7 @@ public class S {
System.out.print(s);
}
@NotNull
private String newMethod() {
private @NotNull String newMethod() {
String s;
s = "";
return s;
@@ -6,8 +6,7 @@ class Test {
System.out.println(x);
}
@NotNull
private String newMethod(int y) {
private @NotNull String newMethod(int y) {
String x;
switch (y){
case 3:
@@ -7,8 +7,7 @@ class Test {
System.out.println(x);
}
@Nullable
private Integer newMethod(int y) {
private @Nullable Integer newMethod(int y) {
int x;
switch (y){
case 3:
@@ -8,8 +8,7 @@ class Test {
b(newMethod());
}
@NotNull
private Supplier newMethod() {
private @NotNull Supplier newMethod() {
return (s) -> {
System.out.println(s);
};
@@ -8,8 +8,7 @@ class Test {
return -1;
}
@Nullable
private String newMethod(int x) {
private @Nullable String newMethod(int x) {
String out = "out";
if (x > 10) return null;
if (x < 10) return null;
@@ -21,8 +21,7 @@ public class OutputVariableReused {
return null;
}
@Nullable
private X newMethod(String s, String left, String right) {
private @Nullable X newMethod(String s, String left, String right) {
String res = convert(s, left, right);
if (res != null) {
return new X(res);
@@ -10,8 +10,7 @@ class Test {
};
}
@NotNull
private String newMethod() {
private @NotNull String newMethod() {
return "42";
}
}
@@ -9,8 +9,7 @@ class X {
}
}
@Nullable
private Object newMethod(Object o) {
private @Nullable Object newMethod(Object o) {
if (o == null) return null;
Object x = bar(o);
return x;
@@ -6,8 +6,7 @@ class X {
}
@NotNull
private static String newMethod() {
private static @NotNull String newMethod() {
System.out.println();
return f();
}
@@ -5,8 +5,7 @@ class Test {
return newMethod();
}
@NotNull
private String newMethod() {
private @NotNull String newMethod() {
return "42";
}
}
@@ -7,8 +7,7 @@ class Test {
return 42l;
}
@Nullable
private Double newMethod(boolean b) {
private @Nullable Double newMethod(boolean b) {
if (b) {
return 42.0;
}
@@ -7,8 +7,7 @@ class Test {
return 42l;
}
@Nullable
private Double newMethod(boolean b, Integer notNullInt) {
private @Nullable Double newMethod(boolean b, Integer notNullInt) {
if (b) {
return (double) notNullInt;
}
@@ -9,8 +9,7 @@ class Test {
return null;
}
@Nullable
private Collection<String> newMethod(boolean c, @NotNull Set<String> set) {
private @Nullable Collection<String> newMethod(boolean c, @NotNull Set<String> set) {
if (c) {
return new ArrayList<String>();
}
@@ -13,8 +13,7 @@ public class Test {
return null;
}
@Nullable
private Test newMethod(List<String> list) {
private @Nullable Test newMethod(List<String> list) {
for (String some : list) {
String x = "x";
String y = "y";
@@ -3,8 +3,7 @@ import org.jetbrains.annotations.NotNull;
interface I {
String FOO = newMethod();
@NotNull
static String newMethod() {
static @NotNull String newMethod() {
return "hello";
}
}
@@ -7,8 +7,7 @@ class C {
newMethod(o).run();
}
@NotNull
private Runnable newMethod(@NotNull Object o) {
private @NotNull Runnable newMethod(@NotNull Object o) {
return new Runnable() {
@Override
public void run() {
@@ -7,8 +7,7 @@ class C {
newMethod(o).run();
}
@NotNull
private Runnable newMethod(@NotNull Object o) {
private @NotNull Runnable newMethod(@NotNull Object o) {
return (Runnable) (() -> bar(o));
}
@@ -7,8 +7,7 @@ class C {
newMethod(o).run();
}
@NotNull
private Runnable newMethod(@NotNull Object o) {
private @NotNull Runnable newMethod(@NotNull Object o) {
return (Runnable) (() -> bar(o));
}
@@ -16,8 +16,7 @@ class X {
return null;
}
@Nullable
private X newMethod(boolean b) {
private @Nullable X newMethod(boolean b) {
if (b) {
int x = 1;
return fun1(x);
@@ -10,8 +10,7 @@ class Test {
return null;
}
@Nullable
private String newMethod(int i, boolean flag) {
private @Nullable String newMethod(int i, boolean flag) {
String xxx = "";
if (flag) {
for (int j = 0; j < 100; j++) {
@@ -13,8 +13,7 @@ class Test {
}
}
@Nullable
private Pojo newMethod() {
private @Nullable Pojo newMethod() {
Pojo x = things.get(0);
if(x.it > 0) {
@@ -11,8 +11,7 @@ class Test {
}
}
@Nullable
private String newMethod() {
private @Nullable String newMethod() {
final String str = "";
if (str == "a") {
return null;
@@ -7,8 +7,7 @@ class Test {
System.out.println(o);
}
@Nullable
private Object newMethod() {
private @Nullable Object newMethod() {
Object o = "";
for (int i = 0; i < 5; i++) {
if (i == 10){
@@ -8,8 +8,7 @@ class Test {
return o;
}
@Nullable
private Object newMethod() {
private @Nullable Object newMethod() {
Object o = "";
for (int i = 0; i < 5; i++) {
if (i == 10){
@@ -11,8 +11,7 @@ class A {
return "";
}
@NotNull
private String newMethod() {
private @NotNull String newMethod() {
try {
return "";
}
@@ -8,8 +8,7 @@ class Test {
return "return";
}
@Nullable
private String newMethod(String variable) {
private @Nullable String newMethod(String variable) {
if (1 == 1) return variable;
if (2 == 1) return "literal";
return null;
@@ -9,8 +9,7 @@ public class Foo {
},
f2 = new Foo(){};
@NotNull
private static String newMethod() {
private static @NotNull String newMethod() {
return "a" + "b";
}
@@ -37,8 +37,7 @@ public class SCR27887 {
return included.size();
}
@NotNull
private RefObjectUList newMethod(ZippingXMLGeneratorFactory genFac) {
private @NotNull RefObjectUList newMethod(ZippingXMLGeneratorFactory genFac) {
RefObjectUList included = makeIncludedSet();
if (!included.isEmpty()) {
ScatteringDocBuilder docBuilder = new MyDocBuilder(repository, included);
@@ -9,8 +9,7 @@ class Test {
return list;
}
@NotNull
private Collection newMethod() {
private @NotNull Collection newMethod() {
return new ArrayList<String>();
}
}
@@ -8,8 +8,7 @@ public class A {
this(newMethod());
}
@NotNull
private static String newMethod() {
private static @NotNull String newMethod() {
return "a";
}
}
@@ -14,8 +14,7 @@ class A {
}
}
@NotNull
private String newMethod(String s) throws MyException {
private @NotNull String newMethod(String s) throws MyException {
bar();
s = "b";
return s;
@@ -13,8 +13,7 @@ public class A {
}
}
@NotNull
private String newMethod(String s) {
private @NotNull String newMethod(String s) {
if (r()) throw new RuntimeException();
s = "b";
return s;
@@ -18,8 +18,7 @@ class A {
}
}
@NotNull
private String newMethod(boolean b) throws ExceptionB {
private @NotNull String newMethod(boolean b) throws ExceptionB {
String s;
s = "b";
if (b) throw new ExceptionB();
@@ -11,8 +11,7 @@ class BasicLazyResolveTest {
}
@NotNull
private Object newMethod() {
private @NotNull Object newMethod() {
return new Object() {
/*