-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Provide possible assignment solutions
- Loading branch information
1 parent
c732711
commit 2668359
Showing
19 changed files
with
137 additions
and
77 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
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
15 changes: 14 additions & 1 deletion
15
...src/main/java/tech/picnic/errorprone/workshop/refasterrules/WorkshopAssignment0Rules.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 |
---|---|---|
@@ -1,9 +1,22 @@ | ||
package tech.picnic.errorprone.workshop.refasterrules; | ||
|
||
import com.google.errorprone.refaster.annotation.AfterTemplate; | ||
import com.google.errorprone.refaster.annotation.BeforeTemplate; | ||
|
||
/** Refaster rule used as example for the assignments of the workshop. */ | ||
final class WorkshopAssignment0Rules { | ||
private WorkshopAssignment0Rules() {} | ||
|
||
/** Prefer {@link String#isEmpty()} over alternatives that consult the string's length. */ | ||
static final class ExampleStringIsEmpty {} | ||
static final class ExampleStringIsEmpty { | ||
@BeforeTemplate | ||
boolean before(String s) { | ||
return s.length() == 0; | ||
} | ||
|
||
@AfterTemplate | ||
boolean after(String s) { | ||
return s.isEmpty(); | ||
} | ||
} | ||
} |
13 changes: 12 additions & 1 deletion
13
...src/main/java/tech/picnic/errorprone/workshop/refasterrules/WorkshopAssignment1Rules.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 |
---|---|---|
@@ -1,11 +1,22 @@ | ||
package tech.picnic.errorprone.workshop.refasterrules; | ||
|
||
import com.google.errorprone.refaster.annotation.AfterTemplate; | ||
import com.google.errorprone.refaster.annotation.BeforeTemplate; | ||
|
||
/** Refaster rules for the first assignment of the workshop. */ | ||
final class WorkshopAssignment1Rules { | ||
private WorkshopAssignment1Rules() {} | ||
|
||
/** Prefer {@link String#String(char[])} over {@link String#copyValueOf(char[])}. */ | ||
static final class NewStringCharArray { | ||
// XXX: Implement this Refaster rule. | ||
@BeforeTemplate | ||
String before(char[] chars) { | ||
return String.copyValueOf(chars); | ||
} | ||
|
||
@AfterTemplate | ||
String after(char[] chars) { | ||
return new String(chars); | ||
} | ||
} | ||
} |
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
35 changes: 30 additions & 5 deletions
35
...src/main/java/tech/picnic/errorprone/workshop/refasterrules/WorkshopAssignment3Rules.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 |
---|---|---|
@@ -1,21 +1,46 @@ | ||
package tech.picnic.errorprone.workshop.refasterrules; | ||
|
||
import static com.google.common.base.Preconditions.checkArgument; | ||
import static com.google.errorprone.refaster.ImportPolicy.STATIC_IMPORT_ALWAYS; | ||
|
||
import com.google.common.base.Preconditions; | ||
import com.google.errorprone.refaster.annotation.AfterTemplate; | ||
import com.google.errorprone.refaster.annotation.BeforeTemplate; | ||
import com.google.errorprone.refaster.annotation.UseImportPolicy; | ||
|
||
/** Refaster rules for the third assignment of the workshop. */ | ||
@SuppressWarnings("UnusedTypeParameter" /* Ignore this for demo purposes. */) | ||
final class WorkshopAssignment3Rules { | ||
private WorkshopAssignment3Rules() {} | ||
|
||
// XXX: Tip: check the input and output files to see the *expected* refactoring. | ||
|
||
/** Prefer {@link Preconditions#checkArgument(boolean)} over if statements. */ | ||
static final class CheckArgumentWithoutMessage { | ||
// XXX: Implement the Refaster rule to get the test green. | ||
@BeforeTemplate | ||
void before(boolean expression) { | ||
if (expression) { | ||
throw new IllegalArgumentException(); | ||
} | ||
} | ||
|
||
@AfterTemplate | ||
@UseImportPolicy(STATIC_IMPORT_ALWAYS) | ||
void after(boolean expression) { | ||
checkArgument(!expression); | ||
} | ||
} | ||
|
||
/** Prefer {@link Preconditions#checkArgument(boolean, Object)} over if statements. */ | ||
static final class CheckArgumentWithMessage { | ||
// XXX: Implement the Refaster rule to get the test green. | ||
@BeforeTemplate | ||
void before(boolean expression, String message) { | ||
if (expression) { | ||
throw new IllegalArgumentException(message); | ||
} | ||
} | ||
|
||
@AfterTemplate | ||
@UseImportPolicy(STATIC_IMPORT_ALWAYS) | ||
void after(boolean expression, String message) { | ||
checkArgument(!expression, message); | ||
} | ||
} | ||
} |
33 changes: 17 additions & 16 deletions
33
...src/main/java/tech/picnic/errorprone/workshop/refasterrules/WorkshopAssignment4Rules.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 |
---|---|---|
@@ -1,25 +1,26 @@ | ||
package tech.picnic.errorprone.workshop.refasterrules; | ||
|
||
import com.google.errorprone.refaster.Refaster; | ||
import com.google.errorprone.refaster.annotation.AfterTemplate; | ||
import com.google.errorprone.refaster.annotation.AlsoNegation; | ||
import com.google.errorprone.refaster.annotation.BeforeTemplate; | ||
import java.util.Objects; | ||
|
||
/** Refaster rules for the fourth assignment of the workshop. */ | ||
@SuppressWarnings("java:S1698" /* Reference comparison is valid for enums. */) | ||
final class WorkshopAssignment4Rules { | ||
private WorkshopAssignment4Rules() {} | ||
|
||
// The test fails because non Enum comparisons are also rewritten. | ||
// Fix the test by tweaking the type parameters. | ||
|
||
// XXX: Get the test to pass by improving the Refaster rule (uncommented it first). | ||
static final class PrimitiveOrReferenceEqualityEnum<T extends Enum<T>> { | ||
@BeforeTemplate | ||
boolean before(T a, T b) { | ||
return Refaster.anyOf(a.equals(b), Objects.equals(a, b)); | ||
} | ||
|
||
// static final class PrimitiveOrReferenceEqualityEnum<T> { | ||
// @BeforeTemplate | ||
// boolean before(T a, T b) { | ||
// return Refaster.anyOf(a.equals(b), Objects.equals(a, b)); | ||
// } | ||
// | ||
// @AfterTemplate | ||
// @AlsoNegation | ||
// boolean after(T a, T b) { | ||
// return a == b; | ||
// } | ||
// } | ||
@AfterTemplate | ||
@AlsoNegation | ||
boolean after(T a, T b) { | ||
return a == b; | ||
} | ||
} | ||
} |
21 changes: 18 additions & 3 deletions
21
...src/main/java/tech/picnic/errorprone/workshop/refasterrules/WorkshopAssignment5Rules.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 |
---|---|---|
@@ -1,12 +1,27 @@ | ||
package tech.picnic.errorprone.workshop.refasterrules; | ||
|
||
import com.google.errorprone.refaster.annotation.AfterTemplate; | ||
import com.google.errorprone.refaster.annotation.BeforeTemplate; | ||
import com.google.errorprone.refaster.annotation.MayOptionallyUse; | ||
import com.google.errorprone.refaster.annotation.Placeholder; | ||
import java.util.stream.Stream; | ||
|
||
/** Refaster rules for the fifth assignment of the workshop. */ | ||
@SuppressWarnings("UnusedTypeParameter" /* Ignore this for demo purposes. */) | ||
final class WorkshopAssignment5Rules { | ||
private WorkshopAssignment5Rules() {} | ||
|
||
abstract static class StreamDoAllMatch<T> { | ||
// XXX: Implement the Refaster rule to get the test green. | ||
// Tip: use the `@Placeholder` annotation. | ||
@Placeholder | ||
abstract boolean test(@MayOptionallyUse T element); | ||
|
||
@BeforeTemplate | ||
boolean before(Stream<T> stream) { | ||
return stream.noneMatch(v -> !test(v)); | ||
} | ||
|
||
@AfterTemplate | ||
boolean after(Stream<T> stream) { | ||
return stream.allMatch(v -> test(v)); | ||
} | ||
} | ||
} |
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
Oops, something went wrong.