Mon, 21 Jan 2013 11:16:28 -0800
Merge
1 /*
2 * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
24 /*
25 * @test
26 * @bug 6993311
27 * @summary annotations on packages are not validated
28 */
30 import java.io.*;
31 import java.net.*;
32 import java.util.*;
33 import javax.tools.*;
34 import com.sun.source.util.*;
36 public class TestAnnotationPackageInfo {
37 public static void main(String... args) throws Exception {
38 new TestAnnotationPackageInfo().run();
39 }
41 static class MyFileObject extends SimpleJavaFileObject {
42 private String text;
43 public MyFileObject(String fileName, String text) {
44 super(URI.create("myfo:/" + fileName), JavaFileObject.Kind.SOURCE);
45 this.text = text;
46 }
47 @Override
48 public CharSequence getCharContent(boolean ignoreEncodingErrors) {
49 return text;
50 }
51 }
53 public void run() throws Exception {
54 final String bootPath = System.getProperty("sun.boot.class.path");
55 final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
56 assert tool != null;
58 JavaFileObject test_java = new MyFileObject("test/Test.java",
59 "package test; public @interface Test {\n" +
60 " public int mandatory();\n" +
61 "}\n");
63 JavaFileObject package_info_java = new MyFileObject("test/package-info.java",
64 "@Test package test;");
66 DiagnosticCollector<JavaFileObject> coll = new DiagnosticCollector<JavaFileObject>();
68 List<String> options = Arrays.asList("-bootclasspath", bootPath);
69 List<? extends JavaFileObject> files = Arrays.asList(test_java, package_info_java);
70 JavacTask ct = (JavacTask)tool.getTask(null, null, coll, options, null, files);
71 ct.analyze();
73 String expectedCode = "compiler.err.annotation.missing.default.value";
74 List<Diagnostic<? extends JavaFileObject>> diags = coll.getDiagnostics();
75 switch (diags.size()) {
76 case 0:
77 throw new Exception("no diagnostics reported");
78 case 1:
79 String code = diags.get(0).getCode();
80 if (code.equals(expectedCode))
81 return;
82 throw new Exception("unexpected diag: " + diags.get(0));
83 default:
84 throw new Exception("unexpected diags reported: " + diags);
85 }
86 }
87 }