1 /*
   2  * Copyright (c) 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 
  26 package jdk.jfr.api.event;
  27 
  28 import jdk.jfr.Event;
  29 import jdk.jfr.EventType;
  30 import jdk.jfr.Recording;
  31 import jdk.jfr.ValueDescriptor;
  32 import jdk.jfr.consumer.RecordedEvent;
  33 import jdk.test.lib.Asserts;
  34 import jdk.test.lib.jfr.Events;
  35 
  36 /**
  37  * @test
  38  * @summary Test with event class inheritance
  39  * @key jfr
  40  *
  41  * @library /lib /
  42  * @run main/othervm jdk.jfr.api.event.TestExtends
  43  */
  44 
  45 public class TestExtends {
  46 
  47     private static final int DEFAULT_FIELD_COUNT = 4;
  48 
  49     @SuppressWarnings("unused")
  50     private static class GrandpaEvent extends Event {
  51         public int gPublicField = 4;
  52         protected int gProtectedField = 3;
  53         private int gPrivateField = 2;
  54         int gDefaultField = 1;
  55         private int hiddenField = 4711;
  56     }
  57 
  58     @SuppressWarnings("unused")
  59     private static class ParentEvent extends GrandpaEvent {
  60         public int pPublicField = 40;
  61         protected int pProtectedField = 30;
  62         private int pPrivateField = 20;
  63         int pDefaultField = 10;
  64         private boolean hiddenField = true;
  65     }
  66 
  67     @SuppressWarnings("unused")
  68     private static class MeEvent extends ParentEvent {
  69         public int mPublicField = 400;
  70         protected int mProtectedField = 300;
  71         private int mPrivateField = 200;
  72         int mDefaultField = 100;
  73         private String hiddenField = "Hidden";
  74     }
  75 
  76     public static void main(String[] args) throws Exception {
  77         Recording r = new Recording();
  78         r.enable(GrandpaEvent.class).withoutStackTrace();
  79         r.enable(ParentEvent.class).withStackTrace();
  80         r.enable(MeEvent.class).withoutStackTrace();
  81         r.start();
  82 
  83         GrandpaEvent g = new GrandpaEvent();
  84         g.commit();
  85 
  86         ParentEvent p = new ParentEvent();
  87         p.commit();
  88 
  89         MeEvent m = new MeEvent();
  90         m.commit();
  91 
  92         r.stop();
  93         for (RecordedEvent re : Events.fromRecording(r)) {
  94             System.out.println(re);
  95         }
  96         // Grandpa
  97         EventType grandpaType = EventType.getEventType(GrandpaEvent.class);
  98         verifyField(grandpaType, "gPublicField");
  99         verifyField(grandpaType, "gProtectedField");
 100         verifyField(grandpaType, "gPrivateField");
 101         verifyField(grandpaType, "gDefaultField");
 102         verifyField(grandpaType, "hiddenField");
 103         verifyFieldCount(grandpaType, 5);
 104 
 105         // Parent
 106         EventType parentType = EventType.getEventType(ParentEvent.class);
 107         verifyField(parentType, "gPublicField");
 108         verifyField(parentType, "gProtectedField");
 109         verifyField(parentType, "gDefaultField");
 110         verifyField(parentType, "pPublicField");
 111         verifyField(parentType, "pProtectedField");
 112         verifyField(parentType, "pPrivateField");
 113         verifyField(parentType, "pDefaultField");
 114         verifyField(parentType, "hiddenField");
 115         verifyFieldCount(parentType, 8);
 116 
 117         // Me
 118         EventType meType = EventType.getEventType(MeEvent.class);
 119         verifyField(meType, "gPublicField");
 120         verifyField(meType, "gProtectedField");
 121         verifyField(meType, "gDefaultField");
 122         verifyField(meType, "pPublicField");
 123         verifyField(meType, "pProtectedField");
 124         verifyField(meType, "pDefaultField");
 125         verifyField(meType, "mPublicField");
 126         verifyField(meType, "mProtectedField");
 127         verifyField(meType, "mPrivateField");
 128         verifyField(meType, "mDefaultField");
 129         verifyField(meType, "hiddenField");
 130         verifyFieldCount(meType, 11);
 131 
 132         for (RecordedEvent re : Events.fromRecording(r)) {
 133             System.out.println(re);
 134         }
 135 
 136         RecordedEvent grandpa = findEvent(r, GrandpaEvent.class.getName());
 137         Asserts.assertEquals(grandpa.getValue("gPublicField"), 4);
 138         Asserts.assertEquals(grandpa.getValue("gProtectedField"), 3);
 139         Asserts.assertEquals(grandpa.getValue("gPrivateField"), 2);
 140         Asserts.assertEquals(grandpa.getValue("gDefaultField"), 1);
 141         Asserts.assertEquals(grandpa.getValue("hiddenField"), 4711);
 142 
 143         RecordedEvent parent = findEvent(r, ParentEvent.class.getName());
 144         Asserts.assertEquals(parent.getValue("gPublicField"), 4);
 145         Asserts.assertEquals(parent.getValue("gProtectedField"), 3);
 146         Asserts.assertEquals(parent.getValue("gDefaultField"), 1);
 147         Asserts.assertEquals(parent.getValue("pPublicField"), 40);
 148         Asserts.assertEquals(parent.getValue("pProtectedField"), 30);
 149         Asserts.assertEquals(parent.getValue("pPrivateField"), 20);
 150         Asserts.assertEquals(parent.getValue("pDefaultField"), 10);
 151         Asserts.assertEquals(parent.getValue("hiddenField"), true);
 152 
 153         RecordedEvent me = findEvent(r, MeEvent.class.getName());
 154         Asserts.assertEquals(me.getValue("gPublicField"), 4);
 155         Asserts.assertEquals(me.getValue("gProtectedField"), 3);
 156         Asserts.assertEquals(me.getValue("gDefaultField"), 1);
 157         Asserts.assertEquals(me.getValue("pPublicField"), 40);
 158         Asserts.assertEquals(me.getValue("pProtectedField"), 30);
 159         Asserts.assertEquals(me.getValue("pDefaultField"), 10);
 160         Asserts.assertEquals(me.getValue("mPublicField"), 400);
 161         Asserts.assertEquals(me.getValue("mProtectedField"), 300);
 162         Asserts.assertEquals(me.getValue("mPrivateField"), 200);
 163         Asserts.assertEquals(me.getValue("mDefaultField"), 100);
 164         Asserts.assertEquals(me.getValue("hiddenField"), "Hidden");
 165     }
 166 
 167     private static RecordedEvent findEvent(Recording r, String name) throws Exception {
 168         for (RecordedEvent re : Events.fromRecording(r)) {
 169             if (re.getEventType().getName().equals(name)) {
 170                 return re;
 171             }
 172         }
 173         throw new Exception("Event type hierarchy exist, but missing event " + name + " from recording");
 174     }
 175 
 176     private static void verifyFieldCount(EventType t, int count) throws Exception {
 177         if (t.getFields().size() != count + DEFAULT_FIELD_COUNT) {
 178             throw new Exception("Incorrect number of fields " + count);
 179         }
 180     }
 181 
 182     private static void verifyField(EventType t, String name) throws Exception {
 183         ValueDescriptor d = t.getField(name);
 184         if (d == null) {
 185             throw new Exception("Missing field " + name + " in event " + t.getName());
 186         }
 187     }
 188 }