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 7826953
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T09:38:18+00:00 2026-06-02T09:38:18+00:00

I’m trying to write a small internal DSL in Java which generates an tree

  • 0

I’m trying to write a small internal DSL in Java which generates an tree of objects. The DSL code looks like this:

RuleBuilder builder = new RuleBuilder(new Syntax());
Syntax s =
builder.rule("rule1")
            .identifier("foo")
            .choice()
                .terminal("bar")
            .end() // 1) Here it works.
       .end() // 2) Here complains the compiler.
       .rule("rule2")
            .identifier("bar")
       .end()
       .build();

The compiler complains (at 2) that the object java.lang.Object returned by end() does not have a method rule(). It’s obvious to me that Object does not have this method. The builder code is as follows (The code assembling the tree is left for simplicity):

class RuleBuilder {
    private final Syntax syntax;

    public RuleBuilder(Syntax syntax) {
        this.syntax = syntax;
    }

    public GenericBuilder<RuleBuilder> rule(String name) {
        return new GenericBuilder<RuleBuilder>(this);
    }

    public Syntax build() { return syntax; }
}

class GenericBuilder<P> {
    private final P parentBuilder;

    public GenericBuilder(P parentBuilder) {
        this.parentBuilder = parentBuilder;
    }

    public <P> P end() {
        return (P)parentBuilder;
    }

    public GenericBuilder<P> identifier(String value) { return this; }
    public GenericBuilder<P> terminal(String value) { return this; }
    public GenericBuilder<GenericBuilder> choice() { return new GenericBuilder<GenericBuilder>(this); }
    // ... other sub node types
}           

The principal idea behind my implementation: The generated syntax tree consists of a Syntax root node which has some Rule nodes. This rule nodes can have some sub nodes. The sub nodes are of two types: Leafs and nodes (in the example code only choice for simplicity). I will provide a fluent interface where I can add nodes and leaves to the tree. To ‘end’ a tree branch there is the end() method which returns the parent builder.

The issue I try to solve is, that the method end() should return the parent builder object which may be of type RuleBuilder or GenericBuilder. I do not understand why it works at 1) and not at 2) in the example above.

I’ve read through a lot of resources about generics and I understand how List<T> and Map<K,V> and such works. And I’m aware of the “Erasure” thing, that the type information is lost at runtime. So I can understand that at runtime end() returns java.lang.Object, when type information is erased. But I got a compile time error. I’ve also read through the blog of Neal Gafter about Super Type Tokens and Typesafe Heterogenous Containers. But I’m not sure if this solves my problem. I tried several different approaches (during my search in the web), but now I’m stuck.

  • 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-02T09:38:20+00:00Added an answer on June 2, 2026 at 9:38 am

    There are two problems. First there’s this:

    public GenericBuilder<GenericBuilder> choice() { 
        return new GenericBuilder<GenericBuilder>(this);
    }
    

    That’s not saying what kind of GenericBuilder the “inner” one is. You should be able to change it to:

    public GenericBuilder<GenericBuilder<P>> choice() { 
        return new GenericBuilder<GenericBuilder<P>>(this);
    }
    

    At that point, the inner end() call will still know it’s got a GenericBuilder<RuleBuilder> instead of a raw GenericBuilder.

    The second problem is here:

    public <P> P end() {
        return (P)parentBuilder;
    }
    

    That’s a generic method when it shouldn’t be – you don’t want to introduce a new type parameter P here; you just want the existing one:

    public P end() {
        return parentBuilder;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I would like to run a str_replace or preg_replace which looks for certain words
I am trying to understand how to use SyndicationItem to display feed which is
For some reason, after submitting a string like this Jack’s Spindle from a text
I used javascript for loading a picture on my website depending on which small
I am trying to render a haml file in a javascript response like so:
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have some data like this: 1 2 3 4 5 9 2 6

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.