-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce
ClassRules
Refaster rule collection
- Loading branch information
1 parent
ffe4db2
commit f0a9de4
Showing
3 changed files
with
115 additions
and
0 deletions.
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
65
error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/ClassRules.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,65 @@ | ||
package tech.picnic.errorprone.refasterrules; | ||
|
||
import com.google.errorprone.refaster.Refaster; | ||
import com.google.errorprone.refaster.annotation.AfterTemplate; | ||
import com.google.errorprone.refaster.annotation.BeforeTemplate; | ||
import java.util.function.Predicate; | ||
import tech.picnic.errorprone.refaster.annotation.OnlineDocumentation; | ||
|
||
/** Refaster rules related to expressions dealing with classes. */ | ||
@OnlineDocumentation | ||
final class ClassRules { | ||
private ClassRules() {} | ||
|
||
/** Prefer {@link Class#isInstance(Object)} over more contrived alternatives. */ | ||
static final class ClassIsInstance<T, S> { | ||
@BeforeTemplate | ||
boolean before(Class<T> clazz, S object) { | ||
return clazz.isAssignableFrom(object.getClass()); | ||
} | ||
|
||
@AfterTemplate | ||
boolean after(Class<T> clazz, S object) { | ||
return clazz.isInstance(object); | ||
} | ||
} | ||
|
||
/** Prefer using the {@code instanceof} keyword over less idiomatic alternatives. */ | ||
static final class Instanceof<T, S> { | ||
@BeforeTemplate | ||
boolean before(S object) { | ||
return Refaster.<T>clazz().isInstance(object); | ||
} | ||
|
||
@AfterTemplate | ||
boolean after(S object) { | ||
return Refaster.<T>isInstance(object); | ||
} | ||
} | ||
|
||
/** Prefer {@link Class#isInstance(Object)} method references over more verbose alternatives. */ | ||
static final class ClassLiteralIsInstancePredicate<T, S> { | ||
@BeforeTemplate | ||
Predicate<S> before() { | ||
return o -> Refaster.<T>isInstance(o); | ||
} | ||
|
||
@AfterTemplate | ||
Predicate<S> after() { | ||
return Refaster.<T>clazz()::isInstance; | ||
} | ||
} | ||
|
||
/** Prefer {@link Class#isInstance(Object)} method references over more verbose alternatives. */ | ||
static final class ClassReferenceIsInstancePredicate<T, S> { | ||
@BeforeTemplate | ||
Predicate<S> before(Class<T> clazz) { | ||
return o -> clazz.isInstance(o); | ||
} | ||
|
||
@AfterTemplate | ||
Predicate<S> after(Class<T> clazz) { | ||
return clazz::isInstance; | ||
} | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
...-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ClassRulesTestInput.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,25 @@ | ||
package tech.picnic.errorprone.refasterrules; | ||
|
||
import com.google.common.collect.ImmutableSet; | ||
import java.io.IOException; | ||
import tech.picnic.errorprone.refaster.test.RefasterRuleCollectionTestCase; | ||
|
||
final class ClassRulesTest implements RefasterRuleCollectionTestCase { | ||
boolean testClassIsInstance() throws IOException { | ||
return CharSequence.class.isAssignableFrom("foo".getClass()); | ||
} | ||
|
||
ImmutableSet<Boolean> testInstanceof() throws IOException { | ||
Class<?> clazz = CharSequence.class; | ||
return ImmutableSet.of(CharSequence.class.isInstance("foo"), clazz.isInstance("bar")); | ||
} | ||
|
||
Predicate<String> testClassLiteralIsInstancePredicate() throws IOException { | ||
return s -> s instanceof CharSequence; | ||
} | ||
|
||
Predicate<String> testClassReferenceIsInstancePredicate() throws IOException { | ||
Class<?> clazz = CharSequence.class; | ||
return s -> clazz.isInstance(s); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
...contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ClassRulesTestOutput.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,25 @@ | ||
package tech.picnic.errorprone.refasterrules; | ||
|
||
import com.google.common.collect.ImmutableSet; | ||
import java.io.IOException; | ||
import tech.picnic.errorprone.refaster.test.RefasterRuleCollectionTestCase; | ||
|
||
final class ClassRulesTest implements RefasterRuleCollectionTestCase { | ||
boolean testClassIsInstance() throws IOException { | ||
return CharSequence.class.isInstance("foo"); | ||
} | ||
|
||
ImmutableSet<Boolean> testInstanceof() throws IOException { | ||
Class<?> clazz = CharSequence.class; | ||
return ImmutableSet.of("foo" instanceof CharSequence, clazz.isInstance("bar")); | ||
} | ||
|
||
Predicate<String> testClassLiteralIsInstancePredicate() throws IOException { | ||
return CharSequence.class::isInstance; | ||
} | ||
|
||
Predicate<String> testClassReferenceIsInstancePredicate() throws IOException { | ||
Class<?> clazz = CharSequence.class; | ||
return clazz::isInstance; | ||
} | ||
} |