-
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.
Better annotation support, simplify setup
- Loading branch information
1 parent
3b232c3
commit b5d73cb
Showing
11 changed files
with
185 additions
and
141 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
96 changes: 96 additions & 0 deletions
96
...c/main/java/tech/picnic/errorprone/refaster/plugin/AnnotatedCompositeCodeTransformer.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,96 @@ | ||
package tech.picnic.errorprone.refaster.plugin; | ||
|
||
import static com.google.errorprone.BugPattern.SeverityLevel.SUGGESTION; | ||
|
||
import com.google.common.collect.ImmutableClassToInstanceMap; | ||
import com.google.common.collect.ImmutableList; | ||
import com.google.errorprone.BugPattern.SeverityLevel; | ||
import com.google.errorprone.CodeTransformer; | ||
import com.google.errorprone.DescriptionListener; | ||
import com.google.errorprone.matchers.Description; | ||
import com.sun.source.util.TreePath; | ||
import com.sun.tools.javac.util.Context; | ||
import java.io.Serializable; | ||
import java.lang.annotation.Annotation; | ||
import java.util.Optional; | ||
import java.util.function.Function; | ||
import tech.picnic.errorprone.refaster.annotation.OnlineDocumentation; | ||
import tech.picnic.errorprone.refaster.annotation.Severity; | ||
|
||
// XXX: Rename? Use `@AutoValue`? | ||
// XXX: Should the name _also_ be derived from an annotation? Upshot is limited. | ||
// XXX: ^ Name could be dropped if we derive it from `description.checkName`, with the assumption | ||
// that package and class names follow idiomatic Java naming convention. Kinda icky... | ||
final class AnnotatedCompositeCodeTransformer implements CodeTransformer, Serializable { | ||
private static final long serialVersionUID = 1L; | ||
|
||
private final String name; | ||
private final ImmutableList<CodeTransformer> transformers; | ||
private final ImmutableClassToInstanceMap<Annotation> annotations; | ||
|
||
AnnotatedCompositeCodeTransformer( | ||
String name, | ||
ImmutableList<CodeTransformer> transformers, | ||
ImmutableClassToInstanceMap<Annotation> annotations) { | ||
this.name = name; | ||
this.transformers = transformers; | ||
this.annotations = annotations; | ||
} | ||
|
||
@Override | ||
public ImmutableClassToInstanceMap<Annotation> annotations() { | ||
return annotations; | ||
} | ||
|
||
@Override | ||
public void apply(TreePath path, Context context, DescriptionListener listener) { | ||
for (CodeTransformer transformer : transformers) { | ||
transformer.apply( | ||
path, | ||
context, | ||
description -> listener.onDescribed(augmentDescription(description, transformer))); | ||
} | ||
} | ||
|
||
private Description augmentDescription(Description description, CodeTransformer delegate) { | ||
// XXX: Replace only the first `$`. | ||
// XXX: Test this. | ||
return Description.builder( | ||
description.position, | ||
description.checkName, | ||
String.format(getLinkPattern(delegate), name.replace('$', '#')), | ||
getSeverity(delegate), | ||
getDescription(delegate)) | ||
.addAllFixes(description.fixes) | ||
.build(); | ||
} | ||
|
||
private String getLinkPattern(CodeTransformer delegate) { | ||
return getAnnotationValue(OnlineDocumentation.class, OnlineDocumentation::value, delegate, ""); | ||
} | ||
|
||
private SeverityLevel getSeverity(CodeTransformer delegate) { | ||
return getAnnotationValue(Severity.class, Severity::value, delegate, SUGGESTION); | ||
} | ||
|
||
private String getDescription(CodeTransformer delegate) { | ||
return getAnnotationValue( | ||
tech.picnic.errorprone.refaster.annotation.Description.class, | ||
tech.picnic.errorprone.refaster.annotation.Description::value, | ||
delegate, | ||
"Refactoring opportunity"); | ||
} | ||
|
||
private <A extends Annotation, T> T getAnnotationValue( | ||
Class<A> annotation, Function<A, T> extractor, CodeTransformer delegate, T defaultValue) { | ||
return getAnnotationValue(delegate, annotation) | ||
.or(() -> getAnnotationValue(this, annotation)) | ||
.map(extractor) | ||
.orElse(defaultValue); | ||
} | ||
|
||
private static <A extends Annotation> Optional<A> getAnnotationValue( | ||
CodeTransformer codeTransformer, Class<A> annotation) { | ||
return Optional.ofNullable(codeTransformer.annotations().getInstance(annotation)); | ||
} | ||
} |
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
98 changes: 0 additions & 98 deletions
98
...port/src/main/java/tech/picnic/errorprone/refaster/AnnotatedCompositeCodeTransformer.java
This file was deleted.
Oops, something went wrong.
22 changes: 22 additions & 0 deletions
22
refaster-support/src/main/java/tech/picnic/errorprone/refaster/annotation/Description.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,22 @@ | ||
package tech.picnic.errorprone.refaster.annotation; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* Describes the intent of a Refaster template or group of Refaster templates. | ||
* | ||
* <p>Annotations on nested classes override the description associated with any enclosing class. | ||
*/ | ||
@Target(ElementType.TYPE) | ||
@Retention(RetentionPolicy.SOURCE) | ||
public @interface Description { | ||
/** | ||
* A description of the annotated Refaster template(s). | ||
* | ||
* @return A non-{@code null} string. | ||
*/ | ||
String value(); | ||
} |
33 changes: 33 additions & 0 deletions
33
...support/src/main/java/tech/picnic/errorprone/refaster/annotation/OnlineDocumentation.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,33 @@ | ||
package tech.picnic.errorprone.refaster.annotation; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* Signals that a Refaster template or group of Refaster templates comes with online documentation. | ||
* | ||
* <p>The provided value may be a full URL, or a URL pattern containing a single {@code %s} | ||
* placeholder. If a placeholder is present, then it will be replaced with the name of the | ||
* associated Refaster template any time the URL is rendered. | ||
* | ||
* <p>By default it is assumed that the Refaster template(s) are documented on the Error Prone | ||
* Support website. Annotations on nested classes override the documentation URL associated with any | ||
* enclosing class. | ||
*/ | ||
// XXX: Is the `%s` protocol sufficiently generic for non-Picnic use cases? | ||
// XXX: The documentation is misleading, in that the generated anchor isn't mentioned. | ||
@Target(ElementType.TYPE) | ||
@Retention(RetentionPolicy.SOURCE) | ||
public @interface OnlineDocumentation { | ||
/** | ||
* The URL or URL pattern of the website at which the annotated Refaster template(s) are | ||
* documented. | ||
* | ||
* @return A non-{@code null} string. | ||
*/ | ||
// XXX: This default is Error Prone Support-specific. Appropriate? (The alternative is to repeat | ||
// this URL pattern in many places.) If we drop this, also update the class documentation. | ||
String value() default "https://error-prone.picnic.tech/refastertemplates/%s"; | ||
} |
24 changes: 24 additions & 0 deletions
24
refaster-support/src/main/java/tech/picnic/errorprone/refaster/annotation/Severity.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,24 @@ | ||
package tech.picnic.errorprone.refaster.annotation; | ||
|
||
import com.google.errorprone.BugPattern.SeverityLevel; | ||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* Describes the severity of a Refaster template or group of Refaster templates. | ||
* | ||
* <p>The default severity is {@link SeverityLevel#SUGGESTION}. Annotations on nested classes | ||
* override the severity associated with any enclosing class. | ||
*/ | ||
@Target(ElementType.TYPE) | ||
@Retention(RetentionPolicy.SOURCE) | ||
public @interface Severity { | ||
/** | ||
* The expected severity of any match of the annotated Refaster template(s). | ||
* | ||
* @return An Error Prone severity level. | ||
*/ | ||
SeverityLevel value(); | ||
} |
24 changes: 0 additions & 24 deletions
24
...-support/src/main/java/tech/picnic/errorprone/refaster/annotation/TemplateCollection.java
This file was deleted.
Oops, something went wrong.
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
10 changes: 5 additions & 5 deletions
10
...pport/src/test/java/tech/picnic/errorprone/refaster/test/MatchInWrongMethodTemplates.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