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