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  import net.sourceforge.jenesis4java.Access;
26  import net.sourceforge.jenesis4java.ClassField;
27  import net.sourceforge.jenesis4java.ClassMethod;
28  import net.sourceforge.jenesis4java.CompilationUnit;
29  import net.sourceforge.jenesis4java.PackageClass;
30  import net.sourceforge.jenesis4java.Type;
31  import net.sourceforge.jenesis4java.Variable;
32  import net.sourceforge.jenesis4java.VirtualMachine;
33  
34  /**
35   * The {@code SimpleGenerator} class creates {@code getXXX()}, {@code setXXX()},
36   * and member fields using the {@code addMember()} method.
37   */
38  class SimpleGenerator {
39  
40      private final VirtualMachine vm;
41  
42      private CompilationUnit unit;
43  
44      private PackageClass cls;
45  
46      /**
47       * Create a new {@code SimpleGenerator}.
48       */
49      private SimpleGenerator() {
50          this.vm = VirtualMachine.getVirtualMachine();
51      }
52  
53      public static void main(String[] argv) throws Exception {
54          VirtualMachine vm = VirtualMachine.getVirtualMachine();
55          SimpleGenerator gen = new SimpleGenerator();
56          gen.initialize("/tmp", "com.mycom.myproject", "Test");
57          gen.addMember(vm.newType("String"), "name");
58          gen.addMember(vm.newArray(Type.INT, 1), "array1");
59          gen.addMember(vm.newArray(Type.INT, 2), "array2");
60          gen.generate();
61      }
62  
63      /**
64       * Intermediate steps: Add a member to the class such that a member field,
65       * getter method, and setter method are defined for the member having the
66       * given name and type.
67       */
68      private void addMember(Type type, String name) {
69          // This "Variable" is defined for later use in statements.
70          Variable thisvar = this.vm.newVar("this." + name);
71          Variable var = this.vm.newVar(name);
72  
73          // PART 1: Make the member field.
74          ClassField field = this.cls.newField(type, name);
75          field.setAccess(Access.PRIVATE);
76  
77          // PART 2: Make the getXXX() method.
78          ClassMethod m1 = this.cls.newMethod(type, "get" + capitalize(name));
79          m1.setAccess(Access.PUBLIC);
80          m1.newReturn().setExpression(thisvar);
81  
82          // PART 3: Make the setXXX(type) method.
83          ClassMethod m2 = this.cls.newMethod(type, "set" + capitalize(name));
84          m2.setAccess(Access.PUBLIC);
85          m2.addParameter(type, name);
86          m2.newStmt(this.vm.newAssign(thisvar, var));
87      }
88  
89      /**
90       * Final step: Generate the class. This calls
91       * {@code CompilationUnit.encode()}
92       */
93      private void generate() {
94          this.unit.encode();
95      }
96  
97      /**
98       * Step 1: Internally setup the {@code CompilationUnit} and
99       * {@code PackageClass} to be generated using the given {@code codebase},
100      * {@code packageName}, and {@code className}.
101      * 
102      * @param codebase
103      *            - where the generated class should be written (for example
104      *            /tmp).
105      * @param packageName
106      *            - the name of the package for the generated class (for example
107      *            com.example.util).
108      * @param className
109      *            - the name of the generated class.
110      */
111     private void initialize(String codebase, String packageName, String className) {
112         this.unit = this.vm.newCompilationUnit(codebase);
113         this.unit.setNamespace(packageName);
114 
115         this.cls = this.unit.newClass(className);
116         this.cls.setAccess(Access.PUBLIC);
117     }
118 
119     private String capitalize(String s) {
120         char[] chars = s.toCharArray();
121         chars[0] = Character.toUpperCase(chars[0]);
122         return new String(chars);
123     }
124 }