7074416: Regression: JSR199: javac doesn't unwrap clientcodewrapper objects

Reviewed-by: mcimadamore
This commit is contained in:
Jonathan Gibbons 2011-08-31 15:39:00 -07:00
parent 67f3781cb1
commit 654ae83d8e
5 changed files with 94 additions and 13 deletions

View File

@ -37,6 +37,7 @@ import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
@ -51,6 +52,7 @@ import com.sun.source.util.TaskEvent;
import com.sun.source.util.TaskListener; import com.sun.source.util.TaskListener;
import com.sun.tools.javac.util.ClientCodeException; import com.sun.tools.javac.util.ClientCodeException;
import com.sun.tools.javac.util.Context; import com.sun.tools.javac.util.Context;
import com.sun.tools.javac.util.JCDiagnostic;
import java.lang.annotation.ElementType; import java.lang.annotation.ElementType;
import java.lang.annotation.Retention; import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy; import java.lang.annotation.RetentionPolicy;
@ -146,7 +148,7 @@ public class ClientCodeWrapper {
return fo; return fo;
} }
<T> DiagnosticListener<T> wrap(DiagnosticListener<T> dl) { <T /*super JavaFileOject*/> DiagnosticListener<T> wrap(DiagnosticListener<T> dl) {
if (isTrusted(dl)) if (isTrusted(dl))
return dl; return dl;
return new WrappedDiagnosticListener<T>(dl); return new WrappedDiagnosticListener<T>(dl);
@ -158,6 +160,16 @@ public class ClientCodeWrapper {
return new WrappedTaskListener(tl); return new WrappedTaskListener(tl);
} }
@SuppressWarnings("unchecked")
private <T> Diagnostic<T> unwrap(final Diagnostic<T> diagnostic) {
if (diagnostic instanceof JCDiagnostic) {
JCDiagnostic d = (JCDiagnostic) diagnostic;
return (Diagnostic<T>) new DiagnosticSourceUnwrapper(d);
} else {
return diagnostic;
}
}
protected boolean isTrusted(Object o) { protected boolean isTrusted(Object o) {
Class<?> c = o.getClass(); Class<?> c = o.getClass();
Boolean trusted = trustedClasses.get(c); Boolean trusted = trustedClasses.get(c);
@ -534,7 +546,7 @@ public class ClientCodeWrapper {
} }
} }
protected class WrappedDiagnosticListener<T> implements DiagnosticListener<T> { protected class WrappedDiagnosticListener<T /*super JavaFileObject*/> implements DiagnosticListener<T> {
protected DiagnosticListener<T> clientDiagnosticListener; protected DiagnosticListener<T> clientDiagnosticListener;
WrappedDiagnosticListener(DiagnosticListener<T> clientDiagnosticListener) { WrappedDiagnosticListener(DiagnosticListener<T> clientDiagnosticListener) {
clientDiagnosticListener.getClass(); // null check clientDiagnosticListener.getClass(); // null check
@ -544,7 +556,7 @@ public class ClientCodeWrapper {
@Override @Override
public void report(Diagnostic<? extends T> diagnostic) { public void report(Diagnostic<? extends T> diagnostic) {
try { try {
clientDiagnosticListener.report(diagnostic); clientDiagnosticListener.report(unwrap(diagnostic));
} catch (ClientCodeException e) { } catch (ClientCodeException e) {
throw e; throw e;
} catch (RuntimeException e) { } catch (RuntimeException e) {
@ -555,6 +567,50 @@ public class ClientCodeWrapper {
} }
} }
public class DiagnosticSourceUnwrapper implements Diagnostic<JavaFileObject> {
public final JCDiagnostic d;
DiagnosticSourceUnwrapper(JCDiagnostic d) {
this.d = d;
}
public Diagnostic.Kind getKind() {
return d.getKind();
}
public JavaFileObject getSource() {
return unwrap(d.getSource());
}
public long getPosition() {
return d.getPosition();
}
public long getStartPosition() {
return d.getStartPosition();
}
public long getEndPosition() {
return d.getEndPosition();
}
public long getLineNumber() {
return d.getLineNumber();
}
public long getColumnNumber() {
return d.getColumnNumber();
}
public String getCode() {
return d.getCode();
}
public String getMessage(Locale locale) {
return d.getMessage(locale);
}
}
protected class WrappedTaskListener implements TaskListener { protected class WrappedTaskListener implements TaskListener {
protected TaskListener clientTaskListener; protected TaskListener clientTaskListener;
WrappedTaskListener(TaskListener clientTaskListener) { WrappedTaskListener(TaskListener clientTaskListener) {

View File

@ -26,10 +26,8 @@
package javax.tools; package javax.tools;
import java.io.File; import java.io.File;
import java.io.InputStream;
import java.io.Writer; import java.io.Writer;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.concurrent.Callable; import java.util.concurrent.Callable;
import javax.annotation.processing.Processor; import javax.annotation.processing.Processor;

View File

@ -28,6 +28,7 @@
*/ */
import com.sun.source.util.JavacTask; import com.sun.source.util.JavacTask;
import com.sun.tools.javac.api.ClientCodeWrapper;
import com.sun.tools.javac.api.JavacTool; import com.sun.tools.javac.api.JavacTool;
import com.sun.tools.javac.util.JCDiagnostic; import com.sun.tools.javac.util.JCDiagnostic;
import java.net.URI; import java.net.URI;
@ -236,7 +237,7 @@ public class UnusedResourcesTest {
public void report(Diagnostic<? extends JavaFileObject> diagnostic) { public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
if (diagnostic.getKind() == Diagnostic.Kind.WARNING && if (diagnostic.getKind() == Diagnostic.Kind.WARNING &&
diagnostic.getCode().contains("try.resource.not.referenced")) { diagnostic.getCode().contains("try.resource.not.referenced")) {
String varName = ((JCDiagnostic)diagnostic).getArgs()[0].toString(); String varName = unwrap(diagnostic).getArgs()[0].toString();
if (varName.equals(TwrStmt.TWR1.resourceName)) { if (varName.equals(TwrStmt.TWR1.resourceName)) {
unused_r1 = true; unused_r1 = true;
} else if (varName.equals(TwrStmt.TWR2.resourceName)) { } else if (varName.equals(TwrStmt.TWR2.resourceName)) {
@ -246,5 +247,13 @@ public class UnusedResourcesTest {
} }
} }
} }
private JCDiagnostic unwrap(Diagnostic<? extends JavaFileObject> diagnostic) {
if (diagnostic instanceof JCDiagnostic)
return (JCDiagnostic) diagnostic;
if (diagnostic instanceof ClientCodeWrapper.DiagnosticSourceUnwrapper)
return ((ClientCodeWrapper.DiagnosticSourceUnwrapper)diagnostic).d;
throw new IllegalArgumentException();
}
} }
} }

View File

@ -21,10 +21,12 @@
* questions. * questions.
*/ */
import com.sun.tools.javac.file.JavacFileManager;
import java.io.*; import java.io.*;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.*; import java.util.*;
import java.util.regex.*; import java.util.regex.*;
import javax.annotation.processing.Processor;
import javax.tools.Diagnostic; import javax.tools.Diagnostic;
import javax.tools.DiagnosticCollector; import javax.tools.DiagnosticCollector;
import javax.tools.JavaCompiler; import javax.tools.JavaCompiler;
@ -37,12 +39,11 @@ import javax.tools.ToolProvider;
// import com.sun.tools.javac.Main // import com.sun.tools.javac.Main
// import com.sun.tools.javac.main.Main // import com.sun.tools.javac.main.Main
import com.sun.tools.javac.api.ClientCodeWrapper;
import com.sun.tools.javac.file.JavacFileManager;
import com.sun.tools.javac.util.Context; import com.sun.tools.javac.util.Context;
import com.sun.tools.javac.util.JavacMessages; import com.sun.tools.javac.util.JavacMessages;
import com.sun.tools.javac.util.JCDiagnostic; import com.sun.tools.javac.util.JCDiagnostic;
import java.net.URL;
import java.net.URLClassLoader;
import javax.annotation.processing.Processor;
/** /**
* Class to handle example code designed to illustrate javac diagnostic messages. * Class to handle example code designed to illustrate javac diagnostic messages.
@ -397,7 +398,7 @@ class Example implements Comparable<Example> {
if (keys != null) { if (keys != null) {
for (Diagnostic<? extends JavaFileObject> d: dc.getDiagnostics()) { for (Diagnostic<? extends JavaFileObject> d: dc.getDiagnostics()) {
scanForKeys((JCDiagnostic) d, keys); scanForKeys(unwrap(d), keys);
} }
} }
@ -418,6 +419,14 @@ class Example implements Comparable<Example> {
for (JCDiagnostic sd: d.getSubdiagnostics()) for (JCDiagnostic sd: d.getSubdiagnostics())
scanForKeys(sd, keys); scanForKeys(sd, keys);
} }
private JCDiagnostic unwrap(Diagnostic<? extends JavaFileObject> diagnostic) {
if (diagnostic instanceof JCDiagnostic)
return (JCDiagnostic) diagnostic;
if (diagnostic instanceof ClientCodeWrapper.DiagnosticSourceUnwrapper)
return ((ClientCodeWrapper.DiagnosticSourceUnwrapper)diagnostic).d;
throw new IllegalArgumentException();
}
} }
/** /**

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -35,6 +35,7 @@ import javax.lang.model.element.TypeElement;
import javax.tools.*; import javax.tools.*;
import com.sun.source.util.JavacTask; import com.sun.source.util.JavacTask;
import com.sun.tools.javac.api.ClientCodeWrapper;
import com.sun.tools.javac.api.JavacTool; import com.sun.tools.javac.api.JavacTool;
import com.sun.tools.javac.util.JCDiagnostic; import com.sun.tools.javac.util.JCDiagnostic;
@ -171,7 +172,7 @@ public class TestSuppression {
public void report(Diagnostic<? extends JavaFileObject> diagnostic) { public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
System.err.println((++total) + ": " System.err.println((++total) + ": "
+ "resolveError:" + isResolveError((JCDiagnostic) diagnostic) + "\n" + "resolveError:" + isResolveError(unwrap(diagnostic)) + "\n"
+ diagnostic); + diagnostic);
Diagnostic.Kind dk = diagnostic.getKind(); Diagnostic.Kind dk = diagnostic.getKind();
Integer c = counts.get(dk); Integer c = counts.get(dk);
@ -181,6 +182,14 @@ public class TestSuppression {
private static boolean isResolveError(JCDiagnostic d) { private static boolean isResolveError(JCDiagnostic d) {
return d.isFlagSet(RESOLVE_ERROR); return d.isFlagSet(RESOLVE_ERROR);
} }
private JCDiagnostic unwrap(Diagnostic<? extends JavaFileObject> diagnostic) {
if (diagnostic instanceof JCDiagnostic)
return (JCDiagnostic) diagnostic;
if (diagnostic instanceof ClientCodeWrapper.DiagnosticSourceUnwrapper)
return ((ClientCodeWrapper.DiagnosticSourceUnwrapper)diagnostic).d;
throw new IllegalArgumentException();
}
} }
@SupportedAnnotationTypes("*") @SupportedAnnotationTypes("*")