Reputation: 3522
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?
Upvotes: 0
Views: 1072
Reputation: 51
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.
Upvotes: 1
Reputation: 503
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.
Upvotes: 0