changeset 1182:f85d980faaf8

7074416: Regression: JSR199: javac doesn't unwrap clientcodewrapper objects Reviewed-by: mcimadamore
author jjg
date Wed, 31 Aug 2011 15:39:00 -0700
parents d0257833498e
children 04f983e3e825 edd7d9bd32dd
files src/share/classes/com/sun/tools/javac/api/ClientCodeWrapper.java src/share/classes/javax/tools/JavaCompiler.java test/tools/javac/TryWithResources/UnusedResourcesTest.java test/tools/javac/diags/Example.java test/tools/javac/processing/errors/TestSuppression.java
diffstat 5 files changed, 94 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/com/sun/tools/javac/api/ClientCodeWrapper.java	Wed Aug 31 16:15:19 2011 +0100
+++ b/src/share/classes/com/sun/tools/javac/api/ClientCodeWrapper.java	Wed Aug 31 15:39:00 2011 -0700
@@ -37,6 +37,7 @@
 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.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 @@
             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 @@
         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 @@
         }
     }
 
-    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 @@
         @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 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) {
--- a/src/share/classes/javax/tools/JavaCompiler.java	Wed Aug 31 16:15:19 2011 +0100
+++ b/src/share/classes/javax/tools/JavaCompiler.java	Wed Aug 31 15:39:00 2011 -0700
@@ -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;
--- a/test/tools/javac/TryWithResources/UnusedResourcesTest.java	Wed Aug 31 16:15:19 2011 +0100
+++ b/test/tools/javac/TryWithResources/UnusedResourcesTest.java	Wed Aug 31 15:39:00 2011 -0700
@@ -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 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 @@
                 }
             }
         }
+
+        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();
+        }
     }
 }
--- a/test/tools/javac/diags/Example.java	Wed Aug 31 16:15:19 2011 +0100
+++ b/test/tools/javac/diags/Example.java	Wed Aug 31 15:39:00 2011 -0700
@@ -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 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 @@
 
             if (keys != null) {
                 for (Diagnostic<? extends JavaFileObject> d: dc.getDiagnostics()) {
-                    scanForKeys((JCDiagnostic) d, keys);
+                    scanForKeys(unwrap(d), keys);
                 }
             }
 
@@ -418,6 +419,14 @@
             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();
+        }
     }
 
     /**
--- a/test/tools/javac/processing/errors/TestSuppression.java	Wed Aug 31 16:15:19 2011 +0100
+++ b/test/tools/javac/processing/errors/TestSuppression.java	Wed Aug 31 15:39:00 2011 -0700
@@ -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.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -35,6 +35,7 @@
 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 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 @@
         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("*")