Path: blob/master/test/jdk/java/lang/annotation/Missing/MissingArrayElement/MissingEnumArrayElementTest.java
41154 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 MissingEnumArrayElementTest.java EnumToCompileAgainst.java33* @clean Enum34* @compile EnumToRunAgainst.java35* @run main MissingEnumArrayElementTest36*/37public class MissingEnumArrayElementTest {3839@Retention(RUNTIME)40@interface AnnotationAnnotation {41EnumArrayAnnotation[] value();42}4344@Retention(RUNTIME)45@interface EnumArrayAnnotation {46Enum[] value();47}4849@AnnotationAnnotation({50@EnumArrayAnnotation({Enum.TWO}),51@EnumArrayAnnotation({Enum.ONE, Enum.TWO}),52@EnumArrayAnnotation({Enum.TWO, Enum.ONE}),53@EnumArrayAnnotation({Enum.TWO, Enum.THREE}),54@EnumArrayAnnotation({Enum.ONE}),55})56static class Test {57void f(58@AnnotationAnnotation({59@EnumArrayAnnotation({Enum.TWO, Enum.ONE}),60@EnumArrayAnnotation({Enum.ONE})61})62int x,63@AnnotationAnnotation({@EnumArrayAnnotation({Enum.ONE})}) int y) {}6465Test(66@AnnotationAnnotation({67@EnumArrayAnnotation({Enum.TWO, Enum.ONE}),68@EnumArrayAnnotation({Enum.ONE})69})70int x,71@AnnotationAnnotation({@EnumArrayAnnotation({Enum.ONE})}) int y) {}72}7374public static void main(String[] args) throws Exception {75classAnnotationTest();76methodParameterAnnotationsTest();77constructorParameterAnnotationsTest();78}7980static void classAnnotationTest() throws Exception {81EnumArrayAnnotation[] outer = Test.class.getAnnotation(AnnotationAnnotation.class).value();82assertMissing(outer[0]);83assertMissing(outer[1]);84assertMissing(outer[2]);85assertMissing(outer[3]);86assertArrayEquals(outer[4].value(), new Enum[] {Enum.ONE});87}8889static void methodParameterAnnotationsTest() throws Exception {90AnnotationAnnotation[] methodParameterAnnotations =91Arrays.stream(92Test.class93.getDeclaredMethod("f", int.class, int.class)94.getParameterAnnotations())95.map(x -> ((AnnotationAnnotation) x[0]))96.toArray(AnnotationAnnotation[]::new);97// The first parameter's annotation contains some well-formed values, and the second98// parameter's99// annotation is well-formed100assertArrayEquals(methodParameterAnnotations[0].value()[1].value(), new Enum[] {Enum.ONE});101assertArrayEquals(methodParameterAnnotations[1].value()[0].value(), new Enum[] {Enum.ONE});102// The first parameter's annotation contains a missing value103assertMissing(methodParameterAnnotations[0].value()[0]);104}105106static void constructorParameterAnnotationsTest() throws Exception {107AnnotationAnnotation[] constructorParameterAnnotations =108Arrays.stream(109Test.class110.getDeclaredConstructor(int.class, int.class)111.getParameterAnnotations())112.map(x -> ((AnnotationAnnotation) x[0]))113.toArray(AnnotationAnnotation[]::new);114// The first parameter's annotation contains some well-formed values, and the second115// parameter's116// annotation is well-formed117assertArrayEquals(constructorParameterAnnotations[0].value()[1].value(), new Enum[] {Enum.ONE});118assertArrayEquals(constructorParameterAnnotations[1].value()[0].value(), new Enum[] {Enum.ONE});119// The first parameter's annotation contains a missing value120assertMissing(constructorParameterAnnotations[0].value()[0]);121}122123static void assertArrayEquals(Object[] actual, Object[] expected) {124if (!Arrays.equals(actual, expected)) {125throw new AssertionError(126"expected: " + Arrays.toString(expected) + ", was: " + Arrays.toString(actual));127}128}129130static void assertMissing(EnumArrayAnnotation annotation) throws Exception {131try {132annotation.value();133throw new AssertionError("expected exception");134} catch (EnumConstantNotPresentException expected) {135if (!expected.getMessage().equals("Enum.TWO")) {136throw new AssertionError(137"expected EnumConstantNotPresentException for Enum.TWO", expected);138}139}140}141}142143144