KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tctest > ClassAdapterTestTarget


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
3  * notice. All rights reserved.
4  */

5 package com.tctest;
6
7 import java.io.ObjectStreamField JavaDoc;
8 import java.util.ArrayList JavaDoc;
9 import java.util.List JavaDoc;
10
11 /**
12  * Adaptation target for ClassAdapterTest
13  */

14 public class ClassAdapterTestTarget {
15   private static final String JavaDoc C = ClassAdapterTestTarget.class
16                                                                                     .getName()
17                                                                                   + ".";
18
19   public static final String JavaDoc KEY = C + "key";
20   public static final String JavaDoc CSTR_THROW_EXCEPTION = C + "cstr-throw-exception";
21   public static final String JavaDoc CSTR_AUTOLOCK_NO_EXCEPTION = C + "cstr-autolock-noexception";
22   public static final String JavaDoc CSTR_AUTOLOCK_THROW_EXCEPTION_INSIDE = C
23                                                                                   + "cstr-autolock-throw-exception-inside";
24
25   // This isn't a real serialVersionUID, but it needs to be here for tests
26
private static final long serialVersionUID = 42L;
27
28   // Again, this isn't really for serialization, but it needs to be here for tests
29
private static final ObjectStreamField JavaDoc[] serialPersistentFields = { new java.io.ObjectStreamField JavaDoc(
30                                                                                                                   "foo",
31                                                                                                                   char[].class) };
32
33   List JavaDoc myRoot = new ArrayList JavaDoc();
34
35   public ClassAdapterTestTarget() {
36     String JavaDoc s = System.getProperty(KEY);
37
38     if (s != null) {
39       if (CSTR_THROW_EXCEPTION.equals(s)) { throw new RuntimeException JavaDoc(s); }
40
41       // This funny looking code is here to create mulitple exit paths from this constructor
42
// It is also here to get some autolocking going on
43
synchronized (s) {
44         if (CSTR_AUTOLOCK_THROW_EXCEPTION_INSIDE.equals(s)) { throw new RuntimeException JavaDoc(s); }
45
46         if (!CSTR_AUTOLOCK_NO_EXCEPTION.equals(s)) { throw new AssertionError JavaDoc(s); }
47
48         if (hashCode() != hashCode()) { return; }
49       }
50     }
51   }
52
53   public void doStuff() {
54     myRoot.add(this);
55   }
56
57   public synchronized void synchronizedInstanceMethodWithWideArgs(double d, long l) {
58     System.out.println("You called synchronizedInstanceMethodWithWideArgs(double, long)!");
59   }
60
61   /**
62    * This is a method that should be called to test a lock.
63    */

64   public void instanceMethod() {
65     System.out.println("You called instanceMethod()!");
66   }
67
68   public void instanceMethodThrowsException() throws LockTestThrowsExceptionException {
69     System.out.println("You called lockTestThrowsException()! About to throw an exception...");
70     throw new LockTestThrowsExceptionException();
71   }
72
73   public void instanceMethodWithArguments(int i) {
74     System.out.println("You called instanceMethodWithArguments(" + i + ")");
75   }
76
77   public void instanceMethodWithArguments(int i, String JavaDoc s) {
78     System.out.println("You called instanceMethodWithArguments(" + i + ", " + s + ")");
79   }
80
81   public void instanceMethodWithArgumentsThrowsException(int i, String JavaDoc s) throws LockTestThrowsExceptionException {
82     System.out.println("You called instanceMethodWithArgumentsThrowsException");
83     throwException();
84   }
85
86   public synchronized void synchronizedInstanceMethod() {
87     System.out.println("You called synchronizedInstanceMethod()");
88   }
89
90   public synchronized void synchronizedInstanceMethodThrowsException() throws LockTestThrowsExceptionException {
91     System.out.println("You called synchronizedInstanceMethodThrowsException()! About to throw an exception...");
92     throw new LockTestThrowsExceptionException();
93   }
94
95   public synchronized void synchronizedInstanceMethodWithArguments(int i, String JavaDoc s) {
96     System.out.println("You called synchronizedInstanceMethodWithArguments");
97   }
98
99   public synchronized void synchronizedInstanceMethodWithArgumentsThrowsException(int i, String JavaDoc s)
100       throws LockTestThrowsExceptionException {
101     System.out.println("You called synchronizedInstanceMethodWithArgumentsThrowsException(int, String)");
102     throwException();
103   }
104
105   public void internalSynchronizedInstanceMethod() {
106     synchronized (this) {
107       System.out.println("You called synchronizedInstanceMethod()!");
108     }
109   }
110
111   public void internalSynchronizedInstanceMethodThrowsException() throws LockTestThrowsExceptionException {
112     synchronized (this) {
113       System.out.println("You called internalSynchronizedInstanceMethodThrowsException");
114       throwException();
115     }
116   }
117
118   public void internalSynchronizedInstanceMethodWithArguments(int i, String JavaDoc s) {
119     synchronized (this) {
120       System.out.println("You called internalSynchronizedInstanceMethodWithArguments(int, String)");
121     }
122   }
123
124   public void internalSynchronizedInstanceMethodWithArgumentsThrowsException(int i, String JavaDoc s)
125       throws LockTestThrowsExceptionException {
126     synchronized (this) {
127       System.out.println("You called internalSynchronizedInstanceMethodWithArgumentsThrowsException(int, String)");
128       throwException();
129     }
130   }
131
132   public static void staticMethod() {
133     System.out.println("You called staticMethod()!");
134   }
135
136   public static void staticMethodThrowsException() throws Exception JavaDoc {
137     throwException();
138   }
139
140   public static void staticMethodWithArguments(int i, String JavaDoc s) {
141     System.out.println("You called staticMethodWithArguments(" + i + ", " + s + ")");
142   }
143
144   public static void staticMethodWithArgumentsThrowsException(int i, String JavaDoc s) throws Exception JavaDoc {
145     System.out.println("You called staticMethodWithArgumentsThrowsException(" + i + ", " + s + ")");
146     if (System.currentTimeMillis() > 0) { throw new LockTestThrowsExceptionException(); }
147   }
148
149   public static synchronized void synchronizedStaticMethod() {
150     System.out.println("You called synchronizedStaticMethod()");
151   }
152
153   public static synchronized void synchronizedStaticMethodThrowsException() throws LockTestThrowsExceptionException {
154     System.out.println("You called synchronizedStaticMethodThrowsException()");
155     throwException();
156   }
157
158   public static synchronized void synchronizedStaticMethodWithArguments(int i, String JavaDoc s) {
159     System.out.println("You called synchronizedStaticMethodWithArguments");
160   }
161
162   public static synchronized void synchronizedStaticMethodWithArgumentsThrowsException(int i, String JavaDoc s)
163       throws LockTestThrowsExceptionException {
164     System.out.println("You called synchronizedStaticMethodWithArgumentsThrowsException(int, String)");
165     throwException();
166   }
167
168   public static void internalSynchronizedStaticMethod() {
169     Object JavaDoc o = new Object JavaDoc();
170     System.out.println("internalSynchronizedStaticMethod(): About to synchronized on " + o + "...");
171     synchronized (o) {
172       System.out.println("You called internalSynchronizedStaticMethod()");
173     }
174   }
175
176   public static void internalSynchronizedStaticMethodThrowsException() throws LockTestThrowsExceptionException {
177     Object JavaDoc o = new Object JavaDoc();
178     synchronized (o) {
179       System.out.println("You called internalSynchronizedStaticMethodThrowsException()");
180       throwException();
181     }
182   }
183
184   public static void internalSynchronizedStaticMethodWithArguments(int i, String JavaDoc s) {
185     Object JavaDoc o = new Object JavaDoc();
186     synchronized (o) {
187       System.out.println("You called internalSynchronizedStaticMethodWithArguments(int, String)");
188     }
189   }
190
191   public static void internalSynchronizedStaticMethodWithArgumentsThrowsException(int i, String JavaDoc s)
192       throws LockTestThrowsExceptionException {
193     Object JavaDoc o = new Object JavaDoc();
194     synchronized (o) {
195       System.out.println("You called internalSynchronizedStaticMethodWithArgumentsThrowsException(int, String)");
196       throwException();
197     }
198   }
199
200   public String JavaDoc instanceMethodReturnsAValue() {
201     String JavaDoc rv = "some return value";
202     System.out.println("You called instanceMethodReturnsAValue(). Returning: " + rv);
203     return rv;
204   }
205
206   public String JavaDoc instanceMethodReturnsAValueThrowsException() throws LockTestThrowsExceptionException {
207     String JavaDoc rv = "You called instanceMethodReturnsAValueThrowsException()";
208     System.out.println(rv);
209     throwException();
210     return rv;
211   }
212
213   public String JavaDoc instanceMethodWithArgumentsReturnsAValue(int i, String JavaDoc s) {
214     String JavaDoc rv = "You called instanceMethodWithArgumentsReturnsAValue(int, String)";
215     System.out.println(rv);
216     return rv;
217   }
218
219   public String JavaDoc instanceMethodWithArgumentsReturnsAValueThrowsException(int i, String JavaDoc s)
220       throws LockTestThrowsExceptionException {
221     String JavaDoc rv = "You called instanceMethodWithArgumentsReturnsAValueThrowsException(int, String)";
222     System.out.println(rv);
223     throwException();
224     return rv;
225   }
226
227   public synchronized String JavaDoc synchronizedInstanceMethodReturnsAValue() {
228     String JavaDoc rv = "some return value";
229     System.out.println("You called synchronizedInstanceMethodReturnsAValue(). Returning " + rv);
230     return rv;
231   }
232
233   public synchronized String JavaDoc synchronizedInstanceMethodReturnsAValueThrowsException()
234       throws LockTestThrowsExceptionException {
235     String JavaDoc rv = "You called synchronizedInstanceMethodReturnsAValueThrowsException()";
236     System.out.println(rv);
237     throwException();
238     return rv;
239   }
240
241   public synchronized String JavaDoc synchronizedInstanceMethodWithArgumentsReturnsAValue(int i, String JavaDoc s) {
242     String JavaDoc rv = "You called synchronizedInstanceMethodWithArgumentsReturnsAValue(int, String)";
243     System.out.println(rv);
244     return rv;
245   }
246
247   public synchronized String JavaDoc synchronizedInstanceMethodWithArgumentsReturnsAValueThrowsException(int i, String JavaDoc s)
248       throws LockTestThrowsExceptionException {
249     String JavaDoc rv = "You called synchronizedInstanceMethodWithArgumentsReturnsAValueThrowsException(int, String)";
250     System.out.println("rv");
251     throwException();
252     return rv;
253   }
254
255   public String JavaDoc internalSynchronizedInstanceMethodReturnsAValue() {
256     String JavaDoc rv = "You called internalSynchronizedInstanceMethodReturnsAValue()";
257     synchronized (this) {
258       System.out.println(rv);
259     }
260     return rv;
261   }
262
263   public String JavaDoc internalSynchronizedInstanceMethodReturnsAValueThrowsException()
264       throws LockTestThrowsExceptionException {
265     String JavaDoc rv = "You called internalSynchronizedInstanceMethodReturnsAValueThrowsException()";
266     synchronized (this) {
267       System.out.println(rv);
268       throwException();
269     }
270     return rv;
271   }
272
273   public String JavaDoc internalSynchronizedInstanceMethodWithArgumentsReturnsAValue(int i, String JavaDoc s) {
274     String JavaDoc rv = "You called internalSynchronizedInstanceMethodWithArgumentsReturnsAValue(int, String)";
275     synchronized (this) {
276       System.out.println(rv);
277     }
278     return rv;
279   }
280
281   public String JavaDoc internalSynchronizedInstanceMethodWithArgumentsReturnsAValueThrowsException(int i, String JavaDoc s)
282       throws LockTestThrowsExceptionException {
283     String JavaDoc rv = "You called internalSynchronizedInstanceMethodWithArgumentsReturnsAValueThrowsException";
284     synchronized (this) {
285       System.out.println(rv);
286       throwException();
287     }
288     return rv;
289   }
290
291   public static String JavaDoc staticMethodReturnsAValue() {
292     String JavaDoc rv = "You called staticMethodReturnsAValue";
293     System.out.println(rv);
294     return rv;
295   }
296
297   public static String JavaDoc staticMethodReturnsAValueThrowsException() throws Exception JavaDoc {
298     String JavaDoc rv = "You called staticMethodReturnsAValueThrowsException()";
299     System.out.println(rv);
300     throwException();
301     return rv;
302   }
303
304   public static String JavaDoc staticMethodWithArgumentsReturnsAValue(int i, String JavaDoc s) {
305     String JavaDoc rv = "You called staticMethodWithArgumentsReturnsAValue(int, String)";
306     System.out.println(rv);
307     return rv;
308   }
309
310   public static String JavaDoc staticMethodWithArgumentsReturnsAValueThrowsException(int i, String JavaDoc s) throws Exception JavaDoc {
311     String JavaDoc rv = "You called staticMethodWithArgumentsReturnsAValueThrowsException(int, String)";
312     throwException();
313     return rv;
314   }
315
316   public static synchronized String JavaDoc synchronizedStaticMethodReturnsAValue() {
317     String JavaDoc rv = "You called synchronizedStaticMethodReturnsAValue()";
318     System.out.println(rv);
319     return rv;
320   }
321
322   public static synchronized String JavaDoc synchronizedStaticMethodReturnsAValueThrowsException()
323       throws LockTestThrowsExceptionException {
324     String JavaDoc rv = "You called synchronizedStaticMethodReturnsAValueThrowsException()";
325     System.out.println(rv);
326     throwException();
327     return rv;
328   }
329
330   public static synchronized String JavaDoc synchronizedStaticMethodWithArgumentsReturnsAValue(int i, String JavaDoc s) {
331     String JavaDoc rv = "You called synchronizedStaticMethodWithArgumentsReturnsAValue(int, String)";
332     System.out.println(rv);
333     return rv;
334   }
335
336   public static synchronized String JavaDoc synchronizedStaticMethodWithArgumentsReturnsAValueThrowsException(int i, String JavaDoc s)
337       throws LockTestThrowsExceptionException {
338     String JavaDoc rv = "You called synchronizedStaticMethodWithArgumentsReturnsAValueThrowsException(int i, String s)";
339     System.out.println(rv);
340     throwException();
341     return rv;
342   }
343
344   public static String JavaDoc internalSynchronizedStaticMethodReturnsAValue() {
345     String JavaDoc rv = "You called internalSynchronizedStaticMethodReturnsAValue()";
346     Object JavaDoc o = new Object JavaDoc();
347     synchronized (o) {
348       System.out.println(rv);
349     }
350     return rv;
351   }
352
353   public static String JavaDoc internalSynchronizedStaticMethodReturnsAValueThrowsException()
354       throws LockTestThrowsExceptionException {
355     String JavaDoc rv = "You called internalSynchronizedStaticMethodReturnsAValueThrowsException()";
356     Object JavaDoc o = new Object JavaDoc();
357     synchronized (o) {
358       System.out.println(rv);
359       throwException();
360     }
361     return rv;
362   }
363
364   public static String JavaDoc internalSynchronizedStaticMethodWithArgumentsReturnsAValue(int i, String JavaDoc s) {
365     String JavaDoc rv = "You called internalSynchronizedStaticMethodWithArgumentsReturnsAValue(int, String)";
366     Object JavaDoc o = new Object JavaDoc();
367     synchronized (o) {
368       System.out.println(rv);
369     }
370     return rv;
371   }
372
373   public static String JavaDoc internalSynchronizedStaticMethodWithArgumentsReturnsAValueThrowsException(int i, String JavaDoc s)
374       throws LockTestThrowsExceptionException {
375     String JavaDoc rv = "You called internalSynchronizedStaticMethodWithArgumentsReturnsAValueThrowsException(int, String)";
376     Object JavaDoc o = new Object JavaDoc();
377     synchronized (o) {
378       System.out.println(rv);
379       throwException();
380     }
381     return rv;
382   }
383
384   public int nestedInternalSynchronizedInstanceMethod() {
385     Object JavaDoc obj1 = new Object JavaDoc();
386     Object JavaDoc obj2 = new Object JavaDoc();
387
388     synchronized (obj1) {
389       System.out.println("Synchronized on obj1");
390       synchronized (obj2) {
391         System.out.println("Synchronized on obj2");
392         synchronized (this) {
393           System.out.println("Synchonized on this");
394           // return the number of synchronized blocks.
395
return 3;
396         }
397       }
398     }
399   }
400
401   private static void throwException() throws LockTestThrowsExceptionException {
402     if (System.currentTimeMillis() > 0) { throw new LockTestThrowsExceptionException(); }
403   }
404
405   public static void main(String JavaDoc[] args) {
406     //
407
}
408 }
Popular Tags