[#1906] Use Xtend to generate Row[N], Record[N] and other code artefacts

- Added generation support for Record[N]
This commit is contained in:
Lukas Eder 2012-11-10 14:38:36 +01:00
parent 970cc7bff3
commit a476ac1957
11 changed files with 259 additions and 116 deletions

View File

@ -0,0 +1,122 @@
package org.jooq.xtend
import java.io.File
import java.io.FileWriter
import java.io.IOException
abstract class Generators {
def write(String className, CharSequence contents) {
val file = new File("./../jOOQ/src/main/java/" + className.replace(".", "/") + ".java");
file.getParentFile().mkdirs();
try {
System::out.println("Generating " + file);
val fw = new FileWriter(file);
fw.append(contents);
fw.flush();
fw.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
def first(int degree) {
switch degree {
case 1 : "first"
case 2 : "second"
case 3 : "third"
case 4 : "fourth"
case 5 : "fifth"
case 6 : "sixth"
case 7 : "seventh"
case 8 : "eighth"
case 9 : "ninth"
case 10 : "tenth"
case 11 : "eleventh"
case 12 : "twelfth"
case 13 : "thirteenth"
case 14 : "fourteenth"
case 15 : "fifteenth"
case 16 : "sixteenth"
case 17 : "seventeenth"
case 18 : "eighteenth"
case 19 : "ninteenth"
case 20 : "twentieth"
case 21 : "twenty-first"
case 22 : "twenty-second"
case degree % 10 == 1 : degree + "st"
case degree % 10 == 2 : degree + "nd"
case degree % 10 == 3 : degree + "rd"
default : degree + "th"
}
}
def classHeader() {
'''
/**
* Copyright (c) 2009-2012, Lukas Eder, lukas.eder@gmail.com
* All rights reserved.
*
* This software is licensed to you under the Apache License, Version 2.0
* (the "License"); You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* . Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* . Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* . Neither the name "jOOQ" nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
'''
}
def generatedAnnotation() {
'''
@Generated("This class was generated using jOOQ-tools")
'''
}
def TN(int degree) {
// A comma-separated list of types T1, T2, .., TN
(1..degree).join(", ", [e | "T" + e])
}
def tn(int degree) {
// A comma-separated list of types T1, T2, .., TN
(1..degree).join(", ", [e | "t" + e])
}
def TN_tn(int degree) {
// A comma-separated list of arguments T1 t1, T2 t2, .., TN tn
(1..degree).join(", ", [e | "T" + e + " t" + e])
}
def Field_TN_tn(int degree) {
(1..degree).join(", ", [e | "Field<T" + e + "> t" + e])
}
}

View File

@ -0,0 +1,111 @@
/**
* Copyright (c) 2009-2012, Lukas Eder, lukas.eder@gmail.com
* All rights reserved.
*
* This software is licensed to you under the Apache License, Version 2.0
* (the "License"); You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* . Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* . Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* . Neither the name "jOOQ" nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package org.jooq.xtend
import org.jooq.Constants
/**
* @author Lukas Eder
*/
class Records extends Generators {
def static void main(String[] args) {
val records = new Records();
records.generateRecordClasses();
}
def generateRecordClasses() {
for (degree : (1..Constants::MAX_ROW_DEGREE)) {
val out = new StringBuilder();
out.append('''
«classHeader»
package org.jooq;
import javax.annotation.Generated;
/**
* A model type for a records with degree <code>«degree»</code>
*
* @see Row«degree»
* @author Lukas Eder
*/
«generatedAnnotation»
public interface Record«degree»<«TN(degree)»> extends Record {
// ------------------------------------------------------------------------
// Row value expressions
// ------------------------------------------------------------------------
/**
* Get this record's fields as a {@link Row«degree»}
*/
Row«degree»<«TN(degree)»> fieldsRow();
/**
* Get this record's values as a {@link Row«degree»}
*/
Row«degree»<«TN(degree)»> valuesRow();
// ------------------------------------------------------------------------
// Field accessors
// ------------------------------------------------------------------------
«FOR d : (1..degree)»
/**
* Get the «first(d)» field
*/
Field<T«d»> field«d»();
«ENDFOR»
// ------------------------------------------------------------------------
// Value accessors
// ------------------------------------------------------------------------
«FOR d : (1..degree)»
/**
* Get the «first(d)» value
*/
T«d» value«d»();
«ENDFOR»
}
''');
write("org.jooq.Record" + degree, out);
}
}
}

View File

@ -35,133 +35,19 @@
*/
package org.jooq.xtend
import java.io.File
import java.io.FileWriter
import java.io.IOException
import org.jooq.Constants
/**
* @author Lukas Eder
*/
class Rows {
class Rows extends Generators {
def static void main(String[] args) {
val rows = new Rows();
rows.generateRowClasses();
rows.generateRowImpl();
}
def write(String className, CharSequence contents) {
val file = new File("./../jOOQ/src/main/java/" + className.replace(".", "/") + ".java");
file.getParentFile().mkdirs();
try {
System::out.println("Generating " + file);
val fw = new FileWriter(file);
fw.append(contents);
fw.flush();
fw.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
def first(int degree) {
switch degree {
case 1 : "first"
case 2 : "second"
case 3 : "third"
case 4 : "fourth"
case 5 : "fifth"
case 6 : "sixth"
case 7 : "seventh"
case 8 : "eighth"
case 9 : "ninth"
case 10 : "tenth"
case 11 : "eleventh"
case 12 : "twelfth"
case 13 : "thirteenth"
case 14 : "fourteenth"
case 15 : "fifteenth"
case 16 : "sixteenth"
case 17 : "seventeenth"
case 18 : "eighteenth"
case 19 : "ninteenth"
case 20 : "twentieth"
case 21 : "twenty-first"
case 22 : "twenty-second"
case degree % 10 == 1 : degree + "st"
case degree % 10 == 2 : degree + "nd"
case degree % 10 == 3 : degree + "rd"
default : degree + "th"
}
}
def classHeader() {
'''
/**
* Copyright (c) 2009-2012, Lukas Eder, lukas.eder@gmail.com
* All rights reserved.
*
* This software is licensed to you under the Apache License, Version 2.0
* (the "License"); You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* . Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* . Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* . Neither the name "jOOQ" nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
'''
}
def generatedAnnotation() {
'''
@Generated("This class was generated using jOOQ-tools")
'''
}
def TN(int degree) {
// A comma-separated list of types T1, T2, .., TN
(1..degree).join(", ", [e | "T" + e])
}
def tn(int degree) {
// A comma-separated list of types T1, T2, .., TN
(1..degree).join(", ", [e | "t" + e])
}
def TN_tn(int degree) {
// A comma-separated list of arguments T1 t1, T2 t2, .., TN tn
(1..degree).join(", ", [e | "T" + e + " t" + e])
}
def Field_TN_tn(int degree) {
(1..degree).join(", ", [e | "Field<T" + e + "> t" + e])
}
def generateRowClasses() {
for (degree : (1..Constants::MAX_ROW_DEGREE)) {
val out = new StringBuilder();

View File

@ -35,12 +35,15 @@
*/
package org.jooq;
import javax.annotation.Generated;
/**
* A model type for a records with degree <code>1</code>
*
* @see Row1
* @author Lukas Eder
*/
@Generated("This class was generated using jOOQ-tools")
public interface Record1<T1> extends Record {
// ------------------------------------------------------------------------

View File

@ -35,12 +35,15 @@
*/
package org.jooq;
import javax.annotation.Generated;
/**
* A model type for a records with degree <code>2</code>
*
* @see Row2
* @author Lukas Eder
*/
@Generated("This class was generated using jOOQ-tools")
public interface Record2<T1, T2> extends Record {
// ------------------------------------------------------------------------

View File

@ -35,12 +35,15 @@
*/
package org.jooq;
import javax.annotation.Generated;
/**
* A model type for a records with degree <code>3</code>
*
* @see Row3
* @author Lukas Eder
*/
@Generated("This class was generated using jOOQ-tools")
public interface Record3<T1, T2, T3> extends Record {
// ------------------------------------------------------------------------

View File

@ -35,12 +35,15 @@
*/
package org.jooq;
import javax.annotation.Generated;
/**
* A model type for a records with degree <code>4</code>
*
* @see Row4
* @author Lukas Eder
*/
@Generated("This class was generated using jOOQ-tools")
public interface Record4<T1, T2, T3, T4> extends Record {
// ------------------------------------------------------------------------

View File

@ -35,12 +35,15 @@
*/
package org.jooq;
import javax.annotation.Generated;
/**
* A model type for a records with degree <code>5</code>
*
* @see Row5
* @author Lukas Eder
*/
@Generated("This class was generated using jOOQ-tools")
public interface Record5<T1, T2, T3, T4, T5> extends Record {
// ------------------------------------------------------------------------

View File

@ -35,12 +35,15 @@
*/
package org.jooq;
import javax.annotation.Generated;
/**
* A model type for a records with degree <code>6</code>
*
* @see Row6
* @author Lukas Eder
*/
@Generated("This class was generated using jOOQ-tools")
public interface Record6<T1, T2, T3, T4, T5, T6> extends Record {
// ------------------------------------------------------------------------

View File

@ -35,12 +35,15 @@
*/
package org.jooq;
import javax.annotation.Generated;
/**
* A model type for a records with degree <code>7</code>
*
* @see Row7
* @author Lukas Eder
*/
@Generated("This class was generated using jOOQ-tools")
public interface Record7<T1, T2, T3, T4, T5, T6, T7> extends Record {
// ------------------------------------------------------------------------

View File

@ -35,12 +35,15 @@
*/
package org.jooq;
import javax.annotation.Generated;
/**
* A model type for a records with degree <code>8</code>
*
* @see Row8
* @author Lukas Eder
*/
@Generated("This class was generated using jOOQ-tools")
public interface Record8<T1, T2, T3, T4, T5, T6, T7, T8> extends Record {
// ------------------------------------------------------------------------