Are you fed up with launching your heavy-weighted java eclipse IDE when you just need to create a quick java class to do some testing (by the way if someone knows an equivalent of Snippet Compiler but for Java that would interest me) ? Don’t you miss your simple notepad just because it would need a little twist ? Or maybe you’re just learning Java with an amazing editor like BlueJ which still lacks code generation. Using this lighweight text DSL or Domain Specific Language is a solution which will hugely improve your productivity (especially if you wrap it inside a snippet protocol).
We saw last time how it was incredibly simple to create a C# code generator, it’s now even easier to write the Java code generator by duplicating the C# code and customizing it for java (install and launch rebol’s console then copy and paste the code below):
generate-code: func[][
attributes: parse/all attributes-list ","
switch Language [
"C#" [generate-code-csharp attributes]
"java" [generate-code-java attributes]
] /default [print ["Language" Language "not yet implemented"]]
]
generate-code-java: func[attributes][
package: if/else ((answer: ask "package (ex.:MyApp): ") = "") ["MyApp"][answer]
class-name: if/else ((answer: ask "class-name (ex.: Cperson): ") = "") ["CPerson"][answer]
private-prefix: "_"
code-template: {// Generated by http://www.reboltutorial.com
package <%package%>;
public class <%class-name%>
{
// private members
<%private-members%>
// empty constructor
public <%class-name%> ()
{
}
// full constructor
public <%class-name%> (<%private-constructor-arguments-list%>)
{
<%private-constructor-body%>
}
// public accessors
<%public-accessors%>
}
}; end of Java class template
private-members-template: {<%attribute-type%> <%private-prefix%><%attribute-name%>;}
private-constructor-arguments-list-template: {<%attribute-type%> <%attribute-name%>}
private-constructor-body-template: {<%private-prefix%><%attribute-name%> = <%attribute-name%>;}
public-accessors-template: {//Accessors for <%attribute-name%>
public <%attribute-type%> get<%attribute-name%>() {
return <%private-prefix%><%attribute-name%>;
}
public void set<%attribute-name%>(<%attribute-type%> <%attribute-name%>) {
<%private-prefix%><%attribute-name%>=<%attribute-name%>;
}
}
private-members: ""
private-constructor-arguments-list: ""
private-constructor-body: ""
public-accessors: ""
n: length? attributes
counter: 0
foreach attribute attributes [
counter: counter + 1
attribute-name: trim/all attribute; remove all whitespaces
attribute-type: if/else ((answer: ask ["attribute-type for" attribute "(ex. String) : " ]) = "") ["String"][answer]
if (counter > 1) [
append private-members " "
append private-constructor-body " "
append public-accessors " "
]
append private-members build-markup private-members-template
append private-constructor-arguments-list build-markup private-constructor-arguments-list-template
append private-constructor-body build-markup private-constructor-body-template
append public-accessors build-markup public-accessors-template
if (counter < n) [
append private-members newline
append private-constructor-arguments-list ", "
append private-constructor-body newline
]
]
source-code: build-markup code-template
write clipboard:// source-code
Print "source code copied into clipboard ..."
input
]
Now test the code by typing (or pasting) in rebol’s console:
phrase: "Create a Java class with Id, First Name, Last Name, Birth Date"
rule: ["Create a " copy Language to " class" thru " with " copy attributes-list to end (
generate-code)]
parse phrase rule

This is the code generated:
// Generated by http://www.reboltutorial.com
package MyApp;
public class CPerson
{
// private members
String _Id;
String _FirstName;
String _LastName;
String _BirthDate;
// empty constructor
public CPerson ()
{
}
// full constructor
public CPerson (String Id, String FirstName, String LastName, String BirthDate)
{
_Id = Id;
_FirstName = FirstName;
_LastName = LastName;
_BirthDate = BirthDate;
}
// public accessors
//Accessors for Id
public String getId() {
return _Id;
}
public void setId(String Id) {
_Id=Id;
}
//Accessors for FirstName
public String getFirstName() {
return _FirstName;
}
public void setFirstName(String FirstName) {
_FirstName=FirstName;
}
//Accessors for LastName
public String getLastName() {
return _LastName;
}
public void setLastName(String LastName) {
_LastName=LastName;
}
//Accessors for BirthDate
public String getBirthDate() {
return _BirthDate;
}
public void setBirthDate(String BirthDate) {
_BirthDate=BirthDate;
}
}
You can test it for example with BlueJ:

You can execute the code over the internet by typing:
do http://rebcode.com/dsl















Cool …that was really quick…
Just a small feedback
Your blog font size is too small to read…I have a big screen monitor and I still hv problem reading … u may want to reconsider it..
Why not reconsider your big screen monitor … joke
You’re right, I will try to change something in the font size next week-end.