1 /*
2  * Copyright (c) 2019, 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 8218419
27  * @library /test/lib
28  * @modules jdk.compiler
29  * @build jdk.test.lib.compiler.CompilerUtils
30  * @run driver GetPackageFromBootClassPath setup
31  * @run main/othervm -Xbootclasspath/a:boot GetPackageFromBootClassPath
32  */
33 
34 import java.lang.annotation.Annotation;
35 import java.nio.file.Path;
36 import java.nio.file.Paths;
37 import java.util.Arrays;
38 
39 public class GetPackageFromBootClassPath {
main(String... args)40     public static void main(String... args) throws Exception {
41         if (args.length == 0) {
42             test();
43         } else {
44             setupBootLib();
45         }
46     }
47 
test()48     private static void test() throws Exception {
49         Class<?> c = Class.forName("foo.Foo", false, null);
50         Package p = c.getPackage();
51         Annotation[] annotations = p.getAnnotations();
52         Class<?> annType = Class.forName("foo.MyAnnotation", false, null);
53         if (annotations.length != 1 ||
54             annotations[0].annotationType() != annType) {
55             throw new RuntimeException("Expected foo.MyAnnotation but got " +
56                 Arrays.toString(annotations));
57         }
58     }
59 
setupBootLib()60     private static void setupBootLib() throws Exception {
61         Path testSrc = Paths.get(System.getProperty("test.src"), "boot");
62         Path bootDir = Paths.get("boot");
63         if (!jdk.test.lib.compiler.CompilerUtils.compile(testSrc, bootDir)) {
64             throw new RuntimeException("compilation fails");
65         }
66     }
67 }
68