Path: blob/master/test/jdk/java/lang/annotation/AnnotationVerifier.java
41152 views
/*1* Copyright (c) 2016, 2017, 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. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425/*26* Create class file using ASM, slightly modified the ASMifier output27*/2829import org.testng.Assert;30import org.testng.annotations.Test;3132import java.lang.annotation.Annotation;33import java.lang.annotation.AnnotationFormatError;34import java.util.Arrays;35import java.util.Set;36import java.util.stream.Collectors;37import java.util.stream.Stream;3839/*40* @test41* @bug 815851042* @summary Verify valid annotation43* @modules java.base/jdk.internal.org.objectweb.asm44* @modules java.base/sun.reflect.annotation45* @clean AnnotationWithVoidReturn AnnotationWithParameter46* AnnotationWithExtraInterface AnnotationWithException47* AnnotationWithHashCode AnnotationWithDefaultMember48* AnnotationWithoutAnnotationAccessModifier HolderX49* @compile -XDignore.symbol.file ClassFileGenerator.java GoodAnnotation.java50* @run main ClassFileGenerator51* @run testng AnnotationVerifier52*/5354public class AnnotationVerifier {5556//=======================================================57// GoodAnnotation...5859@GoodAnnotation60static class HolderA {61}6263@Test64public void holderA_goodAnnotation() {65testGetAnnotation(HolderA.class, GoodAnnotation.class, true);66}6768@Test69public void holderA_annotations() {70testGetAnnotations(HolderA.class, GoodAnnotation.class);71}7273//=======================================================74// AnnotationWithParameter...7576/*77@Retention(RetentionPolicy.RUNTIME)78public @interface AnnotationWithParameter {79int m(int x) default -1;80}81*/8283@GoodAnnotation84@AnnotationWithParameter85static class HolderB {86}8788@Test89public void holderB_annotationWithParameter() {90testGetAnnotation(HolderB.class, AnnotationWithParameter.class, false);91}9293@Test94public void holderB_goodAnnotation() {95testGetAnnotation(HolderB.class, GoodAnnotation.class, true);96}9798@Test99public void holderB_annotations() {100testGetAnnotations(HolderB.class, GoodAnnotation.class);101}102103//=======================================================104// AnnotationWithVoidReturn...105106/*107@Retention(RetentionPolicy.RUNTIME)108public @interface AnnotationWithVoidReturn {109void m() default 1;110}111*/112113@GoodAnnotation114@AnnotationWithVoidReturn115static class HolderC {116}117118@Test(expectedExceptions = AnnotationFormatError.class)119public void holderC_annotationWithVoidReturn() {120testGetAnnotation(HolderC.class, AnnotationWithVoidReturn.class, false);121}122123@Test(expectedExceptions = AnnotationFormatError.class)124public void holderC_goodAnnotation() {125testGetAnnotation(HolderC.class, GoodAnnotation.class, false);126}127128@Test(expectedExceptions = AnnotationFormatError.class)129public void holderC_annotations() {130testGetAnnotations(HolderC.class);131}132133//=======================================================134// AnnotationWithExtraInterface...135136/*137@Retention(RetentionPolicy.RUNTIME)138public @interface AnnotationWithExtraInterface extends java.io.Serializable {139int m() default 1;140}141*/142143@GoodAnnotation144@AnnotationWithExtraInterface145static class HolderD {146}147148@Test(expectedExceptions = AnnotationFormatError.class)149public void holderD_annotationWithExtraInterface() {150testGetAnnotation(HolderD.class, AnnotationWithExtraInterface.class, false);151}152153@Test(expectedExceptions = AnnotationFormatError.class)154public void holderD_goodAnnotation() {155testGetAnnotation(HolderD.class, GoodAnnotation.class, false);156}157158@Test(expectedExceptions = AnnotationFormatError.class)159public void holderD_annotations() {160testGetAnnotations(HolderD.class);161}162163//=======================================================164// AnnotationWithException...165166/*167@Retention(RetentionPolicy.RUNTIME)168public @interface AnnotationWithException {169int m() throws Exception default 1;170}171*/172173@GoodAnnotation174@AnnotationWithException175static class HolderE {176}177178@AnnotationWithException179static class HolderE2 {180}181182@Test183public void holderE_annotationWithException() {184testGetAnnotation(HolderE.class, AnnotationWithException.class, true);185}186187@Test188public void holderE_goodAnnotation() {189testGetAnnotation(HolderE.class, GoodAnnotation.class, true);190}191192@Test193public void holderE_annotations() {194testGetAnnotations(HolderE.class, GoodAnnotation.class, AnnotationWithException.class);195}196197@Test(expectedExceptions = AnnotationFormatError.class)198public void holderE_annotationWithException_equals() {199AnnotationWithException ann1, ann2;200try {201ann1 = HolderE.class.getAnnotation(AnnotationWithException.class);202ann2 = HolderE2.class.getAnnotation(AnnotationWithException.class);203} catch (Throwable t) {204throw new AssertionError("Unexpected exception", t);205}206Assert.assertNotNull(ann1);207Assert.assertNotNull(ann2);208209testEquals(ann1, ann2, true); // this throws AnnotationFormatError210}211212//=======================================================213// AnnotationWithHashCode...214215/*216@Retention(RetentionPolicy.RUNTIME)217public @interface AnnotationWithHashCode {218int hashCode() default 1;219}220*/221222@GoodAnnotation223@AnnotationWithHashCode224static class HolderF {225}226227@AnnotationWithHashCode228static class HolderF2 {229}230231@Test232public void holderF_annotationWithHashCode() {233testGetAnnotation(HolderF.class, AnnotationWithHashCode.class, true);234}235236@Test237public void holderF_goodAnnotation() {238testGetAnnotation(HolderF.class, GoodAnnotation.class, true);239}240241@Test242public void holderF_annotations() {243testGetAnnotations(HolderF.class, GoodAnnotation.class, AnnotationWithHashCode.class);244}245246@Test(expectedExceptions = AnnotationFormatError.class)247public void holderF_annotationWithHashCode_equals() {248AnnotationWithHashCode ann1, ann2;249try {250ann1 = HolderF.class.getAnnotation(AnnotationWithHashCode.class);251ann2 = HolderF2.class.getAnnotation(AnnotationWithHashCode.class);252} catch (Throwable t) {253throw new AssertionError("Unexpected exception", t);254}255Assert.assertNotNull(ann1);256Assert.assertNotNull(ann2);257258testEquals(ann1, ann2, true); // this throws AnnotationFormatError259}260261//=======================================================262// AnnotationWithDefaultMember...263264/*265@Retention(RetentionPolicy.RUNTIME)266public @interface AnnotationWithDefaultMember {267int m() default 1;268default int d() default 2 { return 2; }269}270*/271272@GoodAnnotation273@AnnotationWithDefaultMember274static class HolderG {275}276277@AnnotationWithDefaultMember278static class HolderG2 {279}280281@Test282public void holderG_annotationWithDefaultMember() {283testGetAnnotation(HolderG.class, AnnotationWithDefaultMember.class, true);284}285286@Test287public void holderG_goodAnnotation() {288testGetAnnotation(HolderG.class, GoodAnnotation.class, true);289}290291@Test292public void holderG_annotations() {293testGetAnnotations(HolderG.class, GoodAnnotation.class, AnnotationWithDefaultMember.class);294}295296@Test(expectedExceptions = AnnotationFormatError.class)297public void holderG_annotationWithDefaultMember_equals() {298AnnotationWithDefaultMember ann1, ann2;299try {300ann1 = HolderG.class.getAnnotation(AnnotationWithDefaultMember.class);301ann2 = HolderG2.class.getAnnotation(AnnotationWithDefaultMember.class);302} catch (Throwable t) {303throw new AssertionError("Unexpected exception", t);304}305Assert.assertNotNull(ann1);306Assert.assertNotNull(ann2);307308testEquals(ann1, ann2, true); // this throws AnnotationFormatError309}310311//=======================================================312// AnnotationWithoutAnnotationAccessModifier...313314/*315316@Retention(RetentionPolicy.RUNTIME)317public interface AnnotationWithoutAnnotationAccessModifier extends Annotation {318int m() default 1;319}320321@GoodAnnotation322@AnnotationWithoutAnnotationAccessModifier323static class HolderX {324}325326*/327328@Test329public void holderX_annotationWithoutAnnotationAccessModifier() {330testGetAnnotation(HolderX.class, AnnotationWithoutAnnotationAccessModifier.class, false);331}332333@Test334public void holderX_goodAnnotation() {335testGetAnnotation(HolderX.class, GoodAnnotation.class, true);336}337338@Test339public void holderX_annotations() {340testGetAnnotations(HolderX.class, GoodAnnotation.class);341}342343//=======================================================344// utils345//346347private static void testGetAnnotation(Class<?> holderClass,348Class<? extends Annotation> annType,349boolean expectedPresent) {350Object result = null;351try {352try {353result = holderClass.getAnnotation(annType);354if (expectedPresent != (result != null)) {355throw new AssertionError("Expected " +356(expectedPresent ? "non-null" : "null") +357" result, but got: " + result);358}359} catch (Throwable t) {360result = t;361throw t;362}363} finally {364System.out.println("\n" +365holderClass.getSimpleName() +366".class.getAnnotation(" +367annType.getSimpleName() +368".class) = " +369result);370}371}372373private static void testGetAnnotations(Class<?> holderClass,374Class<? extends Annotation> ... expectedTypes) {375Object result = null;376try {377try {378Annotation[] anns = holderClass.getAnnotations();379380Set<Class<? extends Annotation>> gotTypes =381Stream.of(anns)382.map(Annotation::annotationType)383.collect(Collectors.toSet());384385Set<Class<? extends Annotation>> expTypes =386Stream.of(expectedTypes)387.collect(Collectors.toSet());388389if (!expTypes.equals(gotTypes)) {390throw new AssertionError("Expected annotation types: " + expTypes +391" but got: " + Arrays.toString(anns));392}393result = Arrays.toString(anns);394} catch (Throwable t) {395result = t;396throw t;397}398} finally {399System.out.println("\n" +400holderClass.getSimpleName() +401".class.getAnnotations() = " +402result);403}404}405406private static void testEquals(Annotation ann1, Annotation ann2, boolean expectedEquals) {407Object result = null;408try {409try {410boolean gotEquals = ann1.equals(ann2);411Assert.assertEquals(gotEquals, expectedEquals);412result = gotEquals;413} catch (Throwable t) {414result = t;415throw t;416}417} finally {418System.out.println("\n" + ann1 + ".equals(" + ann2 + ") = " + result);419}420}421}422423424