Path: blob/master/test/jdk/java/lang/annotation/repeatingAnnotations/CustomRepeatingWithSecurityManager.java
41153 views
/*1* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 807305626* @summary Repeating annotations throws java.security.AccessControlException with a SecurityManager27*28* @library /test/lib29* @build jdk.test.lib.Asserts30* @run main CustomRepeatingWithSecurityManager31* @run main/othervm -Djava.security.manager=allow CustomRepeatingWithSecurityManager "withSM"32*/3334import java.lang.annotation.*;35import java.lang.reflect.*;3637import jdk.test.lib.Asserts;3839public class CustomRepeatingWithSecurityManager {40public static void main(String[] args) throws Exception {41if (args.length == 1) {42SecurityManager sm = new SecurityManager();43System.setSecurityManager(sm);44}4546Asserts.assertTrue(new CustomAnnotations().getAnnotationsByType(MyAnnotation.class).length == 2,47"Array should contain 2 annotations");48Asserts.assertEquals(new CustomAnnotations().getAnnotationsByType(MyAnnotation.class)[1].name(),49"Bar", "Should be 'Bar'");50}5152static class CustomAnnotations implements AnnotatedElement {53@Override54public Annotation[] getDeclaredAnnotations() {55Annotation[] res = new Annotation[1];56res[0] = new MyAnnotationsImpl();57return res;58}5960@Override61public Annotation[] getAnnotations() {62return getDeclaredAnnotations();63}6465@Override66public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {67return null;68}69}7071static class MyAnnotationsImpl implements MyAnnotations {72public MyAnnotation[] value() {73MyAnnotation[] res = new MyAnnotation[2];74res[0] = new MyAnnotationImpl("Foo");75res[1] = new MyAnnotationImpl("Bar");76return res;77}7879@Override80public Class<? extends Annotation> annotationType() {81return MyAnnotations.class;82}83}8485static class MyAnnotationImpl implements MyAnnotation {86private String val;87MyAnnotationImpl(String val) {88this.val = val;89}9091public String name() { return val; }9293@Override94public Class<? extends Annotation> annotationType() {95return MyAnnotations.class;96}97}9899@Retention(RetentionPolicy.RUNTIME)100@interface MyAnnotations {101MyAnnotation[] value();102}103104@Retention(RetentionPolicy.RUNTIME)105@Repeatable(MyAnnotations.class)106@interface MyAnnotation {107String name();108}109}110111112