title: Using JET to generate code: Indenting code
body: <p>I'm trying to do the following:</p>
  
  <blockquote>
    <p>Write a JET template that receives an object of type Class as
    argument. The object should represent a Java interface. The template
    generates a class that implements the interface, i.e. provides methods
    for all the method signatures it defines. The name of the generated
    class should be XImplementation where X is the name of the argument
    interface. The methods in the generated class do nothing or only
    return constant values: 0 for int and double, false for boolean, and
    null for reference types. You do not need to consider any other return
    types. E.g. for the following interface A, class AImplementation would
    be generated:</p>
  
  <pre><code>interface A {  
      void m1(int x, int y);  
      int m2(Object a);  
      Object m3();  
  }  
  
  class AImplementation implements A {  
      public void m1(int p1, int p2) { }  
      public int m2(Object p1) { return 0; }  
      public Object m3() { return null; }  
  }  
  </code></pre>
    
    <p>Hint: the unqualified (simple) name of a type
    can be acquired by using the getSimpleName() method on the
    corresponding Class object.</p>
  </blockquote>
  
  <p>I have read the tutorials about JET found on eclipse.org but I am still having trouble understanding what I need to do.</p>
  
  <p>When I make the .txtjet file to be translated, am I trying to make it so the implementation of the .txtjet file writes a huge String with the code that I want to generate in the <code>generate</code> method? Is that the right concept?</p>
  
  <p>If that is the case I'm having trouble with one particular aspect. This is the JET template that I have come up with so far:</p>
  
  <pre><code>&lt;%@ jet imports="java.lang.reflect.*" class="Q2Generator" %&gt;
  &lt;%Object o = (Object) argument;%&gt;
  &lt;%Class c = o.getClass();%&gt;
  
  public class &lt;%=c.getName()%&gt;Implementation implements &lt;%=c.getName()%&gt; {
  &lt;%for (Method m : c.getDeclaredMethods()) {%&gt;
  &lt;%  Class type = m.getReturnType();%&gt;
  &lt;%  if (!type.isPrimitive()) {%&gt;
  public &lt;%=type.getSimpleName()%&gt; &lt;%=m.getName()%&gt; { return null; } // this line is the problem
  &lt;%  }%&gt;
  &lt;%}%&gt;       
  }
  </code></pre>
  
  <p>This template results in the following code in the <code>generate</code> method:</p>
  
  <pre><code>  public final String NL = nl == null ? (System.getProperties().getProperty("line.separator")) : nl;
    protected final String TEXT_1 = NL + "public class ";
    protected final String TEXT_2 = "Implementation implements ";
    protected final String TEXT_3 = " {";
    protected final String TEXT_4 = NL + "public ";
    protected final String TEXT_5 = " ";
    protected final String TEXT_6 = " { return null; }";
    protected final String TEXT_7 = NL + "\t\t" + NL + "}";
    protected final String TEXT_8 = NL;
  
    public String generate(Object argument)
    {
      final StringBuffer stringBuffer = new StringBuffer();
      Object o = (Object) argument;
      Class c = o.getClass();
      stringBuffer.append(TEXT_1);
      stringBuffer.append(c.getName());
      stringBuffer.append(TEXT_2);
      stringBuffer.append(c.getName());
      stringBuffer.append(TEXT_3);
      for (Method m : c.getDeclaredMethods()) {
          Class type = m.getReturnType();
          if (!type.isPrimitive()) {
      stringBuffer.append(TEXT_4);
      stringBuffer.append(type.getSimpleName());
      stringBuffer.append(TEXT_5);
      stringBuffer.append(m.getName());
      stringBuffer.append(TEXT_6);
          }
      }
      stringBuffer.append(TEXT_7);
      stringBuffer.append(TEXT_8);
      return stringBuffer.toString();
    }
  </code></pre>
  
  <p>Is there a way to indent the <code>stringBuffer.append()</code> lines in the if statement? And is the String way the right way of going about this task?</p>
  
  <p>Thanks.</p>