Path: blob/master/test/jdk/java/lang/annotation/repeatingAnnotations/InheritedAssociatedAnnotations.java
41153 views
/*1* Copyright (c) 2013, 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 802717026* @summary getAnnotationsByType needs to take the class hierarchy into account27* when determining which annotations are associated with a given28* class.29* @run main InheritedAssociatedAnnotations30*/3132import java.lang.annotation.*;33import java.lang.reflect.*;34import java.util.Arrays;3536public class InheritedAssociatedAnnotations {3738public static void main(String[] args) {39checkAssociated(A3.class);40checkAssociated(B3.class);41checkAssociated(C3.class);42checkAssociated(D3.class);43}4445private static void checkAssociated(AnnotatedElement ae) {46Ann[] actual = ae.getAnnotationsByType(Ann.class);47Ann[] expected = ae.getAnnotation(ExpectedAssociated.class).value();4849if (!Arrays.equals(actual, expected)) {50throw new RuntimeException(String.format(51"Test failed for %s: Expected %s but got %s.",52ae,53Arrays.toString(expected),54Arrays.toString(actual)));55}56}5758}5960@Retention(RetentionPolicy.RUNTIME)61@interface ExpectedAssociated {62Ann[] value();63}646566@Inherited67@Retention(RetentionPolicy.RUNTIME)68@Target(ElementType.TYPE)69@Repeatable(AnnCont.class)70@interface Ann {71int value();72}7374@Inherited75@Retention(RetentionPolicy.RUNTIME)76@Target(ElementType.TYPE)77@interface AnnCont {78Ann[] value();79}808182@Ann(10)83class A1 {}8485@Ann(20)86class A2 extends A1 {}8788@ExpectedAssociated({@Ann(20)})89class A3 extends A2 {}909192@Ann(10) @Ann(11)93class B1 {}9495@Ann(20)96class B2 extends B1 {}9798@ExpectedAssociated({@Ann(20)})99class B3 extends B2 {}100101102@Ann(10)103class C1 {}104105@Ann(20) @Ann(21)106class C2 extends C1 {}107108@ExpectedAssociated({@Ann(20), @Ann(21)})109class C3 extends C2 {}110111112@Ann(10) @Ann(11)113class D1 {}114115@Ann(20) @Ann(21)116class D2 extends D1 {}117118@ExpectedAssociated({@Ann(20), @Ann(21)})119class D3 extends D2 {}120121122