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 * 28 * @author Staffan Larsen 29 * 30 * @modules jdk.jdi 31 * @run build TestScaffold VMConnection TargetListener TargetAdapter 32 * @run compile -g LambdaStepTest.java 33 * @run driver LambdaStepTest 34 */ 35 import com.sun.jdi.LocalVariable; 36 import com.sun.jdi.ObjectReference; 37 import com.sun.jdi.ReferenceType; 38 import com.sun.jdi.StackFrame; 39 import com.sun.jdi.StringReference; 40 import com.sun.jdi.ThreadReference; 41 import com.sun.jdi.event.BreakpointEvent; 42 import com.sun.jdi.event.StepEvent; 43 44 /********** target program **********/ 45 46 interface DefaultTest { 47 default void defaultMethod() { 48 String from = "default"; 49 System.out.println("Hello from " + from); 50 } 51 } 52 class LambdaStepTestTarg implements DefaultTest { 53 private void test() { 54 String from = "test"; 55 System.out.println("Hello from " + from); 56 } 57 private static void instanceTest() { 58 LambdaStepTestTarg l = new LambdaStepTestTarg(); 59 l.test(); 60 } 61 private static void lambdaTest() { 62 Runnable r = () -> { 63 String from = "lambda"; 64 System.out.println("Hello from " + from); 65 }; 66 r.run(); 67 } 68 private static void defaultTest() { 69 LambdaStepTestTarg l = new LambdaStepTestTarg(); 70 l.defaultMethod(); 71 } 72 public static void main(String[] args) { 73 instanceTest(); 74 lambdaTest(); 75 defaultTest(); 76 System.out.println("Goodbye from LambdaStepTestTarg!"); 77 } 78 } 79 80 81 /********** test program **********/ 82 83 public class LambdaStepTest extends TestScaffold { 84 LambdaStepTest (String args[]) { 85 super(args); 86 } 87 88 public static void main(String[] args) 89 throws Exception 90 { 91 new LambdaStepTest (args).startTests(); 92 } 93 94 /********** test core **********/ 95 96 protected void runTests() 97 throws Exception 98 { 99 // ## Normal instance method 100 101 BreakpointEvent bpe = startTo("LambdaStepTestTarg", "instanceTest", "()V"); 102 ThreadReference thread = bpe.thread(); 103 104 // step over allocation 105 StepEvent se = stepOverLine(thread); 106 System.out.println(se.thread().frame(0)); 107 108 // step into test(); 109 se = stepIntoLine(thread); 110 System.out.println(se.thread().frame(0)); 111 112 // step over variable initialization 113 se = stepOverLine(thread); 114 System.out.println(se.thread().frame(0)); 115 116 // get value of variable "from" 117 StackFrame frame = se.thread().frame(0); 118 LocalVariable lv = frame.visibleVariableByName("from"); 119 System.out.println(lv); 120 StringReference sr = (StringReference) frame.getValue(lv); 121 if (!sr.value().equals("test")) { 122 throw new Exception("Unexpected variable value in instanceTest: "+sr.value()); 123 } 124 125 126 // ## Lambda method 127 128 bpe = resumeTo("LambdaStepTestTarg", "lambdaTest", "()V"); 129 thread = bpe.thread(); 130 131 // step over allocation 132 se = stepOverLine(thread); 133 System.out.println(se.thread().frame(0)); 134 135 // step into run() of the lambda 136 se = stepIntoLine(thread); 137 System.out.println(se.thread().frame(0)); 138 139 // step over variable initialization 140 se = stepOverLine(thread); 141 System.out.println(se.thread().frame(0)); 142 143 // get value of variable "from" 144 frame = se.thread().frame(0); 145 lv = frame.visibleVariableByName("from"); 146 System.out.println(lv); 147 sr = (StringReference) frame.getValue(lv); 148 if (!sr.value().equals("lambda")) { 149 throw new Exception("Unexpected variable value in lambdaTest: "+sr.value()); 150 } 151 152 153 // ## Default method 154 155 bpe = resumeTo("LambdaStepTestTarg", "defaultTest", "()V"); 156 thread = bpe.thread(); 157 158 // step over allocation 159 se = stepOverLine(thread); 160 System.out.println(se.thread().frame(0)); 161 162 // step into defaultMethod() 163 se = stepIntoLine(thread); 164 System.out.println(se.thread().frame(0)); 165 166 // step over variable initialization 167 se = stepOverLine(thread); 168 System.out.println(se.thread().frame(0)); 169 170 // get value of variable "from" 171 frame = se.thread().frame(0); 172 lv = frame.visibleVariableByName("from"); 173 System.out.println(lv); 174 sr = (StringReference) frame.getValue(lv); 175 if (!sr.value().equals("default")) { 176 throw new Exception("Unexpected variable value in lambdaTest: "+sr.value()); 177 } 178 179 180 /* 181 * resume the target listening for events 182 */ 183 listenUntilVMDisconnect(); 184 185 } 186 }