Path: blob/master/test/jdk/java/lang/annotation/ExceptionalToString/ExceptionalToStringTest.java
41154 views
/*1* Copyright (c) 2016, 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 504083026* @summary Verify information annotation strings with exception proxies27* @compile -sourcepath version1 version1/Fleeting.java version1/Utopia.java version1/DangerousAnnotation.java28* @compile AnnotationHost.java29* @build ExceptionalToStringTest30* @run main ExceptionalToStringTest31* @clean Utopia DangerousAnnotation32* @compile -sourcepath version2 -implicit:none version2/Utopia.java33* @compile -sourcepath version2 -implicit:none version2/DangerousAnnotation.java34* @clean Fleeting35* @run main ExceptionalToStringTest36*/3738/**39* There are three potential exception conditions which can occur40* reading annotations:41*42* EnumConstantNotPresentException - "Thrown when an application tries43* to access an enum constant by name and the enum type contains no44* constant with the specified name."45*46* AnnotationTypeMismatchException - "Thrown to indicate that a47* program has attempted to access an element of an annotation whose48* type has changed after the annotation was compiled (or serialized)"49*50* TypeNotPresentException - "Thrown when an application tries to51* access a type using a string representing the type's name, but no52* definition for the type with the specified name can be found."53*54* The test reads an annotation, DangerousAnnotation, which can55* display all three pathologies. The pathologies are <em>not</em>56* present when the version1 sources are used but are present with the57* version2 sources. The version2 sources remove an enum constant58* (EnumConstantNotPresentException), change the return type of an59* annotation method (AnnotationTypeMismatchException), and remove a60* type whose Class literal is referenced (TypeNotPresentException).61*/62public class ExceptionalToStringTest {63public static void main(String... args) {64String annotationAsString = AnnotationHost.class.getAnnotation(DangerousAnnotation.class).toString();6566// Verify no occurrence of "ExceptionProxy" in the string.67if (annotationAsString.indexOf("ExceptionProxy") != -1) {68throw new RuntimeException();69}70}71}727374