Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8652481
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T14:19:01+00:00 2026-06-12T14:19:01+00:00

I’m trying to do the following: Write a JET template that receives an object

  • 0

I’m trying to do the following:

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:

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; }  
}  

Hint: the unqualified (simple) name of a type
can be acquired by using the getSimpleName() method on the
corresponding Class object.

I have read the tutorials about JET found on eclipse.org but I am still having trouble understanding what I need to do.

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 generate method? Is that the right concept?

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:

<%@ jet imports="java.lang.reflect.*" class="Q2Generator" %>
<%Object o = (Object) argument;%>
<%Class c = o.getClass();%>

public class <%=c.getName()%>Implementation implements <%=c.getName()%> {
<%for (Method m : c.getDeclaredMethods()) {%>
<%  Class type = m.getReturnType();%>
<%  if (!type.isPrimitive()) {%>
public <%=type.getSimpleName()%> <%=m.getName()%> { return null; } // this line is the problem
<%  }%>
<%}%>       
}

This template results in the following code in the generate method:

  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();
  }

Is there a way to indent the stringBuffer.append() lines in the if statement? And is the String way the right way of going about this task?

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T14:19:02+00:00Added an answer on June 12, 2026 at 2:19 pm

    I’d use the newer JET2 syntax. You should be able to create a new JET transform with New -> Other.

    As for what’s happening at a high level, you’ll have a template called main.jet which will act as the controller. It doesn’t create any text content itself but it will manage the generation of projects, folders and files.

    I understand that you want to use a class object as input, but I recommend you build out your templates to use an XML file as input. Something like this:

    <root>
        <object name="A">
            <method name="m1" returns="void">
                <arg name="x" type="int" />
                <arg name="y" type="int" />
            </method>
            <method name="m2" returns="int">
                <arg name="a" type="Object" />
            </method>
            <method name="m3" returns="Object">
            </method>
        </object>
    </root>
    

    You can see that given a class we can “easily” create such an XML document.

    So the main.jet would look something like this:

    <%@taglib prefix="ws" id="org.eclipse.jet.workspaceTags" %>
    
    <c:iterate select="/root/object" var="object"
    
        <c:set select="$object" name="impl" ><c:get select="$object/@name"/>Implementation</c:set>
        <c:set select="$object" name="interface" ><c:get select="$object/@name"/></c:set>
    
    </c:iterate>
    
    
    <c:iterate select="/root/object" var="object">
    
        <ws:file path="my project/src/a/b/c/{$object/@interface}.java" template="interface.jet" />
        <ws:file path="my project/src/a/b/c/{$object/@impl}.java" template="impl.jet" />
    
    </c:iterate>
    

    Basically you’re iterating over each object (you can define as many as you want) and you’re building the implementation and interface names and storing those names back into the model.
    Once you’ve done all the naming conventions, you iterate over the object elements and apply templates to the model using the ws:file tag. The tag tells which template to use and then specifies the file name to create with the generation results.

    the interface.jet file might look something like this:

    package a.b.c;
    
    interface <c:get select="$object/@interface"/> { 
    <c:iterate select="$object/method" var="method" > 
        <c:get select="$method/@returns"/> <c:get select="$method/@name"/>(int x, int y);  
    </c:iterate>
    }  
    

    Note that I’ve hard-coded the package to be a.b.c. You can make that variable by adding an attribute to the XML file, probably to the object element and using the c:get tag to insert it into the source. I’ve also left the args hard-coded, but you can use another iterate tag to iterate over the nested elements in the model to write out the method signature.

    So I’ll stop there to see if this is what you were looking for. You may want to ask more in comments or post more questions.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
Basically, what I'm trying to create is a page of div tags, each has
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a small JavaScript validation script that validates inputs based on Regex. I
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.