mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-05-06 05:10:22 +07:00
StreamToLoop: inline simple common method references
This commit is contained in:
@@ -155,6 +155,8 @@ abstract class FunctionHelper {
|
||||
if (expression instanceof PsiMethodReferenceExpression) {
|
||||
PsiMethodReferenceExpression methodRef = (PsiMethodReferenceExpression)expression;
|
||||
if (methodRef.resolve() == null) return null;
|
||||
FunctionHelper fn = tryInlineMethodReference(paramCount, returnType, methodRef);
|
||||
if (fn != null) return fn;
|
||||
return new MethodReferenceFunctionHelper(returnType, type, methodRef);
|
||||
}
|
||||
if (expression instanceof PsiReferenceExpression && ExpressionUtils.isSimpleExpression(expression)) {
|
||||
@@ -176,6 +178,45 @@ abstract class FunctionHelper {
|
||||
return new ComplexExpressionFunctionHelper(returnType, type, interfaceMethod.getName(), expression);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private static FunctionHelper tryInlineMethodReference(int paramCount, PsiType returnType, PsiMethodReferenceExpression methodRef) {
|
||||
PsiElement element = methodRef.resolve();
|
||||
if (element instanceof PsiMethod) {
|
||||
PsiMethod method = (PsiMethod)element;
|
||||
String name = method.getName();
|
||||
PsiClass aClass = method.getContainingClass();
|
||||
if (aClass != null) {
|
||||
String className = aClass.getQualifiedName();
|
||||
if("java.util.Objects".equals(className) && paramCount == 1) {
|
||||
if (name.equals("nonNull")) {
|
||||
return new InlinedFunctionHelper(returnType, 1, "{0}!=null");
|
||||
}
|
||||
if (name.equals("isNull")) {
|
||||
return new InlinedFunctionHelper(returnType, 1, "{0}==null");
|
||||
}
|
||||
}
|
||||
if (paramCount == 2 && name.equals("sum") && (CommonClassNames.JAVA_LANG_INTEGER.equals(className) ||
|
||||
CommonClassNames.JAVA_LANG_LONG.equals(className) ||
|
||||
CommonClassNames.JAVA_LANG_DOUBLE.equals(className))) {
|
||||
return new InlinedFunctionHelper(returnType, 2, "{0}+{1}");
|
||||
}
|
||||
if(CommonClassNames.JAVA_LANG_CLASS.equals(className) && paramCount == 1) {
|
||||
PsiExpression qualifier = methodRef.getQualifierExpression();
|
||||
if(qualifier instanceof PsiClassObjectAccessExpression) {
|
||||
PsiTypeElement type = ((PsiClassObjectAccessExpression)qualifier).getOperand();
|
||||
if(name.equals("isInstance")) {
|
||||
return new InlinedFunctionHelper(returnType, 1, "{0} instanceof "+type.getText());
|
||||
}
|
||||
if(name.equals("cast")) {
|
||||
return new InlinedFunctionHelper(returnType, 1, "("+type.getText()+"){0}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Contract(pure = true)
|
||||
static FunctionHelper newObjectSupplier(PsiType type, String instanceClassName) {
|
||||
|
||||
@@ -6,7 +6,7 @@ import java.util.Objects;
|
||||
public class Main {
|
||||
boolean test(String[] strings) {
|
||||
for (String s : strings) {
|
||||
if (Objects.nonNull(s)) {
|
||||
if (s != null) {
|
||||
if (!s.startsWith("xyz")) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ import java.util.Objects;
|
||||
public class Main {
|
||||
String test(String[] strings) {
|
||||
for (String s : strings) {
|
||||
if (Objects.nonNull(s)) {
|
||||
if (s != null) {
|
||||
if (!s.startsWith("xyz")) {
|
||||
return "s";
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ public class Main {
|
||||
private static void test(List<String> list) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (String str : list) {
|
||||
if (Objects.nonNull(str)) {
|
||||
if (str != null) {
|
||||
sb.append(str);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@ public class Main {
|
||||
double sum = 0;
|
||||
long count = 0;
|
||||
for (String s : list) {
|
||||
if (Objects.nonNull(s)) {
|
||||
if (s != null) {
|
||||
sum += 1.0 / s;
|
||||
count++;
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@ public class Main {
|
||||
long sum = 0;
|
||||
long count = 0;
|
||||
for (String s : list) {
|
||||
if (Objects.nonNull(s)) {
|
||||
if (s != null) {
|
||||
sum += s.length();
|
||||
count++;
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@ public class Main {
|
||||
private void collect() {
|
||||
List<CharSequence> list = new ArrayList<>();
|
||||
for (CharSequence charSequence : getList()) {
|
||||
if (Objects.nonNull(charSequence)) {
|
||||
if (charSequence != null) {
|
||||
list.add(charSequence);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,7 +16,7 @@ public class Main {
|
||||
private void collect() {
|
||||
Map<CharSequence, List<? extends CharSequence>> result = new HashMap<>();
|
||||
for (CharSequence charSequence : getList()) {
|
||||
if (Objects.nonNull(charSequence)) {
|
||||
if (charSequence != null) {
|
||||
if (result.put(charSequence, asList(charSequence)) != null) {
|
||||
throw new IllegalStateException("Duplicate key");
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@ public class Main<T> {
|
||||
Integer acc = 0;
|
||||
for (String s : Arrays.asList("a", "bb", "ccc")) {
|
||||
Integer length = s.length();
|
||||
acc = Integer.sum(acc, length);
|
||||
acc = acc + length;
|
||||
}
|
||||
Integer totalLength = acc;
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@ public class Main {
|
||||
public static DoubleSummaryStatistics test(List<String> strings) {
|
||||
DoubleSummaryStatistics stat = new DoubleSummaryStatistics();
|
||||
for (String str : strings) {
|
||||
if (Objects.nonNull(str)) {
|
||||
if (str != null) {
|
||||
stat.accept(str.length() / 2.0);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@ public class Main {
|
||||
public static Double test(List<String> strings) {
|
||||
double sum = 0;
|
||||
for (String string : strings) {
|
||||
if (Objects.nonNull(string)) {
|
||||
if (string != null) {
|
||||
sum += string.length();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@ import static java.util.Arrays.asList;
|
||||
public class Main {
|
||||
public static boolean test(List<List<String>> list) {
|
||||
for (List<String> strings : list) {
|
||||
if (Objects.nonNull(strings)) {
|
||||
if (strings != null) {
|
||||
for (String s : strings) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ import java.util.Objects;
|
||||
public class Main {
|
||||
String test(List<List<String>> strings) {
|
||||
for (List<String> string : strings) {
|
||||
if (Objects.nonNull(string)) {
|
||||
if (string != null) {
|
||||
for (String s : string) {
|
||||
return "abc";
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ public class Main {
|
||||
public static void test(List<String> strings) {
|
||||
Map<Integer, DoubleSummaryStatistics> map = new HashMap<>();
|
||||
for (String string : strings) {
|
||||
if (Objects.nonNull(string)) {
|
||||
if (string != null) {
|
||||
map.computeIfAbsent(string.length(), k -> new DoubleSummaryStatistics()).accept(string.length());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ public class Main {
|
||||
public static void test(List<String> strings) {
|
||||
Map<Integer, Set<String>> map = new HashMap<>();
|
||||
for (String string : strings) {
|
||||
if (Objects.nonNull(string)) {
|
||||
if (string != null) {
|
||||
map.computeIfAbsent(string.length(), k -> new HashSet<>()).add(string);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@ public class Main {
|
||||
long sum = 0;
|
||||
long count = 0;
|
||||
for (String s1 : list) {
|
||||
if (Objects.nonNull(s1)) {
|
||||
if (s1 != null) {
|
||||
sum += s1.length();
|
||||
count++;
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@ public class Main {
|
||||
public void test(String... list) {
|
||||
Runnable s = () -> {
|
||||
for (String s1 : list) {
|
||||
if (Objects.nonNull(s1)) {
|
||||
if (s1 != null) {
|
||||
System.out.println(s1);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
// "Replace Stream API chain with loop" "true"
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class Main {
|
||||
private static Optional<String> max(List<?> list) {
|
||||
boolean seen = false;
|
||||
String best = null;
|
||||
for (Object o : list) {
|
||||
if (o instanceof String[]) {
|
||||
String[] x = (String[]) o;
|
||||
String s = x[0];
|
||||
if (!seen || s.compareTo(best) > 0) {
|
||||
seen = true;
|
||||
best = s;
|
||||
}
|
||||
}
|
||||
}
|
||||
return seen ? Optional.of(best) : Optional.empty();
|
||||
}
|
||||
}
|
||||
@@ -7,7 +7,7 @@ import java.util.Objects;
|
||||
public class Main {
|
||||
private static void test(List<String> names) {
|
||||
for (String name : names) {
|
||||
if (Objects.nonNull(name)) {
|
||||
if (name != null) {
|
||||
System.out.println(name);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,7 +15,7 @@ public class Main {
|
||||
super(false);
|
||||
boolean b = false;
|
||||
for (String s : Arrays.asList("a", "b", "c")) {
|
||||
if (Objects.nonNull(s)) {
|
||||
if (s != null) {
|
||||
b = true;
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@ public class Main {
|
||||
private static <A> A[] toArraySkippingNulls(List<?> list, IntFunction<A[]> generator) {
|
||||
List<Object> result = new ArrayList<>();
|
||||
for (Object o : list) {
|
||||
if (Objects.nonNull(o)) {
|
||||
if (o != null) {
|
||||
result.add(o);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ public class Main {
|
||||
private static Number[] test(Object[] objects) {
|
||||
List<Object> list = new ArrayList<>();
|
||||
for (Object object : objects) {
|
||||
if (Number.class.isInstance(object)) {
|
||||
if (object instanceof Number) {
|
||||
list.add(object);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
// "Replace Stream API chain with loop" "true"
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class Main {
|
||||
private static Optional<String> max(List<?> list) {
|
||||
return list.stream().filter(String[].class::isInstance).map(String[].class::cast).map(x -> x[0]).m<caret>ax(Comparator.naturalOrder());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user