mirror of
https://github.com/jlengrand/error-prone-support.git
synced 2026-03-10 08:11:25 +00:00
More extensible approach
This commit is contained in:
committed by
Gijs de Jong
parent
7175663bed
commit
de091f7a87
@@ -1,15 +1,21 @@
|
||||
package tech.picnic.errorprone.refaster.runner;
|
||||
|
||||
import com.google.common.base.Suppliers;
|
||||
import com.google.common.collect.ImmutableClassToInstanceMap;
|
||||
import com.google.common.collect.ImmutableListMultimap;
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import com.google.common.reflect.ClassPath;
|
||||
import com.google.common.reflect.ClassPath.ResourceInfo;
|
||||
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.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.UncheckedIOException;
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.util.NoSuchElementException;
|
||||
import java.util.Optional;
|
||||
import java.util.function.Supplier;
|
||||
@@ -106,4 +112,44 @@ public final class CodeTransformers {
|
||||
throw new IllegalStateException("Can't load `CodeTransformer` from " + resource, e);
|
||||
}
|
||||
}
|
||||
|
||||
// XXX: Move to separate file?
|
||||
// XXX: Can we find a better class name?
|
||||
private static final class CodeTransformerDescriptionAdapter implements CodeTransformer {
|
||||
private final String name;
|
||||
private final CodeTransformer delegate;
|
||||
|
||||
private CodeTransformerDescriptionAdapter(String name, CodeTransformer delegate) {
|
||||
this.name = name;
|
||||
this.delegate = delegate;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void apply(TreePath path, Context context, DescriptionListener listener) {
|
||||
delegate.apply(
|
||||
path, context, description -> listener.onDescribed(augmentDescription(description)));
|
||||
}
|
||||
|
||||
@Override
|
||||
public ImmutableClassToInstanceMap<Annotation> annotations() {
|
||||
return delegate.annotations();
|
||||
}
|
||||
|
||||
private Description augmentDescription(Description description) {
|
||||
// XXX: Make this configurable based on a Refaster annotation. (E.g. by allowing users to
|
||||
// specify an optional URL pattern.)
|
||||
// XXX: Replace only the first `$`.
|
||||
// XXX: Review URL format. Currently produced format:
|
||||
// https://error-prone.picnic.tech/refastertemplates/OptionalTemplates#OptionalOrElseThrow
|
||||
// XXX: Test this.
|
||||
return Description.builder(
|
||||
description.position,
|
||||
description.checkName,
|
||||
"https://error-prone.picnic.tech/refastertemplates/" + name.replace('$', '#'),
|
||||
description.severity,
|
||||
"Refactoring opportunity")
|
||||
.addAllFixes(description.fixes)
|
||||
.build();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -142,7 +142,7 @@ public final class Refaster extends BugChecker implements CompilationUnitTreeMat
|
||||
ImmutableRangeSet<Integer> ranges = getReplacementRanges(description, endPositions);
|
||||
if (ranges.asRanges().stream().noneMatch(replacedSections::intersects)) {
|
||||
/* This suggested fix does not overlap with any ("larger") replacement seen until now. Apply it. */
|
||||
state.reportMatch(withCustomLink(description));
|
||||
state.reportMatch(description);
|
||||
replacedSections.addAll(ranges);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user