1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  */
18 
19 package org.apache.harmony.jpda.tests.jdwp.VirtualMachine;
20 
21 import org.apache.harmony.jpda.tests.framework.TestErrorException;
22 import org.apache.harmony.jpda.tests.share.JPDADebuggeeSynchronizer;
23 import org.apache.harmony.jpda.tests.share.SyncDebuggee;
24 
25 /**
26  * Debuggee for {@link DisposeDuringInvokeTest}.
27  */
28 public class DisposeDuringInvokeDebuggee extends SyncDebuggee {
29     public static final String INVOKED_METHOD_NAME = "invokeMethodWithSynchronization";
30     public static final String BREAKPOINT_METHOD_NAME = "breakpointMethod";
31     public static final String THIS_FIELD_NAME = "thisObject";
32 
33     // This field holds the receiver to invoke the invokeMethodWithSynchronization method.
34     public static DisposeDuringInvokeDebuggee thisObject;
35 
36     private class DebuggeeThread extends Thread {
DebuggeeThread()37         public DebuggeeThread() {
38             super("DebuggeeThread");
39         }
40 
run()41         public void run() {
42             breakpointMethod();
43         }
44     }
45 
46     /**
47      * The method used to suspend the DebuggeeThread on a breakpoint.
48      */
breakpointMethod()49     private void breakpointMethod() {
50     }
51 
52     /**
53      * The method called by the DebuggeeThread through JDWP.
54      */
invokeMethodWithSynchronization()55     private void invokeMethodWithSynchronization() {
56         logWriter.println("#START invokeMethodWithSynchronization");
57 
58         // Tell the test we are invoking the requested method.
59         synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_READY);
60 
61         // Wait for the test to send a VirtualMachine.Dispose command and resume us.
62         synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
63 
64         logWriter.println("#END invokeMethodWithSynchronization");
65     }
66 
67     @Override
run()68     public void run() {
69         thisObject = this;
70 
71         DebuggeeThread thrd = new DebuggeeThread();
72         synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_READY);
73 
74         synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
75 
76         logWriter.println("Start DebuggeeThread");
77         thrd.start();
78 
79         logWriter.println("Main thread waits for DebuggeeThread ...");
80         try {
81             thrd.join();
82         } catch (InterruptedException e) {
83            throw new TestErrorException(e);
84         }
85         logWriter.println("DebuggeeThread is finished");
86 
87         // Tell the test we successfully waited for the end of DebuggeeThread.
88         synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_READY);
89 
90         // Wait for the test to resume us.
91         synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
92     }
93 
main(String [] args)94     public static void main(String [] args) {
95         runDebuggee(DisposeDuringInvokeDebuggee.class);
96     }
97 }
98