Class builder scratch
This commit is contained in:
11
.gitignore
vendored
11
.gitignore
vendored
@@ -1,12 +1,7 @@
|
||||
*.class
|
||||
*.iml
|
||||
|
||||
# Mobile Tools for Java (J2ME)
|
||||
.mtj.tmp/
|
||||
.idea/
|
||||
target/
|
||||
|
||||
# Package Files #
|
||||
*.jar
|
||||
*.war
|
||||
*.ear
|
||||
|
||||
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
|
||||
hs_err_pid*
|
||||
|
||||
54
pom.xml
Normal file
54
pom.xml
Normal file
@@ -0,0 +1,54 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>eu.codearte.contract-keeper</groupId>
|
||||
<artifactId>contract-keeper</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
|
||||
<properties>
|
||||
<groovy.version>2.3.7</groovy.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.codehaus.groovy</groupId>
|
||||
<artifactId>groovy-all</artifactId>
|
||||
<version>${groovy.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.11</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.jayway.restassured</groupId>
|
||||
<artifactId>rest-assured</artifactId>
|
||||
<version>2.4.0</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.codehaus.gmavenplus</groupId>
|
||||
<artifactId>gmavenplus-plugin</artifactId>
|
||||
<version>1.2</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
<goal>compile</goal>
|
||||
<goal>testCompile</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
16
src/main/groovy/eu/coderate/ck/Generator.groovy
Normal file
16
src/main/groovy/eu/coderate/ck/Generator.groovy
Normal file
@@ -0,0 +1,16 @@
|
||||
package eu.coderate.ck
|
||||
|
||||
import static eu.coderate.ck.builder.ClassBuilder.createClass
|
||||
import static eu.coderate.ck.builder.MethodBuilder.createVoidMethod
|
||||
|
||||
class Generator {
|
||||
|
||||
public static void main(String[] args) {
|
||||
print createClass('MyTest', 'io.test')
|
||||
.addImport('org.junit.Test')
|
||||
.addStaticImport('org.hamcrest.CoreMatchers.*')
|
||||
.addStaticImport('com.jayway.restassured.RestAssured.*')
|
||||
.addMethod(createVoidMethod('shouldInvokeService'))
|
||||
.build()
|
||||
}
|
||||
}
|
||||
59
src/main/groovy/eu/coderate/ck/builder/BlockBuilder.groovy
Normal file
59
src/main/groovy/eu/coderate/ck/builder/BlockBuilder.groovy
Normal file
@@ -0,0 +1,59 @@
|
||||
package eu.coderate.ck.builder
|
||||
|
||||
import groovy.transform.PackageScope
|
||||
|
||||
/**
|
||||
* @author Jakub Kubrynski
|
||||
*/
|
||||
@PackageScope
|
||||
class BlockBuilder {
|
||||
|
||||
private final StringBuilder builder
|
||||
private final String spacer
|
||||
private int indents
|
||||
|
||||
BlockBuilder(String spacer) {
|
||||
this.spacer = spacer
|
||||
builder = new StringBuilder()
|
||||
}
|
||||
|
||||
BlockBuilder startBlock() {
|
||||
indents++
|
||||
return this
|
||||
}
|
||||
|
||||
BlockBuilder endBlock() {
|
||||
indents--
|
||||
return this
|
||||
}
|
||||
|
||||
BlockBuilder addLine(String line) {
|
||||
addIndentation()
|
||||
builder << "$line\n"
|
||||
return this
|
||||
}
|
||||
|
||||
BlockBuilder addEmptyLine() {
|
||||
builder << '\n'
|
||||
return this
|
||||
}
|
||||
|
||||
void addIndentation() {
|
||||
indents.times {
|
||||
builder << spacer
|
||||
}
|
||||
}
|
||||
|
||||
BlockBuilder addBlock(MethodBuilder methodBuilder) {
|
||||
startBlock()
|
||||
methodBuilder.appendTo(this)
|
||||
endBlock()
|
||||
addEmptyLine()
|
||||
return this
|
||||
}
|
||||
|
||||
@Override
|
||||
String toString() {
|
||||
return builder.toString()
|
||||
}
|
||||
}
|
||||
61
src/main/groovy/eu/coderate/ck/builder/ClassBuilder.groovy
Normal file
61
src/main/groovy/eu/coderate/ck/builder/ClassBuilder.groovy
Normal file
@@ -0,0 +1,61 @@
|
||||
package eu.coderate.ck.builder
|
||||
|
||||
/**
|
||||
* @author Jakub Kubrynski
|
||||
*/
|
||||
class ClassBuilder {
|
||||
|
||||
private final String className
|
||||
private final String packageName
|
||||
private final List<String> imports = []
|
||||
private final List<String> staticImports = []
|
||||
private final List<MethodBuilder> methods = []
|
||||
|
||||
private ClassBuilder(String className, String packageName) {
|
||||
this.packageName = packageName
|
||||
this.className = className
|
||||
}
|
||||
|
||||
static ClassBuilder createClass(String className, String packageName) {
|
||||
return new ClassBuilder(className, packageName)
|
||||
}
|
||||
|
||||
ClassBuilder addImport(String importToAdd) {
|
||||
imports << importToAdd
|
||||
return this
|
||||
}
|
||||
|
||||
ClassBuilder addStaticImport(String importToAdd) {
|
||||
staticImports << importToAdd
|
||||
return this
|
||||
}
|
||||
|
||||
ClassBuilder addMethod(MethodBuilder methodBuilder) {
|
||||
methods << methodBuilder
|
||||
return this
|
||||
}
|
||||
|
||||
String build() {
|
||||
BlockBuilder clazz = new BlockBuilder(" ")
|
||||
.addLine("package $packageName;")
|
||||
.addEmptyLine()
|
||||
imports.sort().each {
|
||||
clazz.addLine("import $it;")
|
||||
}
|
||||
clazz.addEmptyLine()
|
||||
staticImports.sort().each {
|
||||
clazz.addLine("static import $it;")
|
||||
}
|
||||
clazz.addEmptyLine()
|
||||
|
||||
clazz.addLine("public class $className {")
|
||||
clazz.addEmptyLine()
|
||||
|
||||
methods.each {
|
||||
clazz.addBlock(it)
|
||||
}
|
||||
|
||||
clazz.addLine('}')
|
||||
clazz.toString()
|
||||
}
|
||||
}
|
||||
25
src/main/groovy/eu/coderate/ck/builder/MethodBuilder.groovy
Normal file
25
src/main/groovy/eu/coderate/ck/builder/MethodBuilder.groovy
Normal file
@@ -0,0 +1,25 @@
|
||||
package eu.coderate.ck.builder
|
||||
|
||||
/**
|
||||
* @author Jakub Kubrynski
|
||||
*/
|
||||
class MethodBuilder {
|
||||
|
||||
private final String methodName
|
||||
private final String returnType
|
||||
|
||||
private MethodBuilder(String returnType, String methodName) {
|
||||
this.returnType = returnType
|
||||
this.methodName = methodName
|
||||
}
|
||||
|
||||
static MethodBuilder createVoidMethod(String methodName) {
|
||||
return new MethodBuilder("void", methodName)
|
||||
}
|
||||
|
||||
void appendTo(BlockBuilder blockBuilder) {
|
||||
blockBuilder.addLine('@Test')
|
||||
blockBuilder.addLine("public $returnType $methodName {")
|
||||
.addLine('}')
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user