1 package net.sourceforge.jenesis4java.example;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39 import net.sourceforge.jenesis4java.*;
40
41 import java.io.ByteArrayOutputStream;
42 import java.io.File;
43 import java.io.IOException;
44 import java.io.InputStream;
45
46
47
48
49
50 class HelloWorldExample {
51
52 public static void main(String[] argv) throws Exception {
53 long start = System.currentTimeMillis(), time;
54
55 File temp = new File("target/testgenerate");
56 temp.mkdirs();
57
58 if (!temp.isDirectory()) {
59 System.out.println("Error creating temp directories!");
60 return;
61 }
62
63 final String pathToGeneratedFile = temp.getCanonicalPath() + "/com/example/jenesis/HelloWorld.java";
64 File generatedFile = new File(temp, "/com/example/jenesis/HelloWorld.java");
65
66
67 if (generatedFile.exists()) {
68 generatedFile.delete();
69 }
70
71
72 VirtualMachine vm = VirtualMachine.getVirtualMachine();
73
74
75
76
77
78
79 CompilationUnit unit = vm.newCompilationUnit(temp.getCanonicalPath());
80
81
82 unit.setNamespace("com.example.jenesis");
83
84
85 unit.addImport("java.io.Serializable");
86
87
88 unit.setComment(Comment.DOCUMENTATION, "Auto-Generated using the Jenesis Syntax API");
89
90
91 PackageClass cls = unit.newClass("HelloWorld");
92
93 cls.setAccess(Access.PUBLIC);
94
95 cls.setExtends("Object");
96
97 cls.addImplements("Serializable");
98
99 unit.setComment(Comment.DOCUMENTATION, "The HelloWorld example class.");
100
101
102 ClassMethod method = cls.newMethod(vm.newType(Type.VOID), "main");
103
104 method.setAccess(Access.PUBLIC);
105
106 method.isStatic(true);
107
108 method.addParameter(vm.newArray("String", 1), "argv");
109
110
111 Invoke println = vm.newInvoke("System.out", "println");
112
113 println.addArg(vm.newString("Hello World!"));
114
115 method.newStmt(println);
116
117 time = System.currentTimeMillis() - start;
118 System.out.println("constructed in " + time + " ms");
119
120
121 unit.encode();
122
123 Process exec = Runtime.getRuntime().exec(new String[]{
124 "javac",
125 "-d",
126 "target/classes",
127 generatedFile.getCanonicalPath()
128 });
129 exec.waitFor();
130 if (exec.exitValue() != 0) {
131 String streambuffer1 = readStream(exec.getErrorStream());
132 String streambuffer2 = readStream(exec.getInputStream());
133
134 throw new RuntimeException(streambuffer1 + "\n" + streambuffer2);
135 }
136 Class<?> hello = Thread.currentThread().getContextClassLoader().loadClass("com.example.jenesis.HelloWorld");
137
138 time = System.currentTimeMillis() - start;
139 System.out.println("loaded in " + time + " ms");
140
141 java.lang.reflect.Method main = hello.getMethod("main", String[].class);
142
143
144 main.invoke(hello.newInstance(), new Object[]{
145 new String[]{}
146 });
147
148 time = System.currentTimeMillis() - start;
149 System.out.println("done in " + time + " ms");
150 }
151
152 private static String readStream(InputStream error) throws IOException {
153 ByteArrayOutputStream errorBytes = new ByteArrayOutputStream();
154 int count;
155 byte[] buffer = new byte[4096];
156 while ((count = error.read(buffer)) >= 0) {
157 errorBytes.write(buffer, 0, count);
158 }
159 return new String(buffer);
160 }
161 }