1/*
2 * Copyright (c) 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     8073056
27 * @summary Repeating annotations throws java.security.AccessControlException with a SecurityManager
28 *
29 * @library /lib/testlibrary
30 * @build jdk.testlibrary.Asserts
31 * @run main CustomRepeatingWithSecurityManager
32 * @run main/othervm CustomRepeatingWithSecurityManager "withSM"
33 */
34
35import java.lang.annotation.*;
36import java.lang.reflect.*;
37
38import jdk.testlibrary.Asserts;
39
40public class CustomRepeatingWithSecurityManager {
41    public static void main(String[] args) throws Exception {
42        if (args.length == 1) {
43            SecurityManager sm = new SecurityManager();
44            System.setSecurityManager(sm);
45        }
46
47        Asserts.assertTrue(new CustomAnnotations().getAnnotationsByType(MyAnnotation.class).length == 2,
48                "Array should contain 2 annotations");
49        Asserts.assertEquals(new CustomAnnotations().getAnnotationsByType(MyAnnotation.class)[1].name(),
50                "Bar", "Should be 'Bar'");
51    }
52
53    static class CustomAnnotations implements AnnotatedElement {
54        @Override
55        public Annotation[] getDeclaredAnnotations() {
56            Annotation[] res = new Annotation[1];
57            res[0] = new MyAnnotationsImpl();
58            return res;
59        }
60
61        @Override
62        public Annotation[] getAnnotations() {
63            return getDeclaredAnnotations();
64        }
65
66        @Override
67        public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
68            return null;
69        }
70    }
71
72    static class MyAnnotationsImpl implements MyAnnotations {
73        public MyAnnotation[] value() {
74            MyAnnotation[] res = new MyAnnotation[2];
75            res[0] = new MyAnnotationImpl("Foo");
76            res[1] = new MyAnnotationImpl("Bar");
77            return res;
78        }
79
80        @Override
81        public Class<? extends Annotation> annotationType() {
82            return MyAnnotations.class;
83        }
84    }
85
86    static class MyAnnotationImpl implements MyAnnotation {
87        private String val;
88        MyAnnotationImpl(String val) {
89            this.val = val;
90        }
91
92        public String name() { return val; }
93
94        @Override
95        public Class<? extends Annotation> annotationType() {
96            return MyAnnotations.class;
97        }
98    }
99
100    @Retention(RetentionPolicy.RUNTIME)
101    @interface MyAnnotations {
102        MyAnnotation[] value();
103    }
104
105    @Retention(RetentionPolicy.RUNTIME)
106    @Repeatable(MyAnnotations.class)
107    @interface MyAnnotation {
108        String name();
109    }
110}
111