6993311: annotations on packages are not validated

Tue, 15 Mar 2011 11:04:56 -0700

author
jjg
date
Tue, 15 Mar 2011 11:04:56 -0700
changeset 931
c9432f06d9bc
parent 930
cb119107aeea
child 932
edf03ca74991

6993311: annotations on packages are not validated
Reviewed-by: mcimadamore

src/share/classes/com/sun/tools/javac/comp/Attr.java file | annotate | diff | comparison | revisions
src/share/classes/com/sun/tools/javac/main/JavaCompiler.java file | annotate | diff | comparison | revisions
test/tools/javac/annotations/TestAnnotationPackageInfo.java file | annotate | diff | comparison | revisions
test/tools/javac/annotations/pos/package-info.java file | annotate | diff | comparison | revisions
test/tools/javac/processing/filer/TestPackageInfo.java file | annotate | diff | comparison | revisions
test/tools/javac/processing/filer/foo/bar/package-info.java file | annotate | diff | comparison | revisions
     1.1 --- a/src/share/classes/com/sun/tools/javac/comp/Attr.java	Mon Mar 14 11:48:41 2011 -0700
     1.2 +++ b/src/share/classes/com/sun/tools/javac/comp/Attr.java	Tue Mar 15 11:04:56 2011 -0700
     1.3 @@ -3004,6 +3004,30 @@
     1.4          throw new AssertionError();
     1.5      }
     1.6  
     1.7 +    /**
     1.8 +     * Attribute an env for either a top level tree or class declaration.
     1.9 +     */
    1.10 +    public void attrib(Env<AttrContext> env) {
    1.11 +        if (env.tree.getTag() == JCTree.TOPLEVEL)
    1.12 +            attribTopLevel(env);
    1.13 +        else
    1.14 +            attribClass(env.tree.pos(), env.enclClass.sym);
    1.15 +    }
    1.16 +
    1.17 +    /**
    1.18 +     * Attribute a top level tree. These trees are encountered when the
    1.19 +     * package declaration has annotations.
    1.20 +     */
    1.21 +    public void attribTopLevel(Env<AttrContext> env) {
    1.22 +        JCCompilationUnit toplevel = env.toplevel;
    1.23 +        try {
    1.24 +            annotate.flush();
    1.25 +            chk.validateAnnotations(toplevel.packageAnnotations, toplevel.packge);
    1.26 +        } catch (CompletionFailure ex) {
    1.27 +            chk.completionError(toplevel.pos(), ex);
    1.28 +        }
    1.29 +    }
    1.30 +
    1.31      /** Main method: attribute class definition associated with given class symbol.
    1.32       *  reporting completion failures at the given position.
    1.33       *  @param pos The source position at which completion errors are to be
     2.1 --- a/src/share/classes/com/sun/tools/javac/main/JavaCompiler.java	Mon Mar 14 11:48:41 2011 -0700
     2.2 +++ b/src/share/classes/com/sun/tools/javac/main/JavaCompiler.java	Tue Mar 15 11:04:56 2011 -0700
     2.3 @@ -1166,7 +1166,7 @@
     2.4                                    env.enclClass.sym.sourcefile :
     2.5                                    env.toplevel.sourcefile);
     2.6          try {
     2.7 -            attr.attribClass(env.tree.pos(), env.enclClass.sym);
     2.8 +            attr.attrib(env);
     2.9              if (errorCount() > 0 && !shouldStop(CompileState.ATTR)) {
    2.10                  //if in fail-over mode, ensure that AST expression nodes
    2.11                  //are correctly initialized (e.g. they have a type/symbol)
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/test/tools/javac/annotations/TestAnnotationPackageInfo.java	Tue Mar 15 11:04:56 2011 -0700
     3.3 @@ -0,0 +1,87 @@
     3.4 +/*
     3.5 + * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
     3.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3.7 + *
     3.8 + * This code is free software; you can redistribute it and/or modify it
     3.9 + * under the terms of the GNU General Public License version 2 only, as
    3.10 + * published by the Free Software Foundation.
    3.11 + *
    3.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    3.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    3.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    3.15 + * version 2 for more details (a copy is included in the LICENSE file that
    3.16 + * accompanied this code).
    3.17 + *
    3.18 + * You should have received a copy of the GNU General Public License version
    3.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    3.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    3.21 + *
    3.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    3.23 + * or visit www.oracle.com if you need additional information or have any
    3.24 + * questions.
    3.25 + */
    3.26 +
    3.27 +/*
    3.28 + * @test
    3.29 + * @bug 6993311
    3.30 + * @summary annotations on packages are not validated
    3.31 + */
    3.32 +
    3.33 +import java.io.*;
    3.34 +import java.net.*;
    3.35 +import java.util.*;
    3.36 +import javax.tools.*;
    3.37 +import com.sun.source.util.*;
    3.38 +
    3.39 +public class TestAnnotationPackageInfo {
    3.40 +    public static void main(String... args) throws Exception {
    3.41 +        new TestAnnotationPackageInfo().run();
    3.42 +    }
    3.43 +
    3.44 +    static class MyFileObject extends SimpleJavaFileObject {
    3.45 +        private String text;
    3.46 +        public MyFileObject(String fileName, String text) {
    3.47 +            super(URI.create("myfo:/" + fileName), JavaFileObject.Kind.SOURCE);
    3.48 +            this.text = text;
    3.49 +        }
    3.50 +        @Override
    3.51 +        public CharSequence getCharContent(boolean ignoreEncodingErrors) {
    3.52 +            return text;
    3.53 +        }
    3.54 +    }
    3.55 +
    3.56 +    public void run() throws Exception {
    3.57 +        final String bootPath = System.getProperty("sun.boot.class.path");
    3.58 +        final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
    3.59 +        assert tool != null;
    3.60 +
    3.61 +        JavaFileObject test_java = new MyFileObject("test/Test.java",
    3.62 +            "package test; public @interface Test {\n" +
    3.63 +            "    public int mandatory();\n" +
    3.64 +            "}\n");
    3.65 +
    3.66 +        JavaFileObject package_info_java = new MyFileObject("test/package-info.java",
    3.67 +            "@Test package test;");
    3.68 +
    3.69 +        DiagnosticCollector<JavaFileObject> coll = new DiagnosticCollector<JavaFileObject>();
    3.70 +
    3.71 +        List<String> options = Arrays.asList("-bootclasspath",  bootPath);
    3.72 +        List<? extends JavaFileObject> files = Arrays.asList(test_java, package_info_java);
    3.73 +        JavacTask ct = (JavacTask)tool.getTask(null, null, coll, options, null, files);
    3.74 +        ct.analyze();
    3.75 +
    3.76 +        String expectedCode = "compiler.err.annotation.missing.default.value";
    3.77 +        List<Diagnostic<? extends JavaFileObject>> diags = coll.getDiagnostics();
    3.78 +        switch (diags.size()) {
    3.79 +            case 0:
    3.80 +                throw new Exception("no diagnostics reported");
    3.81 +            case 1:
    3.82 +                String code = diags.get(0).getCode();
    3.83 +                if (code.equals(expectedCode))
    3.84 +                    return;
    3.85 +                throw new Exception("unexpected diag: " + diags.get(0));
    3.86 +            default:
    3.87 +                throw new Exception("unexpected diags reported: " + diags);
    3.88 +        }
    3.89 +    }
    3.90 +}
     4.1 --- a/test/tools/javac/annotations/pos/package-info.java	Mon Mar 14 11:48:41 2011 -0700
     4.2 +++ b/test/tools/javac/annotations/pos/package-info.java	Tue Mar 15 11:04:56 2011 -0700
     4.3 @@ -1,5 +1,5 @@
     4.4  /*
     4.5 - * Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.
     4.6 + * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.
     4.7   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4.8   *
     4.9   * This code is free software; you can redistribute it and/or modify it
    4.10 @@ -23,12 +23,12 @@
    4.11  
    4.12  /*
    4.13   * @test
    4.14 - * @bug 4901290
    4.15 + * @bug 4901290 6993311
    4.16   * @summary Package annotations
    4.17   * @author gafter
    4.18   *
    4.19   * @compile package-info.java
    4.20   */
    4.21  
    4.22 -@java.lang.annotation.Documented
    4.23 +@Deprecated
    4.24  package foo.bar;
     5.1 --- a/test/tools/javac/processing/filer/TestPackageInfo.java	Mon Mar 14 11:48:41 2011 -0700
     5.2 +++ b/test/tools/javac/processing/filer/TestPackageInfo.java	Tue Mar 15 11:04:56 2011 -0700
     5.3 @@ -1,5 +1,5 @@
     5.4  /*
     5.5 - * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
     5.6 + * Copyright (c) 2006, 2011, Oracle and/or its affiliates. All rights reserved.
     5.7   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     5.8   *
     5.9   * This code is free software; you can redistribute it and/or modify it
    5.10 @@ -23,7 +23,7 @@
    5.11  
    5.12  /*
    5.13   * @test
    5.14 - * @bug 6380018 6392177
    5.15 + * @bug 6380018 6392177 6993311
    5.16   * @summary Test the ability to create and process package-info.java files
    5.17   * @author  Joseph D. Darcy
    5.18   * @library ../../lib
    5.19 @@ -60,7 +60,7 @@
    5.20  
    5.21          // Verify annotations are as expected
    5.22          Set<TypeElement> expectedAnnotations = new HashSet<TypeElement>();
    5.23 -        expectedAnnotations.add(eltUtils.getTypeElement("java.lang.SuppressWarnings"));
    5.24 +        expectedAnnotations.add(eltUtils.getTypeElement("java.lang.Deprecated"));
    5.25  
    5.26          if (!roundEnv.processingOver()) {
    5.27              System.out.println("\nRound " + round);
    5.28 @@ -90,7 +90,7 @@
    5.29                      } catch(FilerException fe) {}
    5.30  
    5.31                      PrintWriter pw = new PrintWriter(filer.createSourceFile("foo.package-info").openWriter());
    5.32 -                    pw.println("@SuppressWarnings(\"\")");
    5.33 +                    pw.println("@Deprecated");
    5.34                      pw.println("package foo;");
    5.35                      pw.close();
    5.36  
     6.1 --- a/test/tools/javac/processing/filer/foo/bar/package-info.java	Mon Mar 14 11:48:41 2011 -0700
     6.2 +++ b/test/tools/javac/processing/filer/foo/bar/package-info.java	Tue Mar 15 11:04:56 2011 -0700
     6.3 @@ -1,5 +1,5 @@
     6.4  /*
     6.5 - * Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.
     6.6 + * Copyright (c) 2006, 2011, Oracle and/or its affiliates. All rights reserved.
     6.7   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     6.8   *
     6.9   * This code is free software; you can redistribute it and/or modify it
    6.10 @@ -24,5 +24,5 @@
    6.11  /**
    6.12   * Javadoc for the foo.bar package!
    6.13   */
    6.14 -@SuppressWarnings("deprecation")
    6.15 +@Deprecated
    6.16  package foo.bar;

mercurial