KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > codehaus > groovy > classgen > DumpClass


1 /*
2  $Id: DumpClass.java,v 1.21 2004/01/20 09:52:17 jstrachan Exp $
3
4  Copyright 2003 (C) James Strachan and Bob Mcwhirter. All Rights Reserved.
5
6  Redistribution and use of this software and associated documentation
7  ("Software"), with or without modification, are permitted provided
8  that the following conditions are met:
9
10  1. Redistributions of source code must retain copyright
11     statements and notices. Redistributions must also contain a
12     copy of this document.
13
14  2. Redistributions in binary form must reproduce the
15     above copyright notice, this list of conditions and the
16     following disclaimer in the documentation and/or other
17     materials provided with the distribution.
18
19  3. The name "groovy" must not be used to endorse or promote
20     products derived from this Software without prior written
21     permission of The Codehaus. For written permission,
22     please contact info@codehaus.org.
23
24  4. Products derived from this Software may not be called "groovy"
25     nor may "groovy" appear in their names without prior written
26     permission of The Codehaus. "groovy" is a registered
27     trademark of The Codehaus.
28
29  5. Due credit should be given to The Codehaus -
30     http://groovy.codehaus.org/
31
32  THIS SOFTWARE IS PROVIDED BY THE CODEHAUS AND CONTRIBUTORS
33  ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
34  NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
35  FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
36  THE CODEHAUS OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
37  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
38  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
39  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
40  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
41  STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
42  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
43  OF THE POSSIBILITY OF SUCH DAMAGE.
44
45  */

46
47 package org.codehaus.groovy.classgen;
48
49 import groovy.lang.Closure;
50 import groovy.lang.MetaClass;
51
52 import java.util.Iterator JavaDoc;
53 import java.util.List JavaDoc;
54 import java.util.Map JavaDoc;
55
56 import org.codehaus.groovy.runtime.InvokerHelper;
57
58 /**
59  * This is a scratch class used to experiment with ASM to see what kind of
60  * stuff is output for normal Java code
61  *
62  * @author <a HREF="mailto:james@coredevelopers.net">James Strachan</a>
63  * @version $Revision: 1.21 $
64  */

