Merge pull request #3662 from jglick/ConsoleAnnotatorFactory

The context type parameter on ConsoleAnnotatorFactory did not work
This commit is contained in:
Jesse Glick authored and GitHub committed 2018-10-05 18:07:43 -04:00
commit f32c0ba012
5 files changed
+28 -19

No files matched your search

@@ -113,7 +113,7 @@ public class AnnotatedLargeText<T> extends LargeText {
rsp.setContentType(isHtml() ? "text/html;charset=UTF-8" : "text/plain;charset=UTF-8");
}
private ConsoleAnnotator createAnnotator(StaplerRequest req) throws IOException {
private ConsoleAnnotator<T> createAnnotator(StaplerRequest req) throws IOException {
try {
String base64 = req!=null ? req.getHeader("X-ConsoleAnnotator") : null;
if (base64!=null) {
@@ -135,7 +135,7 @@ public class AnnotatedLargeText<T> extends LargeText {
throw new IOException(e);
}
// start from scratch
return ConsoleAnnotator.initial(context==null ? null : context.getClass());
return ConsoleAnnotator.initial(context);
}
@Override
@@ -164,7 +164,7 @@ public class AnnotatedLargeText<T> extends LargeText {
}
public long writeHtmlTo(long start, Writer w) throws IOException {
ConsoleAnnotationOutputStream caw = new ConsoleAnnotationOutputStream(
ConsoleAnnotationOutputStream<T> caw = new ConsoleAnnotationOutputStream<>(
w, createAnnotator(Stapler.getCurrentRequest()), context, charset);
long r = super.writeLogTo(start,caw);
@@ -72,7 +72,7 @@ public class ConsoleAnnotationOutputStream<T> extends LineTransformationOutputSt
this.lineOut = new WriterOutputStream(line,charset);
}
public ConsoleAnnotator getConsoleAnnotator() {
public ConsoleAnnotator<T> getConsoleAnnotator() {
return ann;
}
@@ -80,6 +80,8 @@ public class ConsoleAnnotationOutputStream<T> extends LineTransformationOutputSt
* Called after we read the whole line of plain text, which is stored in {@link #buf}.
* This method performs annotations and send the result to {@link #out}.
*/
@SuppressWarnings({"unchecked", "rawtypes"}) // appears to be unsound
@Override
protected void eol(byte[] in, int sz) throws IOException {
line.reset();
final StringBuffer strBuf = line.getStringBuffer();
@@ -109,9 +111,10 @@ public class ConsoleAnnotationOutputStream<T> extends LineTransformationOutputSt
final ConsoleNote a = ConsoleNote.readFrom(new DataInputStream(b));
if (a!=null) {
if (annotators==null)
annotators = new ArrayList<ConsoleAnnotator<T>>();
annotators = new ArrayList<>();
annotators.add(new ConsoleAnnotator<T>() {
public ConsoleAnnotator annotate(T context, MarkupText text) {
@Override
public ConsoleAnnotator<T> annotate(T context, MarkupText text) {
return a.annotate(context,text,charPos);
}
});
@@ -82,22 +82,25 @@ public abstract class ConsoleAnnotator<T> implements Serializable {
* To indicate that you are not interested in the following lines, return {@code null}.
*/
@CheckForNull
public abstract ConsoleAnnotator annotate(@Nonnull T context, @Nonnull MarkupText text );
public abstract ConsoleAnnotator<T> annotate(@Nonnull T context, @Nonnull MarkupText text );
/**
* Cast operation that restricts T.
*/
@SuppressWarnings("unchecked")
public static <T> ConsoleAnnotator<T> cast(ConsoleAnnotator<? super T> a) {
return (ConsoleAnnotator)a;
}
@SuppressWarnings({"unchecked", "rawtypes"}) // unclear to jglick what is going on here
private static final class ConsoleAnnotatorAggregator<T> extends ConsoleAnnotator<T> {
List<ConsoleAnnotator<T>> list;
ConsoleAnnotatorAggregator(Collection list) {
this.list = new ArrayList<ConsoleAnnotator<T>>(list);
this.list = new ArrayList<>(list);
}
@Override
public ConsoleAnnotator annotate(T context, MarkupText text) {
ListIterator<ConsoleAnnotator<T>> itr = list.listIterator();
while (itr.hasNext()) {
@@ -126,7 +129,7 @@ public abstract class ConsoleAnnotator<T> implements Serializable {
case 1: return cast(all.iterator().next()); // just one
}
return new ConsoleAnnotatorAggregator<T>(all);
return new ConsoleAnnotatorAggregator<>(all);
}
/**
@@ -140,8 +143,9 @@ public abstract class ConsoleAnnotator<T> implements Serializable {
/**
* List all the console annotators that can work for the specified context type.
*/
@SuppressWarnings({"unchecked", "rawtypes"}) // reflective
public static <T> List<ConsoleAnnotator<T>> _for(T context) {
List<ConsoleAnnotator<T>> r = new ArrayList<ConsoleAnnotator<T>>();
List<ConsoleAnnotator<T>> r = new ArrayList<>();
for (ConsoleAnnotatorFactory f : ConsoleAnnotatorFactory.all()) {
if (f.type().isInstance(context)) {
ConsoleAnnotator ca = f.newInstance(context);
@@ -80,13 +80,13 @@ public abstract class ConsoleAnnotatorFactory<T> implements ExtensionPoint {
* @return
* null if this factory is not going to participate in the annotation of this console.
*/
public abstract ConsoleAnnotator newInstance(T context);
public abstract ConsoleAnnotator<T> newInstance(T context);
/**
* For which context type does this annotator work?
*/
public Class type() {
Type type = Types.getBaseClass(getClass(), ConsoleAnnotator.class);
public Class<?> type() {
Type type = Types.getBaseClass(getClass(), ConsoleAnnotatorFactory.class);
if (type instanceof ParameterizedType)
return Types.erasure(Types.getTypeArgument(type,0));
else
@@ -105,7 +105,7 @@ public abstract class ConsoleAnnotatorFactory<T> implements ExtensionPoint {
}
private URL getResource(String fileName) {
Class c = getClass();
Class<?> c = getClass();
return c.getClassLoader().getResource(c.getName().replace('.','/').replace('$','/')+ fileName);
}
@@ -125,6 +125,7 @@ public abstract class ConsoleAnnotatorFactory<T> implements ExtensionPoint {
/**
* All the registered instances.
*/
@SuppressWarnings("rawtypes")
public static ExtensionList<ConsoleAnnotatorFactory> all() {
return ExtensionList.lookup(ConsoleAnnotatorFactory.class);
}
@@ -81,16 +81,17 @@ public class ConsoleAnnotatorTest {
* Only annotates the first occurrence of "ooo".
*/
@TestExtension("completedStatelessLogAnnotation")
public static final ConsoleAnnotatorFactory DEMO_ANNOTATOR = new ConsoleAnnotatorFactory() {
public ConsoleAnnotator newInstance(Object context) {
public static final class DemoAnnotatorFactory extends ConsoleAnnotatorFactory<FreeStyleBuild> {
@Override
public ConsoleAnnotator<FreeStyleBuild> newInstance(FreeStyleBuild context) {
return new DemoAnnotator();
}
};
}
public static class DemoAnnotator extends ConsoleAnnotator<Object> {
public static class DemoAnnotator extends ConsoleAnnotator<FreeStyleBuild> {
private static final String ANNOTATE_TEXT = "ooo" + System.getProperty("line.separator");
@Override
public ConsoleAnnotator annotate(Object build, MarkupText text) {
public ConsoleAnnotator<FreeStyleBuild> annotate(FreeStyleBuild build, MarkupText text) {
if (text.getText().equals(ANNOTATE_TEXT)) {
text.addMarkup(0,3,"<b class=demo>","</b>");
return null;