1 /*
   2  * Copyright (c) 2013, 2015, 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.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /**
  25  * @test
  26  * @summary Test stepping through lambdas
  27  * @author Staffan Larsen
  28  *
  29  * @run build TestScaffold VMConnection TargetListener TargetAdapter
  30  * @run compile -g LambdaStepTest.java
  31  * @run driver LambdaStepTest
  32  */
  33 import com.sun.jdi.LocalVariable;
  34 import com.sun.jdi.ObjectReference;
  35 import com.sun.jdi.ReferenceType;
  36 import com.sun.jdi.StackFrame;
  37 import com.sun.jdi.StringReference;
  38 import com.sun.jdi.ThreadReference;
  39 import com.sun.jdi.event.BreakpointEvent;
  40 import com.sun.jdi.event.StepEvent;
  41 
  42  /********** target program **********/
  43 
  44 interface DefaultTest {
  45     default void defaultMethod() {
  46         String from = "default";
  47         System.out.println("Hello from " + from);
  48     }
  49 }
  50 class LambdaStepTestTarg implements DefaultTest {
  51     private void test() {
  52         String from = "test";
  53         System.out.println("Hello from " + from);
  54     }
  55     private static void instanceTest() {
  56         LambdaStepTestTarg l = new LambdaStepTestTarg();
  57         l.test();
  58     }
  59     private static void lambdaTest() {
  60         Runnable r = () -> {
  61             String from = "lambda";
  62             System.out.println("Hello from " + from);
  63         };
  64         r.run();
  65     }
  66     private static void defaultTest() {
  67         LambdaStepTestTarg l = new LambdaStepTestTarg();
  68         l.defaultMethod();
  69     }
  70     public static void main(String[] args) {
  71         instanceTest();
  72         lambdaTest();
  73         defaultTest();
  74         System.out.println("Goodbye from LambdaStepTestTarg!");
  75     }
  76 }
  77 
  78 
  79  /********** test program **********/
  80 
  81 public class LambdaStepTest extends TestScaffold {
  82     LambdaStepTest (String args[]) {
  83         super(args);
  84     }
  85 
  86     public static void main(String[] args)
  87         throws Exception
  88     {
  89         new LambdaStepTest (args).startTests();
  90     }
  91 
  92     /********** test core **********/
  93 
  94     protected void runTests()
  95         throws Exception
  96     {
  97         // ## Normal instance method
  98 
  99         BreakpointEvent bpe = startTo("LambdaStepTestTarg", "instanceTest", "()V");
 100         ThreadReference thread = bpe.thread();
 101 
 102         // step over allocation
 103         StepEvent se = stepOverLine(thread);
 104         System.out.println(se.thread().frame(0));
 105 
 106         // step into test();
 107         se = stepIntoLine(thread);
 108         System.out.println(se.thread().frame(0));
 109 
 110         // step over variable initialization
 111         se = stepOverLine(thread);
 112         System.out.println(se.thread().frame(0));
 113 
 114         // get value of variable "from"
 115         StackFrame frame = se.thread().frame(0);
 116         LocalVariable lv = frame.visibleVariableByName("from");
 117         System.out.println(lv);
 118         StringReference sr = (StringReference) frame.getValue(lv);
 119         if (!sr.value().equals("test")) {
 120             throw new Exception("Unexpected variable value in instanceTest: "+sr.value());
 121         }
 122 
 123 
 124         // ## Lambda method
 125 
 126         bpe = resumeTo("LambdaStepTestTarg", "lambdaTest", "()V");
 127         thread = bpe.thread();
 128 
 129         // step over allocation
 130         se = stepOverLine(thread);
 131         System.out.println(se.thread().frame(0));
 132 
 133         // step into run() of the lambda
 134         se = stepIntoLine(thread);
 135         System.out.println(se.thread().frame(0));
 136 
 137         // step over variable initialization
 138         se = stepOverLine(thread);
 139         System.out.println(se.thread().frame(0));
 140 
 141         // get value of variable "from"
 142         frame = se.thread().frame(0);
 143         lv = frame.visibleVariableByName("from");
 144         System.out.println(lv);
 145         sr = (StringReference) frame.getValue(lv);
 146         if (!sr.value().equals("lambda")) {
 147             throw new Exception("Unexpected variable value in lambdaTest: "+sr.value());
 148         }
 149 
 150 
 151         // ## Default method
 152 
 153         bpe = resumeTo("LambdaStepTestTarg", "defaultTest", "()V");
 154         thread = bpe.thread();
 155 
 156         // step over allocation
 157         se = stepOverLine(thread);
 158         System.out.println(se.thread().frame(0));
 159 
 160         // step into defaultMethod()
 161         se = stepIntoLine(thread);
 162         System.out.println(se.thread().frame(0));
 163 
 164         // step over variable initialization
 165         se = stepOverLine(thread);
 166         System.out.println(se.thread().frame(0));
 167 
 168         // get value of variable "from"
 169         frame = se.thread().frame(0);
 170         lv = frame.visibleVariableByName("from");
 171         System.out.println(lv);
 172         sr = (StringReference) frame.getValue(lv);
 173         if (!sr.value().equals("default")) {
 174             throw new Exception("Unexpected variable value in lambdaTest: "+sr.value());
 175         }
 176 
 177 
 178         /*
 179          * resume the target listening for events
 180          */
 181         listenUntilVMDisconnect();
 182 
 183     }
 184 }