Path: blob/master/test/jdk/java/lang/annotation/Missing/MissingArrayElement/MissingAnnotationArrayElementTest.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.lang.reflect.Constructor;27import java.lang.reflect.Method;2829/*30* @test31* @bug 718398532* @summary getAnnotation() should throw NoClassDefFoundError when an annotation class is not33* present at runtime34* @compile MissingAnnotationArrayElementTest.java MissingAnnotation.java35* @clean MissingAnnotation36* @run main MissingAnnotationArrayElementTest37*/38public class MissingAnnotationArrayElementTest {3940@Retention(RUNTIME)41@interface AnnotationAnnotation {42MissingAnnotation[] value();43}4445@AnnotationAnnotation({@MissingAnnotation, @MissingAnnotation})46static class Test {47void f(@AnnotationAnnotation({@MissingAnnotation, @MissingAnnotation}) int x) {}4849Test(@AnnotationAnnotation({@MissingAnnotation, @MissingAnnotation}) int x) {}50}5152public static void main(String[] args) throws Exception {53// MissingAnnotation will be absent from the runtime classpath, causing a54// NoClassDefFoundError when AnnotationAnnotation is read (since the type of its value array55// references cannot be completed).56assertThrowsNoClassDefFoundError(57() -> Test.class.getAnnotation(AnnotationAnnotation.class));58Method method = Test.class.getDeclaredMethod("f", int.class);59assertThrowsNoClassDefFoundError(method::getParameterAnnotations);60Constructor constructor = Test.class.getDeclaredConstructor(int.class);61assertThrowsNoClassDefFoundError(constructor::getParameterAnnotations);62}6364interface ThrowingRunnable {65void run() throws Exception;66}6768static void assertThrowsNoClassDefFoundError(ThrowingRunnable throwingRunnable)69throws Exception {70try {71throwingRunnable.run();72throw new AssertionError("expected exception");73} catch (NoClassDefFoundError expected) {74if (!expected.getMessage().contains("MissingAnnotation")) {75throw expected;76}77}78}79}808182