changeset 1003:c9432f06d9bc

6993311: annotations on packages are not validated Reviewed-by: mcimadamore
author jjg
date Tue, 15 Mar 2011 11:04:56 -0700
parents cb119107aeea
children edf03ca74991
files src/share/classes/com/sun/tools/javac/comp/Attr.java src/share/classes/com/sun/tools/javac/main/JavaCompiler.java test/tools/javac/annotations/TestAnnotationPackageInfo.java test/tools/javac/annotations/pos/package-info.java test/tools/javac/processing/filer/TestPackageInfo.java test/tools/javac/processing/filer/foo/bar/package-info.java
diffstat 6 files changed, 121 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/com/sun/tools/javac/comp/Attr.java	Mon Mar 14 11:48:41 2011 -0700
+++ b/src/share/classes/com/sun/tools/javac/comp/Attr.java	Tue Mar 15 11:04:56 2011 -0700
@@ -3004,6 +3004,30 @@
         throw new AssertionError();
     }
 
+    /**
+     * Attribute an env for either a top level tree or class declaration.
+     */
+    public void attrib(Env<AttrContext> env) {
+        if (env.tree.getTag() == JCTree.TOPLEVEL)
+            attribTopLevel(env);
+        else
+            attribClass(env.tree.pos(), env.enclClass.sym);
+    }
+
+    /**
+     * Attribute a top level tree. These trees are encountered when the
+     * package declaration has annotations.
+     */
+    public void attribTopLevel(Env<AttrContext> env) {
+        JCCompilationUnit toplevel = env.toplevel;
+        try {
+            annotate.flush();
+            chk.validateAnnotations(toplevel.packageAnnotations, toplevel.packge);
+        } catch (CompletionFailure ex) {
+            chk.completionError(toplevel.pos(), ex);
+        }
+    }
+
     /** Main method: attribute class definition associated with given class symbol.
      *  reporting completion failures at the given position.
      *  @param pos The source position at which completion errors are to be
--- a/src/share/classes/com/sun/tools/javac/main/JavaCompiler.java	Mon Mar 14 11:48:41 2011 -0700
+++ b/src/share/classes/com/sun/tools/javac/main/JavaCompiler.java	Tue Mar 15 11:04:56 2011 -0700
@@ -1166,7 +1166,7 @@
                                   env.enclClass.sym.sourcefile :
                                   env.toplevel.sourcefile);
         try {
-            attr.attribClass(env.tree.pos(), env.enclClass.sym);
+            attr.attrib(env);
             if (errorCount() > 0 && !shouldStop(CompileState.ATTR)) {
                 //if in fail-over mode, ensure that AST expression nodes
                 //are correctly initialized (e.g. they have a type/symbol)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/tools/javac/annotations/TestAnnotationPackageInfo.java	Tue Mar 15 11:04:56 2011 -0700
@@ -0,0 +1,87 @@
+/*
+ * Copyright (c) 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
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/*
+ * @test
+ * @bug 6993311
+ * @summary annotations on packages are not validated
+ */
+
+import java.io.*;
+import java.net.*;
+import java.util.*;
+import javax.tools.*;
+import com.sun.source.util.*;
+
+public class TestAnnotationPackageInfo {
+    public static void main(String... args) throws Exception {
+        new TestAnnotationPackageInfo().run();
+    }
+
+    static class MyFileObject extends SimpleJavaFileObject {
+        private String text;
+        public MyFileObject(String fileName, String text) {
+            super(URI.create("myfo:/" + fileName), JavaFileObject.Kind.SOURCE);
+            this.text = text;
+        }
+        @Override
+        public CharSequence getCharContent(boolean ignoreEncodingErrors) {
+            return text;
+        }
+    }
+
+    public void run() throws Exception {
+        final String bootPath = System.getProperty("sun.boot.class.path");
+        final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
+        assert tool != null;
+
+        JavaFileObject test_java = new MyFileObject("test/Test.java",
+            "package test; public @interface Test {\n" +
+            "    public int mandatory();\n" +
+            "}\n");
+
+        JavaFileObject package_info_java = new MyFileObject("test/package-info.java",
+            "@Test package test;");
+
+        DiagnosticCollector<JavaFileObject> coll = new DiagnosticCollector<JavaFileObject>();
+
+        List<String> options = Arrays.asList("-bootclasspath",  bootPath);
+        List<? extends JavaFileObject> files = Arrays.asList(test_java, package_info_java);
+        JavacTask ct = (JavacTask)tool.getTask(null, null, coll, options, null, files);
+        ct.analyze();
+
+        String expectedCode = "compiler.err.annotation.missing.default.value";
+        List<Diagnostic<? extends JavaFileObject>> diags = coll.getDiagnostics();
+        switch (diags.size()) {
+            case 0:
+                throw new Exception("no diagnostics reported");
+            case 1:
+                String code = diags.get(0).getCode();
+                if (code.equals(expectedCode))
+                    return;
+                throw new Exception("unexpected diag: " + diags.get(0));
+            default:
+                throw new Exception("unexpected diags reported: " + diags);
+        }
+    }
+}
--- a/test/tools/javac/annotations/pos/package-info.java	Mon Mar 14 11:48:41 2011 -0700
+++ b/test/tools/javac/annotations/pos/package-info.java	Tue Mar 15 11:04:56 2011 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2003, 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
@@ -23,12 +23,12 @@
 
 /*
  * @test
- * @bug 4901290
+ * @bug 4901290 6993311
  * @summary Package annotations
  * @author gafter
  *
  * @compile package-info.java
  */
 
-@java.lang.annotation.Documented
+@Deprecated
 package foo.bar;
--- a/test/tools/javac/processing/filer/TestPackageInfo.java	Mon Mar 14 11:48:41 2011 -0700
+++ b/test/tools/javac/processing/filer/TestPackageInfo.java	Tue Mar 15 11:04:56 2011 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2006, 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
@@ -23,7 +23,7 @@
 
 /*
  * @test
- * @bug 6380018 6392177
+ * @bug 6380018 6392177 6993311
  * @summary Test the ability to create and process package-info.java files
  * @author  Joseph D. Darcy
  * @library ../../lib
@@ -60,7 +60,7 @@
 
         // Verify annotations are as expected
         Set<TypeElement> expectedAnnotations = new HashSet<TypeElement>();
-        expectedAnnotations.add(eltUtils.getTypeElement("java.lang.SuppressWarnings"));
+        expectedAnnotations.add(eltUtils.getTypeElement("java.lang.Deprecated"));
 
         if (!roundEnv.processingOver()) {
             System.out.println("\nRound " + round);
@@ -90,7 +90,7 @@
                     } catch(FilerException fe) {}
 
                     PrintWriter pw = new PrintWriter(filer.createSourceFile("foo.package-info").openWriter());
-                    pw.println("@SuppressWarnings(\"\")");
+                    pw.println("@Deprecated");
                     pw.println("package foo;");
                     pw.close();
 
--- a/test/tools/javac/processing/filer/foo/bar/package-info.java	Mon Mar 14 11:48:41 2011 -0700
+++ b/test/tools/javac/processing/filer/foo/bar/package-info.java	Tue Mar 15 11:04:56 2011 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2006, 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
@@ -24,5 +24,5 @@
 /**
  * Javadoc for the foo.bar package!
  */
-@SuppressWarnings("deprecation")
+@Deprecated
 package foo.bar;