1/* 2 * Copyright (c) 2011, 2015, 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 */ 23 24/* 25 * @test 26 * @bug 6993311 27 * @summary annotations on packages are not validated 28 * @modules jdk.compiler 29 */ 30 31import java.io.*; 32import java.net.*; 33import java.util.*; 34import javax.tools.*; 35import com.sun.source.util.*; 36 37public class TestAnnotationPackageInfo { 38 public static void main(String... args) throws Exception { 39 new TestAnnotationPackageInfo().run(); 40 } 41 42 static class MyFileObject extends SimpleJavaFileObject { 43 private String text; 44 public MyFileObject(String fileName, String text) { 45 super(URI.create("myfo:/" + fileName), JavaFileObject.Kind.SOURCE); 46 this.text = text; 47 } 48 @Override 49 public CharSequence getCharContent(boolean ignoreEncodingErrors) { 50 return text; 51 } 52 } 53 54 public void run() throws Exception { 55 final JavaCompiler tool = ToolProvider.getSystemJavaCompiler(); 56 assert tool != null; 57 58 JavaFileObject test_java = new MyFileObject("test/Test.java", 59 "package test; public @interface Test {\n" + 60 " public int mandatory();\n" + 61 "}\n"); 62 63 JavaFileObject package_info_java = new MyFileObject("test/package-info.java", 64 "@Test package test;"); 65 66 DiagnosticCollector<JavaFileObject> coll = new DiagnosticCollector<JavaFileObject>(); 67 68 List<? extends JavaFileObject> files = Arrays.asList(test_java, package_info_java); 69 JavacTask ct = (JavacTask)tool.getTask(null, null, coll, null, null, files); 70 ct.analyze(); 71 72 String expectedCode = "compiler.err.annotation.missing.default.value"; 73 List<Diagnostic<? extends JavaFileObject>> diags = coll.getDiagnostics(); 74 switch (diags.size()) { 75 case 0: 76 throw new Exception("no diagnostics reported"); 77 case 1: 78 String code = diags.get(0).getCode(); 79 if (code.equals(expectedCode)) 80 return; 81 throw new Exception("unexpected diag: " + diags.get(0)); 82 default: 83 throw new Exception("unexpected diags reported: " + diags); 84 } 85 } 86} 87