6993311: annotations on packages are not validated

Reviewed-by: mcimadamore
This commit is contained in:
Jonathan Gibbons 2011-03-15 11:04:56 -07:00
parent 8f64aeabe2
commit 13d31713dc
6 changed files with 121 additions and 10 deletions

View File

@ -3004,6 +3004,30 @@ public class Attr extends JCTree.Visitor {
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

View File

@ -1166,7 +1166,7 @@ public class JavaCompiler implements ClassReader.SourceCompleter {
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)

View File

@ -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);
}
}
}

View File

@ -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;

View File

@ -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 @@ public class TestPackageInfo extends JavacTestingAbstractProcessor {
// 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 @@ public class TestPackageInfo extends JavacTestingAbstractProcessor {
} 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();

View File

@ -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;