65 public class DumpClass {
66
67     private String JavaDoc bar;
68     private String JavaDoc result;
69     private Object JavaDoc x;
70
71     public String JavaDoc getResult() {
72         return result;
73     }
74
75     public String JavaDoc getBar() {
76         return bar;
77     }
78
79     public void setBar(String JavaDoc value) {
80         this.bar = value;
81     }
82
83     public void iterateOverList() {
84         // equivalent of
85
// for i in ["a", "b", "c"] {
86
// System.out.println(i);
87
// }
88
List JavaDoc list = InvokerHelper.createList(new Object JavaDoc[] { "a", "b", "c" });
89         for (Iterator JavaDoc iter = InvokerHelper.asIterator(list); iter.hasNext();) {
90             Object JavaDoc i = iter.next();
91             InvokerHelper.invokeMethod(System.out, "println", i);
92         }
93     }
94
95     public void iterateOverMap() {
96         Map JavaDoc map = InvokerHelper.createMap(new Object JavaDoc[] { "a", "x", "b", "y", "c", "z" });
97         for (Iterator JavaDoc iter = InvokerHelper.asIterator(map); iter.hasNext();) {
98             Object JavaDoc i = iter.next();
99             InvokerHelper.invokeMethod(System.out, "println", i);
100         }
101     }
102
103     public void printValues(Object JavaDoc collection) {
104         for (Iterator JavaDoc iter = InvokerHelper.asIterator(collection); iter.hasNext();) {
105             Object JavaDoc foo = iter.next();
106             InvokerHelper.invokeMethod(System.out, "println", foo);
107         }
108     }
109
110     public Object JavaDoc emptyMethod() {
111         return null;
112     }
113
114     public void emptyVoidMethod() {
115     }
116
117     // public void testAssertion() {
118
// assert bar == null;
119
// assert result == null : "message";
120
// }
121

122     public void testGroovyAssertion2() {
123         if (InvokerHelper.compareEqual(bar, "foo")) {
124         }
125         else {
126             InvokerHelper.assertFailed("expression", "message");
127         }
128
129         bar = "abc";
130
131         if (InvokerHelper.compareNotEqual(bar, "foo")) {
132         }
133         else {
134             InvokerHelper.assertFailed("expression", "not null");
135         }
136
137         if (InvokerHelper.compareEqual(bar, "abc")) {
138         }
139         else {
140             InvokerHelper.assertFailed("expression", "not null");
141         }
142     }
143
144     public void testFieldSet() {
145         if (InvokerHelper.compareNotEqual(x, "foo")) {
146         }
147         else {
148             InvokerHelper.assertFailed("expression", "message");
149         }
150
151         x = "foo";
152
153         if (InvokerHelper.compareEqual(x, "foo")) {
154         }
155         else {
156             InvokerHelper.assertFailed("expression", "message");
157         }
158         if (InvokerHelper.compareNotEqual(x, "foo")) {
159         }
160         else {
161             InvokerHelper.assertFailed("expression", "message");
162         }
163     }
164     public void assertFailed() {
165         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc("Exception: ");
166         buffer.append("x = ");
167         buffer.append(x);
168         InvokerHelper.assertFailed(buffer, "message");
169     }
170
171     public void setLocalVar() {
172         Object JavaDoc x = null;
173         Object JavaDoc i = null;
174         for (Iterator JavaDoc iter = InvokerHelper.asIterator(InvokerHelper.createRange(new Integer JavaDoc(0), new Integer JavaDoc(10), true));
175             iter.hasNext();
176             ) {
177             i = iter.next();
178             x = i;
179         }
180     }
181
182     public void testGroovyAssertion() {
183         x = "abc";
184         if (InvokerHelper.compareEqual(x, "foo")) {
185         }
186         else {
187             InvokerHelper.assertFailed("expression", "message");
188         }
189     }
190
191     public void doPlus() {
192         Object JavaDoc z = "abcd";
193         x = InvokerHelper.invokeMethod(z, "length", null);
194     }
195
196     public void setBoolean() {
197         x = Boolean.TRUE;
198     }
199
200     public void tryCatch() {
201         try {
202             InvokerHelper.invokeMethod(this, "testGroovyAssertion", null);
203         }
204         catch (AssertionError JavaDoc e) {
205             InvokerHelper.invokeMethod(this, "onException", e);
206         }
207         finally {
208             InvokerHelper.invokeMethod(this, "finallyBlock", null);
209         }
210         InvokerHelper.invokeMethod(this, "afterTryCatch", null);
211     }
212
213     public void doPrintln() {
214         Object JavaDoc value = InvokerHelper.getProperty(System JavaDoc.class, "out");
215         InvokerHelper.invokeMethod(value, "println", "Hello");
216     }
217
218     public void doClosure() {
219         x = new Closure(this) {
220             public Object JavaDoc call(Object JavaDoc arguments) {
221                 System.out.println();
222                 return null;
223             }
224
225             public MetaClass getMetaClass() {
226                 return null;
227             }
228
229             public void setMetaClass(MetaClass metaClass) {
230             }
231         };
232     }
233
234     public Object JavaDoc ifDemo() {
235         if (InvokerHelper.compareEqual(bar, "abc")) {
236             return Boolean.TRUE;
237         }
238         else {
239             return Boolean.FALSE;
240         }
241     }
242
243     public void testWhile() {
244         while (InvokerHelper.compareEqual(bar, "abc")) {
245             System.out.println("Hello");
246         }
247     }
248
249     public void testDoWhile() {
250         do {
251             System.out.println("Hello");
252         }
253         while (InvokerHelper.compareEqual(bar, "abc"));
254     }
255 }
256
Popular Tags