Created
October 3, 2012 16:38
-
-
Save devnoo/3828156 to your computer and use it in GitHub Desktop.
Simple builder generator using qdox and groovy templates
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.thoughtworks.qdox.JavaDocBuilder | |
import com.thoughtworks.qdox.model.JavaClass | |
import com.thoughtworks.qdox.model.Type | |
import com.thoughtworks.qdox.model.JavaField | |
import com.thoughtworks.qdox.model.JavaMethod | |
import com.thoughtworks.qdox.model.JavaSource | |
import groovy.text.SimpleTemplateEngine | |
import org.apache.commons.io.FileUtils | |
JavaDocBuilder builder = new JavaDocBuilder(); | |
builder.addSourceTree(new File("src/main/java/nl/dnb/dgs/commons/backend/entities")); | |
builder.getSources().each { JavaSource js -> | |
js.getClasses().each { JavaClass jc -> | |
if (jc.isInterface() || jc.isEnum()){ | |
return | |
} | |
List<JavaField> fields = jc.getFields().findAll { !(it.isStatic() && it.isFinal()) } | |
def engine = new SimpleTemplateEngine() | |
def bindings = [imports : Arrays.asList(js.getImports()), pkg: jc.getPackage(), name: jc.getName(), fields : fields] | |
def template = engine.createTemplate(new File("src/test/groovy/builder_template.txt")).make(bindings) | |
def packageFolder = js.getPackage().replace(".","/"); | |
File packageFile = new File("src/test/groovy/" + packageFolder) | |
if (!packageFile.exists()){ | |
FileUtils.forceMkdir(packageFile) | |
} | |
def file = new File("${jc.getName()}FixtureBuilder.groovy", packageFile) | |
file.write(template.toString()) | |
} | |
} | |
package ${pkg} | |
<% imports.each { imp -> | |
println "import ${imp}" | |
} | |
%> | |
import nl.dnb.dgs.test.DgsFixtureBuilder | |
class ${name}FixtureBuilder extends DgsFixtureBuilder { | |
<% fields.each { field -> | |
println "\t${field.type.name} ${field.name}" | |
} | |
%> | |
<% fields.each { field -> | |
println "\t${name}FixtureBuilder with${field.name}(${field.type.name} ${field.name}){" | |
println "\t\tthis.${field.name} = ${field.name}" | |
println "\t\tthis" | |
println "\t}\n" | |
} | |
%> | |
\tstatic ${name}FixtureBuilder a${name}(){ | |
\t\tnew ${name}FixtureBuilder() | |
\t} | |
\t${name} build(){ | |
\t\tdef objectToBuild = new ${name}() | |
<% fields.each { field -> | |
println "\t\tobjectToBuild.${field.name} = ${field.name}" | |
}%>\t\treturn objectToBuild | |
\t} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment