Path: blob/master/test/jdk/java/lang/annotation/Missing/MissingArrayElement/MissingClassArrayElementTest.java
41155 views
/*1* Copyright (c) 2018, Google Inc. 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*/2223import static java.lang.annotation.RetentionPolicy.RUNTIME;2425import java.lang.annotation.Retention;26import java.util.Arrays;2728/*29* @test30* @bug 718398531* @summary getAnnotation() throws an ArrayStoreException when the annotation class not present32* @compile MissingClassArrayElementTest.java MissingClass.java MissingClass2.java33* @clean MissingClass MissingClass234* @run main MissingClassArrayElementTest35*/36public class MissingClassArrayElementTest {3738@Retention(RUNTIME)39@interface AnnotationAnnotation {40ClassArrayAnnotation[] value();41}4243@Retention(RUNTIME)44@interface ClassArrayAnnotation {45Class<?>[] value();46}4748@AnnotationAnnotation({49@ClassArrayAnnotation({MissingClass.class}),50@ClassArrayAnnotation({MissingClass.class, String.class}),51@ClassArrayAnnotation({String.class, MissingClass.class}),52@ClassArrayAnnotation({MissingClass.class, MissingClass2.class}),53@ClassArrayAnnotation({String.class})54})55static class Test {56void f(57@AnnotationAnnotation({58@ClassArrayAnnotation({MissingClass.class, MissingClass.class}),59@ClassArrayAnnotation({Float.class})60})61int x,62@AnnotationAnnotation({@ClassArrayAnnotation({Double.class})}) int y) {}6364Test(65@AnnotationAnnotation({66@ClassArrayAnnotation({MissingClass.class, MissingClass.class}),67@ClassArrayAnnotation({Short.class}),68})69int x,70@AnnotationAnnotation({@ClassArrayAnnotation({Character.class})}) int y) {}71}7273public static void main(String[] args) throws Exception {74classAnnotationTest();75methodParameterAnnotationsTest();76constructorParameterAnnotationsTest();77}7879static void classAnnotationTest() throws Exception {80ClassArrayAnnotation[] outer = Test.class.getAnnotation(AnnotationAnnotation.class).value();81assertMissing(outer[0]);82assertMissing(outer[1]);83assertMissing(outer[2]);84assertMissing(outer[3]);85assertArrayEquals(outer[4].value(), new Class<?>[] {String.class});86}8788static void methodParameterAnnotationsTest() throws Exception {89AnnotationAnnotation[] methodParameterAnnotations =90Arrays.stream(91Test.class92.getDeclaredMethod("f", int.class, int.class)93.getParameterAnnotations())94.map(x -> ((AnnotationAnnotation) x[0]))95.toArray(AnnotationAnnotation[]::new);96// The first parameter's annotation contains some well-formed values, and the second97// parameter's98// annotation is well-formed99assertArrayEquals(100methodParameterAnnotations[0].value()[1].value(), new Class<?>[] {Float.class});101assertArrayEquals(102methodParameterAnnotations[1].value()[0].value(), new Class<?>[] {Double.class});103// The first parameter's annotation contains a missing value104assertMissing(methodParameterAnnotations[0].value()[0]);105}106107static void constructorParameterAnnotationsTest() throws Exception {108AnnotationAnnotation[] constructorParameterAnnotations =109Arrays.stream(110Test.class111.getDeclaredConstructor(int.class, int.class)112.getParameterAnnotations())113.map(x -> ((AnnotationAnnotation) x[0]))114.toArray(AnnotationAnnotation[]::new);115// The first parameter's annotation contains some well-formed values, and the second116// parameter's117// annotation is well-formed118assertArrayEquals(119constructorParameterAnnotations[0].value()[1].value(),120new Class<?>[] {Short.class});121assertArrayEquals(122constructorParameterAnnotations[1].value()[0].value(),123new Class<?>[] {Character.class});124// The first parameter's annotation contains a missing value125assertMissing(constructorParameterAnnotations[0].value()[0]);126}127128static void assertArrayEquals(Object[] actual, Object[] expected) {129if (!Arrays.equals(actual, expected)) {130throw new AssertionError(131"expected: " + Arrays.toString(expected) + ", was: " + Arrays.toString(actual));132}133}134135static void assertMissing(ClassArrayAnnotation missing) {136try {137missing.value();138throw new AssertionError("expected exception");139} catch (TypeNotPresentException expected) {140if (!expected.typeName().equals("MissingClass")) {141throw new AssertionError(142"expected TypeNotPresentException: MissingClass", expected);143}144}145}146}147148149