IDEA-163764 "Replace Optional.isPresent() checks with functional-style expressions" create uncompilable code

IDEA-163463 Stream API migration: type argument before map appears sometimes when it's unnecessary
This commit is contained in:
Tagir Valeev
2016-11-11 11:28:44 +07:00
parent 67c4dfb483
commit 7e574d661c
16 changed files with 190 additions and 16 deletions

View File

@@ -0,0 +1,15 @@
// "Replace Optional.isPresent() condition with functional style expression" "true"
import java.lang.annotation.Annotation;
import java.lang.reflect.AnnotatedElement;
import java.util.Optional;
public class Main<T> {
public static <A extends Annotation> Optional<A> findAnnotation(Optional<? extends AnnotatedElement> element) {
return element.<Optional<A>>map(annotatedElement -> annotatedElement.getAnnotations().length == 0 ? Optional.empty() : null).orElseGet(() -> findAnnotation((AnnotatedElement) null));
}
private static <A extends Annotation> Optional<A> findAnnotation(AnnotatedElement element) {
return Optional.empty();
}
}

View File

@@ -0,0 +1,9 @@
// "Replace Optional.isPresent() condition with functional style expression" "true"
import java.util.*;
public class Main {
public static Runnable get(Optional<String> s) {
return s.<Runnable>map(s1 -> s1::trim).orElse(null);
}
}

View File

@@ -0,0 +1,18 @@
// "Replace Optional.isPresent() condition with functional style expression" "true"
import java.lang.annotation.Annotation;
import java.lang.reflect.AnnotatedElement;
import java.util.Optional;
public class Main<T> {
public static <A extends Annotation> Optional<A> findAnnotation(Optional<? extends AnnotatedElement> element) {
if (element.isPre<caret>sent()) {
return element.get().getAnnotations().length == 0 ? Optional.empty() : null;
}
return findAnnotation((AnnotatedElement)null);
}
private static <A extends Annotation> Optional<A> findAnnotation(AnnotatedElement element) {
return Optional.empty();
}
}

View File

@@ -0,0 +1,18 @@
// "Replace Optional.isPresent() condition with functional style expression" "false"
import java.lang.annotation.Annotation;
import java.lang.reflect.AnnotatedElement;
import java.util.Optional;
public class Main<T> {
public static <A extends Annotation> Optional<A> findAnnotation(Optional<? extends AnnotatedElement> element) {
if (element.isPre<caret>sent()) {
return Optional.empty();
}
return findAnnotation((AnnotatedElement)null);
}
private static <A extends Annotation> Optional<A> findAnnotation(AnnotatedElement element) {
return Optional.empty();
}
}

View File

@@ -0,0 +1,12 @@
// "Replace Optional.isPresent() condition with functional style expression" "true"
import java.util.*;
public class Main {
public static Runnable get(Optional<String> s) {
if(s.isPres<caret>ent()) {
return s.get()::trim;
}
return null;
}
}

View File

@@ -5,7 +5,7 @@ import java.util.stream.Collectors;
public class Main {
public List<Runnable> test(List<String> list) {
List<Runnable> result = list.stream().<Runnable>map(s -> new Runnable() {
List<Runnable> result = list.stream().map(s -> new Runnable() {
@Override
public void run() {
String str = s;

View File

@@ -0,0 +1,11 @@
// "Replace with collect" "true"
import java.util.*;
import java.util.stream.Collectors;
public class Main {
public List<CharSequence> getListCharSequence(List<String> input) {
List<CharSequence> result = input.stream().filter(s -> !s.isEmpty()).map(String::trim).collect(Collectors.toList());
return result;
}
}

View File

@@ -0,0 +1,10 @@
// "Replace with findFirst()" "true"
import java.util.*;
public class Main {
public List<String> getErrors(List<String> data) {
List<String> def = Collections.singletonList("Not found");
return data.stream().filter(s -> s.startsWith("xyz")).findFirst().<List<String>>map(s -> s.length() < 10 ? Collections.emptyList() : Arrays.asList()).orElse(def);
}
}

View File

@@ -0,0 +1,10 @@
// "Replace with findFirst()" "true"
import java.util.*;
public class Main {
public Runnable getRunnable(List<String> data) {
Runnable def = () -> {};
return data.stream().filter(s -> s.startsWith("xyz")).findFirst().<Runnable>map(s -> s.length() > 2 ? s::trim : System.out::println).orElse(def);
}
}

View File

@@ -0,0 +1,15 @@
// "Replace with collect" "true"
import java.util.*;
public class Main {
public List<CharSequence> getListCharSequence(List<String> input) {
List<CharSequence> result = new ArrayList<>();
for(String s : in<caret>put) {
if(!s.isEmpty()) {
result.add(s.trim());
}
}
return result;
}
}

View File

@@ -0,0 +1,15 @@
// "Replace with findFirst()" "true"
import java.util.*;
public class Main {
public List<String> getErrors(List<String> data) {
List<String> def = Collections.singletonList("Not found");
for(String s : dat<caret>a) {
if(s.startsWith("xyz")) {
return s.length() < 10 ? Collections.emptyList() : Arrays.asList();
}
}
return def;
}
}

View File

@@ -0,0 +1,15 @@
// "Replace with findFirst()" "true"
import java.util.*;
public class Main {
public Runnable getRunnable(List<String> data) {
Runnable def = () -> {};
for(String s : dat<caret>a) {
if(s.startsWith("xyz")) {
return s.length() > 2 ? s::trim : System.out::println;
}
}
return def;
}
}