1   /*
2    * $Id: DumpClass4.java,v 1.14 2004/03/06 15:53:15 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 that the
8    * following conditions are met:
9    *  1. Redistributions of source code must retain copyright statements and
10   * notices. Redistributions must also contain a copy of this document.
11   *  2. Redistributions in binary form must reproduce the above copyright
12   * notice, this list of conditions and the following disclaimer in the
13   * documentation and/or other materials provided with the distribution.
14   *  3. The name "groovy" must not be used to endorse or promote products
15   * derived from this Software without prior written permission of The Codehaus.
16   * For written permission, please contact info@codehaus.org.
17   *  4. Products derived from this Software may not be called "groovy" nor may
18   * "groovy" appear in their names without prior written permission of The
19   * Codehaus. "groovy" is a registered trademark of The Codehaus.
20   *  5. Due credit should be given to The Codehaus - http://groovy.codehaus.org/
21   * 
22   * THIS SOFTWARE IS PROVIDED BY THE CODEHAUS AND CONTRIBUTORS ``AS IS'' AND ANY
23   * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24   * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25   * DISCLAIMED. IN NO EVENT SHALL THE CODEHAUS OR ITS CONTRIBUTORS BE LIABLE FOR
26   * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30   * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31   * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
32   * DAMAGE.
33   *  
34   */
35  
36  package org.codehaus.groovy.classgen;
37  
38  import groovy.lang.Reference;
39  
40  import org.codehaus.groovy.runtime.InvokerHelper;
41  
42  /***
43   * This is a scratch class used to experiment with ASM to see what kind of
44   * stuff is output for normal Java code
45   * 
46   * @author <a href="mailto:james@coredevelopers.net">James Strachan</a>
47   * @version $Revision: 1.14 $
48   */
49  public class DumpClass4 {
50  
51      public DumpClass4() {
52      }
53  
54      public static void main(String[] args) {
55          Object foo = InvokerHelper.invokeConstructorOf(DumpClass4.class, null);
56      }
57  
58      public void run() {
59          synchronized (this) {
60              InvokerHelper.invokeMethod(this, "foo", null);
61          }
62          InvokerHelper.invokeMethod(this, "bar", null);
63      }
64  
65      public void throwException() {
66          throw (RuntimeException) InvokerHelper.invokeConstructor("java.lang.RuntimeException", "Hello");
67      }
68  
69      public void switchTest(int i) {
70          String x = "foo";
71  
72          switch (i) {
73              case 1 :
74                  x = "1";
75                  break;
76              case 2 :
77                  x = "2";
78              case 3 :
79                  x = "3";
80                  break;
81              default :
82                  x = "default";
83          }
84          System.out.println(x);
85      }
86  
87      public Object createReferenceTest() {
88          Reference foo = new Reference();
89          foo.set(new Integer(12));
90          return foo.get();
91      }
92  
93      public static void makeInstance() {
94          InvokerHelper.invokeConstructorOf(DumpClass4.class, null);
95      }
96  
97      public void makeNestedArray() {
98          InvokerHelper.invokeMethod(
99              "outer",
100             "foo",
101             new Object[] { InvokerHelper.invokeMethod("inner", "plus", new Object[] { "1" })
102         });
103     }
104 
105     public void makeNestedEmptyArray() {
106         InvokerHelper
107             .invokeMethod("outer", "foo", new Object[] { InvokerHelper.invokeMethod("inner", "plus", new Object[] {
108             })
109             });
110     }
111 
112     public Object makeAnotherArray(Object a, Object b) {
113         return new Object[] { a, b };
114     }
115 
116     public void tryFinally() {
117         try {
118             System.out.println("Try");
119         }
120         finally {
121             System.out.println("Finally");
122         }
123     }
124 
125     public void tryCatchFinally() {
126         try {
127             System.out.println("Try");
128         }
129         catch (Exception e) {
130             System.out.println("Catch");
131         }
132         finally {
133             System.out.println("Finally");
134         }
135     }
136 
137     public void emptyTryCatch() {
138         try {
139         }
140         catch (Throwable t) {
141         }
142     }
143     
144     public void usePrimitiveType() {
145         System.out.println(int.class);
146     }
147     
148     public String testSuperCall() {
149         return super.toString();
150     }
151     
152     public Object createReference(Object foo) {
153         return new Reference(foo);
154     }
155 }