View Javadoc
1   /*
2   UnusedImports
3   processJavadoc = false
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.List;
20  import java.util.Map;
21  import java.util.Collections;
22  import java.util.function.Function;
23  import java.util.function.Supplier;
24  import java.util.Arrays; // violation 'Unused import - java.util.Arrays.'
25  import static java.lang.Integer.parseInt; // violation 'Unused import - java.lang.Integer.'
26  import static java.lang.String.format; // violation 'Unused import - java.lang.String.format.'
27  import static java.util.List.of; // violation 'Unused import - java.util.List.of.'
28  import static java.util.Collections.emptyMap; // violation 'Unused import - java.util.Collections.emptyMap.'
29  
30  /**
31   * This {@link Arrays::sort} is NOT a valid link,
32   * same as the one below.
33   * Use {@link Integer::parseInt}.
34   */
35  public class InputUnusedImportsFromStaticMethodRefJavadocDisabled {
36  
37      public static <T, R> Function<T, R> func(Function<T, R> f) { return f; }
38  
39      InputUnusedImportsFromStaticMethodRefJavadocDisabled() {
40      }
41  
42      void testMethodRef()
43      {
44          Optional<String> test = Optional.empty();
45          test.map(String::format);
46      }
47  
48      void testMethodRefWithClass()
49      {
50          Optional<String> test = Optional.empty();
51          test.map(Objects::nonNull);
52      }
53      void testConstructorMethodRefArgument()
54      {
55          func(func(@CJ2 WeakReference::new));
56      }
57  
58      void testConstructorMethodRefAssignment()
59      {
60          ListGetter2<String> listGetter = ArrayList::new;
61          listGetter.get();
62      }
63      void testMethodRefOnGenericTypeAssignment()
64      {
65          Function<String, List<String>> fn = List::of;
66          List<String> list = fn.apply("test");
67      }
68  
69      void testMethodRefOnGenericTypeArgument()
70      {
71          Function<String, List<String>> fn = func(List::of);
72          List<String> list = fn.apply("test");
73      }
74      void testMethodRefImportedInJavadoc()
75      {
76          Supplier<Map<String, String>> supplier = Collections::emptyMap;
77          Map<String, String> list = supplier.get();
78      }
79  
80  }
81  
82  @Retention(RUNTIME) @Target({TYPE_USE}) @interface CJ2 { }
83  
84  interface ListGetter2<T> {
85      List<T> get();
86  }