01: /**
02: * InstantJ
03: *
04: * Copyright (C) 2002 Nils Meier
05: *
06: * This library is free software; you can redistribute it and/or
07: * modify it under the terms of the GNU Lesser General Public
08: * License as published by the Free Software Foundation; either
09: * version 2.1 of the License, or (at your option) any later version.
10: *
11: * This library is distributed in the hope that it will be useful,
12: * but WITHOUT ANY WARRANTY; without even the implied warranty of
13: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14: * Lesser General Public License for more details.
15: *
16: */package instantj.tst;
17:
18: import instantj.compile.CompilationFailedException;
19: import instantj.compile.Source;
20: import instantj.compile.Compiler;
21: import instantj.reflect.ReflectAccess;
22:
23: /**
24: * A simple testcase that compiles a dynamic Java type
25: */
26: public class SimpleCompileTest {
27:
28: /** the source */
29: private final static String SOURCE = " package bar; \n"
30: + " public class Foo { \n"
31: + " public String toString() { \n"
32: + " return \"Foo Rulez!\"; \n"
33: + " } \n"
34: + " } \n";
35:
36: /**
37: * MAIN
38: */
39: public static void main(String[] args) {
40:
41: // Disclaimer
42: System.out.println("InstantJ - "
43: + ReflectAccess.getInstance().calcClassNameOf(
44: SimpleCompileTest.class));
45:
46: // Debug
47: System.out.println("Compiling :\n\n" + SOURCE);
48:
49: // Compile the source & get the type
50: Class type;
51: try {
52: type = Compiler.compile(new Source(SOURCE), true).getType();
53: } catch (CompilationFailedException e) {
54: System.out.println("Compilation failed because of "
55: + e.getMessage());
56: e.printErrors(System.out);
57: return;
58: }
59:
60: // Test the type
61: try {
62: System.out.println(" .toString() returns "
63: + type.newInstance());
64: } catch (Throwable t) {
65: System.out.println("Couldn't instantiate type");
66: }
67:
68: // Done
69: }
70: }
|