1 /*
   2  * Copyright (c) 2014, 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.assertTrue;
  30 
  31 import java.time.Duration;
  32 import java.util.List;
  33 
  34 import javax.script.ScriptEngine;
  35 import javax.script.ScriptEngineManager;
  36 
  37 import jdk.jfr.Event;
  38 import jdk.jfr.Recording;
  39 import jdk.jfr.consumer.RecordedEvent;
  40 import jdk.jfr.consumer.RecordedFrame;
  41 import jdk.jfr.consumer.RecordedStackTrace;
  42 import jdk.test.lib.jfr.Events;
  43 
  44 
  45 /*
  46  * @test
  47  * @key jfr
  48  *
  49  * @library /test/lib
  50  * @modules java.scripting
  51  *          jdk.jfr
  52  *
  53  * @run main/othervm jdk.jfr.api.consumer.TestHiddenMethod
  54  */
  55 public final class TestHiddenMethod {
  56 
  57     public static void main(String[] args) throws Throwable {
  58         Recording recording = new Recording();
  59         recording.enable(MyEvent.class).withThreshold(Duration.ofMillis(0));
  60         recording.start();
  61 
  62         // Commit event with hidden methods
  63         ScriptEngineManager manager = new ScriptEngineManager();
  64         ScriptEngine engine = manager.getEngineByName("nashorn");
  65         engine.eval(
  66                 "function emit() {"
  67                 + "  print('About to emit event from Javascript');"
  68                 + "  var TestEvent = Java.type(\"jdk.jfr.api.consumer.TestHiddenMethod$MyEvent\");"
  69                 + "  var event = new TestEvent;"
  70                 + "  event.begin();"
  71                 + "  event.end();"
  72                 + "  event.commit();"
  73                 + "  print('Event emitted from Javascript!');"
  74                 + "}"
  75                 + "emit();");
  76 
  77         // Commit event with visible method
  78         MyEvent visible = new MyEvent();
  79         visible.begin();
  80         visible.end();
  81         visible.commit();
  82         recording.stop();
  83 
  84         List<RecordedEvent> events = Events.fromRecording(recording);
  85         assertEquals(2, events.size(), "Expected two events");
  86         RecordedEvent hiddenEvent = events.get(0);
  87         RecordedEvent visibleEvent = events.get(1);
  88 
  89         System.out.println("hiddenEvent:" + hiddenEvent);
  90         System.out.println("visibleEvent:" + visibleEvent);
  91 
  92         assertTrue(hasHiddenStackFrame(hiddenEvent), "No hidden frame in hidden event: " + hiddenEvent);
  93         assertFalse(hasHiddenStackFrame(visibleEvent), "Hidden frame in visible event: " + visibleEvent);
  94     }
  95 
  96     private static boolean hasHiddenStackFrame(RecordedEvent event) throws Throwable {
  97         RecordedStackTrace stacktrace = event.getStackTrace();
  98         List<RecordedFrame> frames = stacktrace.getFrames();
  99         assertFalse(frames.isEmpty(), "Stacktrace frames was empty");
 100         for (RecordedFrame frame : frames) {
 101             if (frame.getMethod().isHidden()) {
 102                 return true;
 103             }
 104         }
 105         return false;
 106     }
 107 
 108     public static class MyEvent extends Event {
 109     }
 110 
 111 }