1 /* 2 * Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 package jdk.jfr.api.metadata.eventtype; 26 27 import java.lang.annotation.Annotation; 28 import java.lang.reflect.Method; 29 import java.lang.reflect.Modifier; 30 import java.util.ArrayList; 31 import java.util.HashMap; 32 import java.util.List; 33 import java.util.Map; 34 35 import jdk.jfr.AnnotationElement; 36 import jdk.jfr.BooleanFlag; 37 import jdk.jfr.Category; 38 import jdk.jfr.ContentType; 39 import jdk.jfr.Description; 40 import jdk.jfr.Enabled; 41 import jdk.jfr.Event; 42 import jdk.jfr.EventFactory; 43 import jdk.jfr.EventType; 44 import jdk.jfr.Experimental; 45 import jdk.jfr.Frequency; 46 import jdk.jfr.Label; 47 import jdk.jfr.MemoryAddress; 48 import jdk.jfr.DataAmount; 49 import jdk.jfr.MetadataDefinition; 50 import jdk.jfr.Name; 51 import jdk.jfr.Percentage; 52 import jdk.jfr.Period; 53 import jdk.jfr.Registered; 54 import jdk.jfr.Relational; 55 import jdk.jfr.StackTrace; 56 import jdk.jfr.Threshold; 57 import jdk.jfr.Timespan; 58 import jdk.jfr.Timestamp; 59 import jdk.jfr.TransitionFrom; 60 import jdk.jfr.TransitionTo; 61 import jdk.jfr.Unsigned; 62 import jdk.jfr.ValueDescriptor; 63 import jdk.test.lib.Asserts; 64 65 /* 66 * @test 67 * @summary Test for AnnotationElement.getAnnotationElements() 68 * @key jfr 69 * @library /test/lib 70 * @run main/othervm jdk.jfr.api.metadata.eventtype.TestGetAnnotationElements 71 */ 72 public class TestGetAnnotationElements { 73 74 @SuppressWarnings("unchecked") 75 public static void main(String[] args) throws Throwable { 76 Class<?>[] jfrAnnotations = { 77 Category.class, Description.class, Enabled.class, 78 Experimental.class, BooleanFlag.class, Frequency.class, Label.class, 79 MemoryAddress.class, DataAmount.class, Name.class, 80 Registered.class, Percentage.class, 81 Period.class, Relational.class, StackTrace.class, 82 Threshold.class, Timespan.class, Timestamp.class, 83 TransitionFrom.class, TransitionTo.class, Unsigned.class 84 }; 85 86 for (Class<?> clz : jfrAnnotations) { 87 Class<? extends Annotation> annptationClass = (Class<? extends Annotation>) clz; 88 System.out.println("AnnotationElement: " + annptationClass); 89 Map<String, Object> values = createValueMapForAnnotation(annptationClass); 90 List<Annotation> persistableAnnotation = createPersistableAnnotationList(annptationClass); 91 AnnotationElement ae = new AnnotationElement(annptationClass, values); 92 List<AnnotationElement> aes = ae.getAnnotationElements(); 93 Asserts.assertEquals(persistableAnnotation.size(), aes.size()); 94 } 95 96 List<ValueDescriptor> fields = new ArrayList<>(); 97 List<AnnotationElement> eventAnnotations = new ArrayList<>(); 98 eventAnnotations.add(new AnnotationElement(Label.class, "MyEvent")); 99 100 EventFactory f = EventFactory.create(eventAnnotations, fields); 101 EventType type = f.getEventType(); 102 List<AnnotationElement> aes = type.getAnnotationElements().get(0).getAnnotationElements(); 103 Asserts.assertEquals(0, aes.size()); 104 105 EventType et = EventType.getEventType(MyEvent.class); 106 ValueDescriptor field = et.getField("transactionRate"); 107 aes = field.getAnnotationElements().get(0).getAnnotationElements(); 108 Asserts.assertEquals(3, aes.size()); 109 assertContainsAnnotation(aes, Description.class); 110 assertContainsAnnotation(aes, Label.class); 111 assertContainsAnnotation(aes, ContentType.class); 112 113 } 114 115 private static List<Annotation> createPersistableAnnotationList( Class<? extends Annotation> annptationClass) { 116 List<Annotation> as = new ArrayList<>(); 117 for (Annotation a : annptationClass.getAnnotations()) { 118 MetadataDefinition m = a.annotationType().getAnnotation(MetadataDefinition.class); 119 if (m != null) { 120 as.add(a); 121 } 122 } 123 return as; 124 } 125 126 private static void assertContainsAnnotation(List<AnnotationElement> aez, Class<?> clz) { 127 for (AnnotationElement ae : aez) { 128 if (ae.getTypeName().equals(clz.getName())) { 129 return; 130 } 131 } 132 Asserts.fail("Class " + clz + " not found in the annotation elements"); 133 } 134 135 private static Map<String, Object> createValueMapForAnnotation(Class<?> clz) { 136 Map<String, Object> map = new HashMap<>(); 137 for (Method method : clz.getDeclaredMethods()) { 138 int modifiers = method.getModifiers(); 139 if (Modifier.isPublic(modifiers) || Modifier.isProtected(modifiers)) { 140 map.put(method.getName(), "value"); 141 } 142 } 143 return map; 144 } 145 146 private static class MyEvent extends Event { 147 148 @Frequency 149 long transactionRate; 150 } 151 152 }