提交 64840665 编写于 作者: J jjg

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

Reviewed-by: mcimadamore
上级 0e0b65f4
......@@ -37,6 +37,7 @@ import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
......@@ -51,6 +52,7 @@ import com.sun.source.util.TaskEvent;
import com.sun.source.util.TaskListener;
import com.sun.tools.javac.util.ClientCodeException;
import com.sun.tools.javac.util.Context;
import com.sun.tools.javac.util.JCDiagnostic;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
......@@ -146,7 +148,7 @@ public class ClientCodeWrapper {
return fo;
}
<T> DiagnosticListener<T> wrap(DiagnosticListener<T> dl) {
<T /*super JavaFileOject*/> DiagnosticListener<T> wrap(DiagnosticListener<T> dl) {
if (isTrusted(dl))
return dl;
return new WrappedDiagnosticListener<T>(dl);
......@@ -158,6 +160,16 @@ public class ClientCodeWrapper {
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) {
Class<?> c = o.getClass();
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;
WrappedDiagnosticListener(DiagnosticListener<T> clientDiagnosticListener) {
clientDiagnosticListener.getClass(); // null check
......@@ -544,7 +556,7 @@ public class ClientCodeWrapper {
@Override
public void report(Diagnostic<? extends T> diagnostic) {
try {
clientDiagnosticListener.report(diagnostic);
clientDiagnosticListener.report(unwrap(diagnostic));
} catch (ClientCodeException e) {
throw 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 TaskListener clientTaskListener;
WrappedTaskListener(TaskListener clientTaskListener) {
......
......@@ -26,10 +26,8 @@
package javax.tools;
import java.io.File;
import java.io.InputStream;
import java.io.Writer;
import java.nio.charset.Charset;
import java.util.List;
import java.util.Locale;
import java.util.concurrent.Callable;
import javax.annotation.processing.Processor;
......
......@@ -28,6 +28,7 @@
*/
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.util.JCDiagnostic;
import java.net.URI;
......@@ -236,7 +237,7 @@ public class UnusedResourcesTest {
public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
if (diagnostic.getKind() == Diagnostic.Kind.WARNING &&
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)) {
unused_r1 = true;
} 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();
}
}
}
......@@ -21,10 +21,12 @@
* questions.
*/
import com.sun.tools.javac.file.JavacFileManager;
import java.io.*;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.*;
import java.util.regex.*;
import javax.annotation.processing.Processor;
import javax.tools.Diagnostic;
import javax.tools.DiagnosticCollector;
import javax.tools.JavaCompiler;
......@@ -37,12 +39,11 @@ import javax.tools.ToolProvider;
// import com.sun.tools.javac.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.JavacMessages;
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.
......@@ -397,7 +398,7 @@ class Example implements Comparable<Example> {
if (keys != null) {
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())
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();
}
}
/**
......
/*
* 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.
*
* 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 com.sun.source.util.JavacTask;
import com.sun.tools.javac.api.ClientCodeWrapper;
import com.sun.tools.javac.api.JavacTool;
import com.sun.tools.javac.util.JCDiagnostic;
......@@ -171,7 +172,7 @@ public class TestSuppression {
public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
System.err.println((++total) + ": "
+ "resolveError:" + isResolveError((JCDiagnostic) diagnostic) + "\n"
+ "resolveError:" + isResolveError(unwrap(diagnostic)) + "\n"
+ diagnostic);
Diagnostic.Kind dk = diagnostic.getKind();
Integer c = counts.get(dk);
......@@ -181,6 +182,14 @@ public class TestSuppression {
private static boolean isResolveError(JCDiagnostic d) {
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("*")
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册