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.consumer;
  26 
  27 import static jdk.test.lib.Asserts.assertEquals;
  28 import static jdk.test.lib.Asserts.assertFalse;
  29 import static jdk.test.lib.Asserts.assertNotNull;
  30 import static jdk.test.lib.Asserts.assertTrue;
  31 
  32 import java.util.List;
  33 
  34 import jdk.jfr.Event;
  35 import jdk.jfr.Recording;
  36 import jdk.jfr.consumer.RecordedEvent;
  37 import jdk.jfr.consumer.RecordedMethod;
  38 import jdk.jfr.consumer.RecordedFrame;
  39 import jdk.jfr.consumer.RecordedStackTrace;
  40 import jdk.test.lib.jfr.Events;
  41 
  42 
  43 /**
  44  * @test
  45  * @key jfr
  46  *
  47  * @library /lib /
  48  * @run main/othervm jdk.jfr.api.consumer.TestRecordedMethodDescriptor
  49  */
  50 public final class TestRecordedMethodDescriptor {
  51 
  52     private static boolean isMainMethodDescriptorRecorded;
  53     private static final String MAIN_METHOD_DESCRIPTOR = "([Ljava/lang/String;)V";
  54     private static final String MAIN_METHOD_NAME = "main";
  55 
  56     public static void main(String[] args) throws Throwable {
  57         Recording recording = new Recording();
  58         recording.enable(MyEvent.class).withStackTrace();
  59         recording.start();
  60 
  61         MyEvent event = new MyEvent();
  62         event.begin();
  63         event.end();
  64         event.commit();
  65         recording.stop();
  66 
  67         List<RecordedEvent> recordedEvents = Events.fromRecording(recording);
  68         assertEquals(1, recordedEvents.size(), "Expected one event");
  69         RecordedEvent recordedEvent = recordedEvents.get(0);
  70 
  71         RecordedStackTrace stacktrace = recordedEvent.getStackTrace();
  72         List<RecordedFrame> frames = stacktrace.getFrames();
  73         assertFalse(frames.isEmpty(), "Stacktrace frames was empty");
  74         for (RecordedFrame frame : frames) {
  75             analyzeRecordedMethodDescriptor(frame.getMethod());
  76         }
  77 
  78         assertTrue(isMainMethodDescriptorRecorded, "main() method descriptor has never been recorded");
  79     }
  80 
  81     private static void analyzeRecordedMethodDescriptor(RecordedMethod method) {
  82 
  83         String descr = method.getDescriptor();
  84         assertNotNull(descr, "Method descriptor is null");
  85         String name = method.getName();
  86         assertNotNull(name, "Method name is null");
  87 
  88         if (name.equals(MAIN_METHOD_NAME) && descr.equals(MAIN_METHOD_DESCRIPTOR)) {
  89             assertFalse(isMainMethodDescriptorRecorded, "main() method descriptor already recorded");
  90             isMainMethodDescriptorRecorded = true;
  91         }
  92     }
  93 
  94     public static class MyEvent extends Event {
  95     }
  96 
  97 }