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  * @requires vm.hasJFR
  70  * @library /test/lib
  71  * @run main/othervm jdk.jfr.api.metadata.eventtype.TestGetAnnotationElements
  72  */
  73 public class TestGetAnnotationElements {
  74 
  75     @SuppressWarnings("unchecked")
  76     public static void main(String[] args) throws Throwable {
  77         Class<?>[] jfrAnnotations = {
  78             Category.class, Description.class, Enabled.class,
  79             Experimental.class, BooleanFlag.class, Frequency.class, Label.class,
  80             MemoryAddress.class, DataAmount.class, Name.class,
  81             Registered.class, Percentage.class,
  82             Period.class, Relational.class, StackTrace.class,
  83             Threshold.class, Timespan.class, Timestamp.class,
  84             TransitionFrom.class, TransitionTo.class, Unsigned.class
  85         };
  86 
  87         for (Class<?> clz : jfrAnnotations) {
  88             Class<? extends Annotation> annptationClass = (Class<? extends Annotation>) clz;
  89             System.out.println("AnnotationElement: " + annptationClass);
  90             Map<String, Object> values = createValueMapForAnnotation(annptationClass);
  91             List<Annotation> persistableAnnotation = createPersistableAnnotationList(annptationClass);
  92             AnnotationElement ae = new AnnotationElement(annptationClass, values);
  93             List<AnnotationElement> aes = ae.getAnnotationElements();
  94             Asserts.assertEquals(persistableAnnotation.size(), aes.size());
  95         }
  96 
  97         List<ValueDescriptor> fields = new ArrayList<>();
  98         List<AnnotationElement> eventAnnotations = new ArrayList<>();
  99         eventAnnotations.add(new AnnotationElement(Label.class, "MyEvent"));
 100 
 101         EventFactory f = EventFactory.create(eventAnnotations, fields);
 102         EventType type = f.getEventType();
 103         List<AnnotationElement> aes = type.getAnnotationElements().get(0).getAnnotationElements();
 104         Asserts.assertEquals(0, aes.size());
 105 
 106         EventType et = EventType.getEventType(MyEvent.class);
 107         ValueDescriptor field = et.getField("transactionRate");
 108         aes = field.getAnnotationElements().get(0).getAnnotationElements();
 109         Asserts.assertEquals(3, aes.size());
 110         assertContainsAnnotation(aes, Description.class);
 111         assertContainsAnnotation(aes, Label.class);
 112         assertContainsAnnotation(aes, ContentType.class);
 113 
 114     }
 115 
 116     private static List<Annotation> createPersistableAnnotationList( Class<? extends Annotation> annptationClass) {
 117        List<Annotation> as = new ArrayList<>();
 118         for (Annotation a : annptationClass.getAnnotations()) {
 119            MetadataDefinition m = a.annotationType().getAnnotation(MetadataDefinition.class);
 120            if (m != null) {
 121                as.add(a);
 122            }
 123        }
 124         return as;
 125     }
 126 
 127     private static void assertContainsAnnotation(List<AnnotationElement> aez, Class<?> clz) {
 128         for (AnnotationElement ae : aez) {
 129             if (ae.getTypeName().equals(clz.getName())) {
 130                 return;
 131             }
 132         }
 133         Asserts.fail("Class " + clz + " not found in the annotation elements");
 134     }
 135 
 136     private static Map<String, Object> createValueMapForAnnotation(Class<?> clz) {
 137         Map<String, Object> map = new HashMap<>();
 138         for (Method method : clz.getDeclaredMethods()) {
 139             int modifiers = method.getModifiers();
 140             if (Modifier.isPublic(modifiers) || Modifier.isProtected(modifiers)) {
 141                 map.put(method.getName(), "value");
 142             }
 143         }
 144         return map;
 145     }
 146 
 147     private static class MyEvent extends Event {
 148 
 149         @Frequency
 150         long transactionRate;
 151     }
 152 
 153 }