View Javadoc
1   package net.sourceforge.jenesis4java.example;
2   
3   /*
4    * #%L
5    * Jenesis 4 Java Code Generator
6    * %%
7    * Copyright (C) 2000 - 2015 jenesis4java
8    * %%
9    * This program is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU Lesser General Public License as
11   * published by the Free Software Foundation, either version 3 of the
12   * License, or (at your option) any later version.
13   * 
14   * This program is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Lesser Public License for more details.
18   * 
19   * You should have received a copy of the GNU General Lesser Public
20   * License along with this program.  If not, see
21   * <http://www.gnu.org/licenses/lgpl-3.0.html>.
22   * #L%
23   */
24  
25  /**
26   * Copyright (C) 2008 Richard van Nieuwenhoven - ritchie [at] gmx [dot] at
27   * Copyright (C) 2000, 2001 Paul Cody Johnston - pcj@inxar.org This program is
28   * free software; you can redistribute it and/or modify it under the terms of
29   * the GNU Lesser General Public License as published by the Free Software
30   * Foundation; either version 2 of the License, or (at your option) any later
31   * version. This program is distributed in the hope that it will be useful, but
32   * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
33   * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
34   * for more details. You should have received a copy of the GNU Lesser General
35   * Public License along with this program; if not, write to the Free Software
36   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
37   */
38  import net.sourceforge.jenesis4java.Access;
39  import net.sourceforge.jenesis4java.Binary;
40  import net.sourceforge.jenesis4java.ClassMethod;
41  import net.sourceforge.jenesis4java.Comment;
42  import net.sourceforge.jenesis4java.CompilationUnit;
43  import net.sourceforge.jenesis4java.Invoke;
44  import net.sourceforge.jenesis4java.Let;
45  import net.sourceforge.jenesis4java.PackageClass;
46  import net.sourceforge.jenesis4java.Type;
47  import net.sourceforge.jenesis4java.VirtualMachine;
48  
49  /**
50   * Example which generates a class which prints "Hello World!" to standard
51   * output.
52   */
53  class HelloWorldAndi {
54  
55      public static void main(String[] argv) throws Exception {
56          // Get the VirtualMachine implementation.
57          VirtualMachine vm = VirtualMachine.getVirtualMachine();
58  
59          // Instantiate a new CompilationUnit. The argument to the
60          // compilation unit is the "codebase" or directory where the
61          // compilation unit should be written.
62          //
63          // Make a new compilation unit rooted to the given sourcepath.
64          CompilationUnit unit = vm.newCompilationUnit("/tmp");
65  
66          // Set the package namespace.
67          unit.setNamespace("com.example.jenesis");
68  
69          // Add an import statement for fun.
70          unit.addImport("java.io.Serializable");
71  
72          // Comment the package with a javadoc (DocumentationComment).
73          unit.setComment(Comment.DOCUMENTATION, "Auto-Generated using the Jenesis Syntax API");
74  
75          // Make a new class.
76          PackageClass cls = unit.newClass("HelloWorldAndi");
77          // Make it a public class.
78          cls.setAccess(Access.PUBLIC);
79          // Extend Object just for fun.
80          cls.setExtends("Object");
81          // Implement serializable just for fun.
82          cls.addImplements("Serializable");
83          // Comment the class with a javadoc (DocumentationComment).
84          unit.setComment(Comment.DOCUMENTATION, "The HelloWorld example class :)");
85  
86          // Make a new Method in the Class having type VOID and name "main".
87          ClassMethod method = cls.newMethod(vm.newType(Type.VOID), "main");
88          // Make it a public method.
89          method.setAccess(Access.PUBLIC);
90          // Make it a static method
91          method.isStatic(true);
92          // Add the "String[] argv" formal parameter.
93          method.addParameter(vm.newArray("String", 1), "argv");
94  
95          // Create a new Method Invocation expression.
96          Invoke println = vm.newInvoke("System.out", "println");
97          // Add the Hello World string literal as the sole argument.
98          println.addArg(vm.newString("Hello World!"));
99          // Add this expression to the method in a statement.
100         method.newStmt(println);
101 
102         ClassMethod method2 = cls.newMethod(vm.newType(Type.INT), "test");
103 
104         method2.setAccess(Access.PRIVATE);
105         ClassMethod method3 = cls.newMethod(vm.newType("String"), "set" + capitalize("name"));
106         method3.setAccess(Access.PUBLIC);
107         method3.addParameter(vm.newType("int"), "input");
108         Let letx = method3.newLet(vm.newType(Type.LONG));
109         letx.addAssign("x", vm.newBinary(Binary.ADD, vm.newVar("input"), vm.newLong(12L)));
110         method3.newReturn().setExpression(vm.newVar("x"));
111         // Encode the file, compile it, and load the class
112         System.out.println(cls);
113 
114         // java.lang.Class hello = cls.load();
115 
116         // Get the main method
117         // java.lang.reflect.Method main = hello.getMethod("main", new Class[] {
118         // String[].class });
119 
120         // Invoke it
121         // main.invoke(hello.newInstance(), new Object[] { new String[] {} });
122     }
123 
124     private static String capitalize(String s) {
125         char[] chars = s.toCharArray();
126         chars[0] = Character.toUpperCase(chars[0]);
127         return new String(chars);
128     }
129 }