0

I'm looking to develop a tool which will take simplified code in the format:

MyObject
{
    [Get, Construct]
    String name;

    [Get, Construct]
    String description;

    [Get, Set]
    Boolean isAlive;
}

And will spit out java/C# code like:

public class MyObject {

    public MyObject(
            String name,
            String description) {

        this.name = name;
        this.description = description;
    }

    public String getName() {

        return this.name;
    }

    public String getDescription() {

        return this.description;
    }

    public Boolean getIsAlive() {

        return this.isAlive;
    }

    public Boolean setIsAlive(
            Boolean isAlive) {

        this.isAlive = isAlive;
    }


    private String name;
    private String description;
    private Boolean isAlive;
}

The purpose of this is to basically make my life easier, as a lot of what I've been doing lately it making objects which are simply reflections of things in the logical model of our project.

I already know how I'm going to write the output, but I'm just wondering if;

A) Are there any tools around that already do this kind of thing? B) Are there any libraries around (either in C# or java), which would be useful in parsing the input language? I know it's a custom language, but it shares a lot of similarities with C#. I'm wondering if there is anything that may save me some time?

4
  • Is your "logical model" in your head, or is there some sort of computerised representation of it? Commented Jul 5, 2016 at 15:56
  • In the Java world we would use jaxb, where the "custom language" is a XML schema (xsd file). Xjc is the tools that does the code generation and it has a plugin architecture, so you can write your own code generation if you want (or just augment the default code that is generated). There is no reason why you could not write a plugin that would produce C# code in addition to Java code. Commented Jul 5, 2016 at 15:59
  • The logical model is essentially in our heads, or in non machine actionable documentation. Commented Jul 5, 2016 at 16:01
  • Eclipse will do the accessors for you. Commented Jul 5, 2016 at 16:04

2 Answers 2

1

I did a lot of good experience with Xtext which is an official eclipse project. it comes with many useful features out of the box, has a quite active community, is easily extensible/customizeable and documented well. Xtext generates custom DSL environments based on a grammar definition and supports targeting eclipse itself, intellJ and web environments - more information: https://eclipse.org/Xtext/

Further there is https://www.jetbrains.com/mps/ which comes with a similar solution for the same problem. a highlight might be the projectional editing.

Both tools working on another level of abstraction than e.g. using ANTLR and providing many solutions including creating custom generators targeting your individual target language to generate e.g. code for - once you got into and it fits into your requirements it will make your life a bit easier.

Sign up to request clarification or add additional context in comments.

1 Comment

Xtext looks interesting; I wish I'd known about it sooner ;)
0

You're basically defining your own domain-specific language. If you want to go down that route, you could use something like ANTLR -- you define how your language works, feed the definition into ANTLR, and it creates a Java program that can read your simple spec and turn it into full Java source. This is probably more work than you want to do, though.

There are already tools that turn a class specification into Java code. Often you have to represent your class design using UML, using a graphical editor or text-based description a bit like the one you proposed (look at eg this question for examples). Other tools use other "languages" to represent your class, but I'm not really familiar with those.

I should point out that representing your design with UML or some other system can be almost as slow as typing the Java code directly. It's only really useful if you change things a lot, or take advantage of other things that come along with a machine-readable representation of your class -- eg generating diagrams, automated checking of design rules, etc.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.