1
2
3
4
5
6
7
8 package com.puppycrawl.tools.checkstyle.checks.imports.unusedimports;
9
10 import static java.lang.annotation.ElementType.TYPE_USE;
11 import static java.lang.annotation.RetentionPolicy.RUNTIME;
12
13 import java.lang.annotation.Retention;
14 import java.lang.annotation.Target;
15 import java.lang.ref.WeakReference;
16 import java.util.ArrayList;
17 import java.util.Optional;
18 import java.util.Objects;
19 import java.util.Arrays;
20 import java.util.List;
21 import java.util.Map;
22 import java.util.Collections;
23 import java.util.function.Consumer;
24 import java.util.function.Function;
25 import java.util.function.Supplier;
26 import static java.lang.String.format;
27 import static java.util.Arrays.sort;
28 import static java.util.List.of;
29 import static java.util.Collections.emptyMap;
30
31
32
33
34
35 public class InputUnusedImportsFromStaticMethodRef {
36 public static <T, R> Function<T, R> func(Function<T, R> f) { return f; }
37
38 InputUnusedImportsFromStaticMethodRef() {
39 }
40
41 void testMethodRef() {
42 Optional<String> test = Optional.empty();
43 test.map(String::format);
44 }
45
46 void testMethodRefWithClass() {
47 Optional<String> test = Optional.empty();
48 test.map(Objects::nonNull);
49 }
50
51 void testMethodRefAssignment() {
52 int[] array = { 10, 2, 19, 5, 17 };
53 Consumer<int[]> consumer = Arrays::sort;
54 consumer.accept(array);
55 }
56
57 void testConstructorMethodRefArgument() {
58 func(func(@CJ WeakReference::new));
59 }
60
61 void testConstructorMethodRefAssignment() {
62 ListGetter<String> listGetter = ArrayList::new;
63 listGetter.get();
64 }
65 void testMethodRefOnGenericTypeAssignment() {
66 Function<String, List<String>> fn = List::of;
67 List<String> list = fn.apply("test");
68 }
69
70 void testMethodRefOnGenericTypeArgument() {
71 Function<String, List<String>> fn = func(List::of);
72 List<String> list = fn.apply("test");
73 }
74
75 void testMethodRefImportedInJavadoc() {
76 Supplier<Map<String, String>> supplier = Collections::emptyMap;
77 Map<String, String> list = supplier.get();
78 }
79
80 void testMethodRefNotImported() {
81 Function<String, Integer> parseIntFunc = Integer::parseInt;
82 int number = parseIntFunc.apply("42");
83 }
84
85 }
86
87 @Retention(RUNTIME) @Target({TYPE_USE}) @interface CJ { }
88 interface ListGetter<T> {
89 List<T> get();
90 }
91