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
19package org.apache.harmony.jpda.tests.jdwp.ThreadReference;
20
21import org.apache.harmony.jpda.tests.share.JPDADebuggeeSynchronizer;
22import org.apache.harmony.jpda.tests.share.SyncDebuggee;
23
24/**
25 * The class specifies debuggee for
26 * <code>org.apache.harmony.jpda.tests.jdwp.ThreadReference.ForceEarlyReturnTest</code>.
27 * This debuggee starts the tested thread which name is supplied by test and
28 * synchronized with test via the <code>SGNL_READY</code> and
29 * <code>SGNL_CONTINUE</code> signals.
30 */
31public class ForceEarlyReturnDebuggee extends SyncDebuggee {
32
33    public static String threadName;
34
35    static boolean isFuncVoidBreak = true;
36
37    static TestObject testObj = new TestObject();
38
39    public static final String THREAD_VOID = "THREAD_VOID";
40
41    public static final String THREAD_OBJECT = "THREAD_OBJECT";
42
43    public static final String THREAD_INT = "THREAD_INT";
44
45    public static final String THREAD_SHORT = "THREAD_SHORT";
46
47    public static final String THREAD_BYTE = "THREAD_BYTE";
48
49    public static final String THREAD_CHAR = "THREAD_CHAR";
50
51    public static final String THREAD_BOOLEAN = "THREAD_BOOLEAN";
52
53    public static final String THREAD_LONG = "THREAD_LONG";
54
55    public static final String THREAD_FLOAT = "THREAD_FLOAT";
56
57    public static final String THREAD_DOUBLE = "THREAD_DOUBLE";
58
59    public static boolean condition = true;
60
61    static Object waitForStart = new Object();
62
63    static Object waitForFinish = new Object();
64
65    @Override
66    public void run() {
67        synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_READY);
68        threadName = synchronizer.receiveMessage();
69        DebuggeeThread thrd = new DebuggeeThread(threadName);
70        synchronized (waitForStart) {
71            thrd.start();
72            try {
73                waitForStart.wait();
74            } catch (InterruptedException e) {
75
76            }
77        }
78        synchronized (waitForFinish) {
79            logWriter.println("thread is finished");
80        }
81
82        synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
83    }
84
85    public Object func_Object() {
86        logWriter.println("In func_Object");
87        synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_READY);
88        while (condition)
89            ;
90        return new Object();
91    }
92
93    public int func_Int() {
94        logWriter.println("In func_Int");
95        synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_READY);
96        while (condition)
97            ;
98        return -1;
99    }
100
101    public short func_Short() {
102        logWriter.println("In func_Short");
103        synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_READY);
104        while (condition)
105            ;
106        return -1;
107    }
108
109    public byte func_Byte() {
110        logWriter.println("In func_Byte");
111        synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_READY);
112        while (condition)
113            ;
114        return -1;
115    }
116
117    public char func_Char() {
118        logWriter.println("In func_Char");
119        synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_READY);
120        while (condition)
121            ;
122        return 'Z';
123    }
124
125    public boolean func_Boolean() {
126        logWriter.println("In func_Boolean");
127        synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_READY);
128        while (condition)
129            ;
130        return false;
131    }
132
133    public long func_Long() {
134        logWriter.println("In func_Long");
135        synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_READY);
136        while (condition)
137            ;
138        return -1;
139    }
140
141    public float func_Float() {
142        logWriter.println("In func_Float");
143        synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_READY);
144        while (condition)
145            ;
146        return -1;
147    }
148
149    public double func_Double() {
150        logWriter.println("In func_Double");
151        synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_READY);
152        while (condition)
153            ;
154        return -1;
155    }
156
157    public void func_Void() {
158        logWriter.println("In func_Void");
159        synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_READY);
160        while (condition)
161            ;
162        isFuncVoidBreak = false;
163        return;
164    }
165
166    class DebuggeeThread extends Thread {
167
168        public DebuggeeThread(String name) {
169            super(name);
170        }
171
172        @Override
173        public void run() {
174
175            synchronized (ForceEarlyReturnDebuggee.waitForFinish) {
176
177                synchronized (ForceEarlyReturnDebuggee.waitForStart) {
178
179                    ForceEarlyReturnDebuggee.waitForStart.notifyAll();
180
181                    logWriter.println(getName() + ": started");
182
183                    if (getName().equals(THREAD_OBJECT)) {
184                        Object result = func_Object();
185                        logWriter.println(getName() + ": " + "Object");
186                        if (result instanceof TestObject) {
187                            synchronizer.sendMessage("TRUE");
188                        } else {
189                            synchronizer.sendMessage("FALSE");
190                        }
191                        logWriter
192                                .println(getName() + ": func_Object returned.");
193
194                    } else if (getName().equals(THREAD_INT)) {
195                        int result = func_Int();
196                        logWriter.println(getName() + ": " + result);
197                        synchronizer
198                                .sendMessage(new Integer(result).toString());
199                        logWriter.println(getName() + ": func_Int returned.");
200                    } else if (getName().equals(THREAD_SHORT)) {
201                        short result = func_Short();
202                        logWriter.println(getName() + ": " + result);
203                        synchronizer
204                                .sendMessage(new Integer(result).toString());
205                        logWriter.println(getName() + ": func_Short returned.");
206                    } else if (getName().equals(THREAD_BYTE)) {
207                        byte result = func_Byte();
208                        logWriter.println(getName() + ": " + result);
209                        synchronizer
210                                .sendMessage(new Integer(result).toString());
211                        logWriter.println(getName() + ": func_Byte returned.");
212                    } else if (getName().equals(THREAD_CHAR)) {
213                        char result = func_Char();
214                        logWriter.println(getName() + ": " + result);
215                        synchronizer.sendMessage(new Character(result)
216                                .toString());
217                        logWriter.println(getName() + ": func_Char returned.");
218                    } else if (getName().equals(THREAD_BOOLEAN)) {
219                        Boolean result = func_Boolean();
220                        logWriter.println(getName() + ": " + result);
221                        synchronizer
222                                .sendMessage(new Boolean(result).toString());
223                        logWriter.println(getName()
224                                + ": func_Boolean returned.");
225                    } else if (getName().equals(THREAD_LONG)) {
226                        long result = func_Long();
227                        logWriter.println(getName() + ": " + result);
228                        synchronizer.sendMessage(new Long(result).toString());
229                        logWriter.println(getName() + ": func_Long returned.");
230                    } else if (getName().equals(THREAD_FLOAT)) {
231                        float result = func_Float();
232                        logWriter.println(getName() + ": " + result);
233                        synchronizer.sendMessage(new Float(result).toString());
234                        logWriter.println(getName() + ": func_Float returned.");
235                    } else if (getName().equals(THREAD_DOUBLE)) {
236                        double result = func_Double();
237                        logWriter.println(getName() + ": " + result);
238                        synchronizer.sendMessage(new Double(result).toString());
239                        logWriter
240                                .println(getName() + ": func_Double returned.");
241                    } else if (getName().equals(THREAD_VOID)) {
242                        func_Void();
243                        logWriter.println(getName() + ": " + "void");
244                        if (isFuncVoidBreak) {
245                            synchronizer.sendMessage("TRUE");
246                        } else {
247                            synchronizer.sendMessage("FALSE");
248                        }
249                        logWriter.println(getName() + ": func_Void returned.");
250                    } else {
251                        logWriter.println(getName() + ": no func is called.");
252                        synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_READY);
253                        synchronizer.receiveMessage("ThreadExit");
254                    }
255
256                    logWriter.println(getName() + ": finished");
257
258                }
259            }
260        }
261    }
262
263    public static void main(String[] args) {
264        runDebuggee(ForceEarlyReturnDebuggee.class);
265    }
266
267}
268
269class TestObject {
270
271}
272