-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue #12923: static imports with method reference reported as unused
- Loading branch information
Showing
8 changed files
with
350 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
.../tools/checkstyle/checks/imports/unusedimports/InputUnusedImportsFromStaticMethodRef.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/* | ||
UnusedImports | ||
processJavadoc = (default)true | ||
*/ | ||
|
||
package com.puppycrawl.tools.checkstyle.checks.imports.unusedimports; | ||
|
||
import static java.lang.annotation.ElementType.TYPE_USE; | ||
import static java.lang.annotation.RetentionPolicy.RUNTIME; | ||
|
||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.Target; | ||
import java.lang.ref.WeakReference; | ||
import java.util.ArrayList; | ||
import java.util.Optional; | ||
import java.util.Objects; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Collections; | ||
import java.util.function.Consumer; | ||
import java.util.function.Function; | ||
import java.util.function.Supplier; | ||
import static java.lang.String.format; // violation 'Unused import - java.lang.String.format.' | ||
import static java.util.Arrays.sort; // violation 'Unused import - java.util.Arrays.sort.' | ||
import static java.util.List.of; // violation 'Unused import - java.util.List.of.' | ||
import static java.util.Collections.emptyMap; // violation 'Unused import - java.util.Collections.emptyMap.' | ||
|
||
/** | ||
* Use {@link Collections::emptyMap} in this javadoc. | ||
*/ | ||
public class InputUnusedImportsFromStaticMethodRef { | ||
public static <T, R> Function<T, R> func(Function<T, R> f) { return f; } | ||
|
||
InputUnusedImportsFromStaticMethodRef() { | ||
} | ||
|
||
void testMethodRef() { | ||
Optional<String> test = Optional.empty(); | ||
test.map(String::format); | ||
} | ||
|
||
void testMethodRefWithClass() { | ||
Optional<String> test = Optional.empty(); | ||
test.map(Objects::nonNull); | ||
} | ||
|
||
void testMethodRefAssignment() { | ||
int[] array = { 10, 2, 19, 5, 17 }; | ||
Consumer<int[]> consumer = Arrays::sort; | ||
consumer.accept(array); | ||
} | ||
|
||
void testConstructorMethodRefArgument() { | ||
func(func(@CJ WeakReference::new)); | ||
} | ||
|
||
void testConstructorMethodRefAssignment() { | ||
ListGetter<String> listGetter = ArrayList::new; | ||
listGetter.get(); | ||
} | ||
void testMethodRefOnGenericTypeAssignment() { | ||
Function<String, List<String>> fn = List::of; | ||
List<String> list = fn.apply("test"); | ||
} | ||
|
||
void testMethodRefOnGenericTypeArgument() { | ||
Function<String, List<String>> fn = func(List::of); | ||
List<String> list = fn.apply("test"); | ||
} | ||
|
||
void testMethodRefImportedInJavadoc() { | ||
Supplier<Map<String, String>> supplier = Collections::emptyMap; | ||
Map<String, String> list = supplier.get(); | ||
} | ||
|
||
void testMethodRefNotImported() { | ||
Function<String, Integer> parseIntFunc = Integer::parseInt; | ||
int number = parseIntFunc.apply("42"); | ||
} | ||
|
||
} | ||
|
||
@Retention(RUNTIME) @Target({TYPE_USE}) @interface CJ { } | ||
interface ListGetter<T> { | ||
List<T> get(); | ||
} | ||
|
32 changes: 32 additions & 0 deletions
32
...heckstyle/checks/imports/unusedimports/InputUnusedImportsFromStaticMethodRefExtended.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
UnusedImports | ||
processJavadoc = (default)true | ||
*/ | ||
|
||
package com.puppycrawl.tools.checkstyle.checks.imports.unusedimports; | ||
|
||
import java.util.Optional; | ||
|
||
import java.util.function.Function; | ||
import java.util.List; | ||
import java.util.Arrays; | ||
import java.util.Objects; // violation 'Unused import - java.util.Objects.' | ||
import static java.util.Arrays.asList; // violation 'Unused import - java.util.Arrays.asList.' | ||
|
||
public class InputUnusedImportsFromStaticMethodRefExtended { | ||
InputUnusedImportsFromStaticMethodRefExtended() { | ||
} | ||
|
||
void testMethodRefWithQualifiedName() | ||
{ | ||
Optional<String> test = Optional.empty(); | ||
test.map(java.util.Objects::nonNull); | ||
} | ||
|
||
void testMethodRefWithGenericType() | ||
{ | ||
Function<String[],List<String>> listGetter = Arrays::<String>asList; | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
...ckstyle/checks/imports/unusedimports/InputUnusedImportsFromStaticMethodRefInDocsOnly.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
UnusedImports | ||
processJavadoc = (default)true | ||
*/ | ||
package com.puppycrawl.tools.checkstyle.checks.imports.unusedimports; | ||
|
||
import java.util.Collections; | ||
import java.util.Arrays; | ||
import static java.lang.Integer.parseInt; // violation 'Unused import - java.lang.Integer.parseInt.' | ||
import static java.util.Collections.emptyEnumeration; // violation 'Unused import - java.util.Collections.emptyEnumeration.' | ||
import static java.util.Arrays.sort; // violation 'Unused import - java.util.Arrays.sort.' | ||
|
||
public class InputUnusedImportsFromStaticMethodRefInDocsOnly { | ||
|
||
/** | ||
* Use {@link Collections::emptyEnumeration} this line is skipped as it is not valid link. | ||
* @see Collections#emptyEnumeration | ||
* @throws IllegalAccessError::new | ||
*/ | ||
public static void m() {} | ||
|
||
/** | ||
* @see Arrays#sort | ||
* @throws IllegalAccessError::new | ||
*/ | ||
public static void n() {} | ||
|
||
/** | ||
* Use {@link Integer::parseInt} this line is skipped as it is not valid link. | ||
* @throws IllegalAccessError::new | ||
*/ | ||
public static void l() {} | ||
|
||
} |
85 changes: 85 additions & 0 deletions
85
...le/checks/imports/unusedimports/InputUnusedImportsFromStaticMethodRefJavadocDisabled.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
UnusedImports | ||
processJavadoc = false | ||
*/ | ||
|
||
package com.puppycrawl.tools.checkstyle.checks.imports.unusedimports; | ||
|
||
import static java.lang.annotation.ElementType.TYPE_USE; | ||
import static java.lang.annotation.RetentionPolicy.RUNTIME; | ||
|
||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.Target; | ||
import java.lang.ref.WeakReference; | ||
import java.util.ArrayList; | ||
import java.util.Optional; | ||
import java.util.Objects; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Collections; | ||
import java.util.function.Function; | ||
import java.util.function.Supplier; | ||
import java.util.Arrays; // violation 'Unused import - java.util.Arrays.' | ||
import static java.lang.Integer.parseInt; // violation 'Unused import - java.lang.Integer.' | ||
import static java.lang.String.format; // violation 'Unused import - java.lang.String.format.' | ||
import static java.util.List.of; // violation 'Unused import - java.util.List.of.' | ||
import static java.util.Collections.emptyMap; // violation 'Unused import - java.util.Collections.emptyMap.' | ||
|
||
/** | ||
* Use {@link Arrays::sort} in this javadoc. | ||
* Use {@link Integer::parseInt} in this javadoc. | ||
*/ | ||
public class InputUnusedImportsFromStaticMethodRefJavadocDisabled { | ||
|
||
public static <T, R> Function<T, R> func(Function<T, R> f) { return f; } | ||
|
||
InputUnusedImportsFromStaticMethodRefJavadocDisabled() { | ||
} | ||
|
||
void testMethodRef() | ||
{ | ||
Optional<String> test = Optional.empty(); | ||
test.map(String::format); | ||
} | ||
|
||
void testMethodRefWithClass() | ||
{ | ||
Optional<String> test = Optional.empty(); | ||
test.map(Objects::nonNull); | ||
} | ||
void testConstructorMethodRefArgument() | ||
{ | ||
func(func(@CJ2 WeakReference::new)); | ||
} | ||
|
||
void testConstructorMethodRefAssignment() | ||
{ | ||
ListGetter2<String> listGetter = ArrayList::new; | ||
listGetter.get(); | ||
} | ||
void testMethodRefOnGenericTypeAssignment() | ||
{ | ||
Function<String, List<String>> fn = List::of; | ||
List<String> list = fn.apply("test"); | ||
} | ||
|
||
void testMethodRefOnGenericTypeArgument() | ||
{ | ||
Function<String, List<String>> fn = func(List::of); | ||
List<String> list = fn.apply("test"); | ||
} | ||
void testMethodRefImportedInJavadoc() | ||
{ | ||
Supplier<Map<String, String>> supplier = Collections::emptyMap; | ||
Map<String, String> list = supplier.get(); | ||
} | ||
|
||
} | ||
|
||
@Retention(RUNTIME) @Target({TYPE_USE}) @interface CJ2 { } | ||
|
||
interface ListGetter2<T> { | ||
List<T> get(); | ||
} |
Oops, something went wrong.