7
\$\begingroup\$

I use Groovy to write business rules into big, long term Java software. Use Groovy in object way, that is not as groovy Script, but every elementary groovy source overrides some Java API class.

i.e. AtLoginScript, PreparePaport etc.

I had an idea to clone in very little scale IDE functionality: generate class from, or Override method.

My idea is much simpler: do automatically, only first-shot, write method stub where this must be done (abstract method), maybe in comment where may be done (non-abstract).

My mini project has first effects, in this sample GroovyGenerator create his-own 'child class'.

My target for next days is to:

  • implement negative conditions for final or private method,
  • some decisions with more deep parent class (now only one-level)
  • types when applicable
  • maybe implement groovy property from getter/setter pair?
  • constructors
  • extend from 0 or 1 class and implement zero to \$n\$ interfaces (What to do with override level?)

I haven't big trouble with coding (one small problem: reflection usually doesn't give parameters name), but with idea.

This tool is made for in-place IT-man, who generate business rules in, for example, Notepad or simple editor non-capable in generation.

What can be done better in this project?

package pl.cogitat.scripting;

import java.lang.reflect.Method;
import java.lang.reflect.Parameter;

public class GroovyGenerator {  
    Class analyzedClass;
    private String packg;
    private String name;

    public GroovyGenerator(String pckg, String name, Class analyzedClass) {
        super();
        this.analyzedClass = analyzedClass;
        this.packg = pckg;
        this.name = name;
    }


    public String getName() {
        return name;
    }


    public void setName(String name) {
        this.name = name;
    }


    public String getPackg() {
        return packg;
    }


    public String performGeneration()
    {
        StringBuilder sb = new StringBuilder();
        sb.append("package ").append(packg).append("\n\n /* Code auto generated\n * do edit what You want \n */\n\n");

        String parentName = analyzedClass.getSimpleName();
        sb.append("class ").append(name).append(" extends ").append(parentName).append(" {\n");

        for(Method m : analyzedClass.getDeclaredMethods())
        {
            sb.append("def ").append(m.getName()).append("(");
            int pcnt = 0;
            for(Parameter p:m.getParameters())
            {
                if(pcnt>0)
                    sb.append(", ");
                sb.append(p.getType().getSimpleName()).append(" ").append(p.getName());
                pcnt++;
            }
            sb.append(")\n{\n}\n");
        }

        sb.append("\n}\n");
        return sb.toString();

    }
}

and untouched generated Groovy:

package my.script

 /* Code auto generated
 * do edit what You want 
 */

class SelfGenerator extends GroovyGenerator {
def getName()
{
}
def setName(String arg0)
{
}
def performGeneration()
{
}
def getPackg()
{
}

}
\$\endgroup\$
  • \$\begingroup\$ Technical comment No 1: if engine override non-abstract method, call super.method in default implementation (aka generated code is safe). Best with word "return" (if not void) although in Groovy isn't required \$\endgroup\$ – Jacek Cz Sep 3 '15 at 13:21
  • \$\begingroup\$ No 2. In Java 8 (when set compiler options) names of argument may be known. \$\endgroup\$ – Jacek Cz Sep 3 '15 at 13:21
  • \$\begingroup\$ No. 3, bad news. Javadoc is absent in bytecode (.class file). Probably use annotations for documenting \$\endgroup\$ – Jacek Cz Sep 3 '15 at 13:23
2
\$\begingroup\$

You can use Groovy to generate the Groovy source code.

The source code generator

Here's an example source code generator.

import groovy.text.SimpleTemplateEngine

def generate(Class klass, String packageName, String name, String template) {
    def engine = new SimpleTemplateEngine()
    def binding = [
        packageName: packageName,
        name: name,
        superClass: klass.name,
        methods: klass.metaClass.methods
            .findAll { !(it.toString().contains(' final ')) }
            .collect {    
                def args = []        
                def m = it.toString() =~ /(.*) (.*\.)(.*)(\((.*)\).*)/

                m.matches()                
                if(m.group(5).size() > 0) {
                    m.group(5).split(',').eachWithIndex {arg, index ->
                        args << "$arg p$index"
                    }
                }

                return "${m.group(1).replace('native', '')} ${m.group(3)}(${args.join(', ')})"
            }
    ]

    engine.createTemplate(template).make(binding).toString()
}

How it works

It's actually quite simple. The method signatures are gathered from the class's MetaClass. Then some hefty formatting is applied to write the method signatures correctly. Also, final methods are excluded.

Example

def template = '''
package $packageName

/* Code auto generated
 * do edit what You want 
 */

class $name extends $superClass {
<% 
methods.each {
    println ''
    println "\t$it {"
    println ''
    println "\t}"
} %>
}
'''

generate(Object, 'my.script', 'SelfGenerator', template)

Generated source code

package my.script

/* Code auto generated
 * do edit what You want 
 */

class SelfGenerator extends java.lang.Object {

    public boolean equals(java.lang.Object p0) {

    }

    public  int hashCode() {

    }

    public java.lang.String toString() {

    }

}

Templates

Ideally, you'd have two templates: one for the class and one for the methods. That would avoid the clunky JSP-style programming. You can read more about Groovy's template engines here.

\$\endgroup\$
  • \$\begingroup\$ Fast thanks to @Emmanuel. Using templates (any) is very good idea and will be realised unconditionally :) For example control over aesthetics of code, comment, doc, is 100x better. I rethink Groovy code too (today my java generator is better than yesterday, many if's, not only at "final") , BTW is very interesting at "hacker" level. In general You show (indirect) good question: now API classes are 100% Java, but in future, when some will be groovish, then algorithm need work in this way \$\endgroup\$ – Jacek Cz Sep 4 '15 at 8:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.