This commit is contained in:
Lukas Eder 2020-05-13 10:27:35 +02:00
parent ef991ddb12
commit 37ec8d3a34
6 changed files with 725 additions and 165 deletions

View File

@ -152,7 +152,7 @@ abstract class AbstractGenerator implements Generator {
}
enum Language {
JAVA, SCALA, XML;
JAVA, SCALA, KOTLIN, XML;
}
void logDatabaseParameters(Database db) {

View File

@ -79,7 +79,8 @@ public abstract class GeneratorWriter<W extends GeneratorWriter<W>> {
private final File file;
private final String encoding;
private final StringBuilder sb;
private int indentTabs;
private int indentTabsThisLine;
private int indentTabsAllLines;
private String tabString = " ";
private String newlineString = "\n";
private boolean newline = true;
@ -147,12 +148,12 @@ public abstract class GeneratorWriter<W extends GeneratorWriter<W>> {
public W print(String string, Object... args) {
string = string.replace("\n", newlineString).replace("\t", tabString);
if (newline && indentTabs > 0) {
for (int i = 0; i < indentTabs; i++)
if (newline && indentTabsThisLine + indentTabsAllLines > 0) {
for (int i = 0; i < indentTabsThisLine + indentTabsAllLines; i++)
sb.append(tabString);
newline = false;
indentTabs = 0;
indentTabsThisLine = 0;
}
if (args.length > 0) {
@ -257,14 +258,24 @@ public abstract class GeneratorWriter<W extends GeneratorWriter<W>> {
return (W) this;
}
@SuppressWarnings("unchecked")
public W indent(int tabs) {
this.indentTabsAllLines = tabs;
return (W) this;
}
public int indent() {
return indentTabsAllLines;
}
@SuppressWarnings("unchecked")
public W tab(int tabs) {
this.indentTabs = tabs;
this.indentTabsThisLine = tabs;
return (W) this;
}
public int tab() {
return indentTabs;
return indentTabsThisLine;
}
public boolean close() {

View File

@ -39,6 +39,7 @@ public class JavaWriter extends GeneratorWriter<JavaWriter> {
private String packageName;
private final boolean isJava;
private final boolean isScala;
private final boolean isKotlin;
public JavaWriter(File file, String fullyQualifiedTypes) {
this(file, fullyQualifiedTypes, null);
@ -49,18 +50,7 @@ public class JavaWriter extends GeneratorWriter<JavaWriter> {
}
public JavaWriter(File file, String fullyQualifiedTypes, String encoding, boolean javadoc) {
super(file, encoding, null);
this.className = file.getName().replaceAll("\\.(java|scala)$", "");
this.isJava = file.getName().endsWith(".java");
this.isScala = file.getName().endsWith(".scala");
this.fullyQualifiedTypes = fullyQualifiedTypes == null ? null : Pattern.compile(fullyQualifiedTypes);
this.javadoc = javadoc;
if (isJava)
tabString(" ");
else if (isScala)
tabString(" ");
this(file, fullyQualifiedTypes, encoding, javadoc, null);
}
public JavaWriter(File file, String fullyQualifiedTypes, String encoding, boolean javadoc, Files files) {
@ -69,10 +59,11 @@ public class JavaWriter extends GeneratorWriter<JavaWriter> {
this.className = file.getName().replaceAll("\\.(java|scala)$", "");
this.isJava = file.getName().endsWith(".java");
this.isScala = file.getName().endsWith(".scala");
this.isKotlin = file.getName().endsWith(".kt");
this.fullyQualifiedTypes = fullyQualifiedTypes == null ? null : Pattern.compile(fullyQualifiedTypes);
this.javadoc = javadoc;
if (isJava)
if (isJava || isKotlin)
tabString(" ");
else if (isScala)
tabString(" ");
@ -204,7 +195,11 @@ public class JavaWriter extends GeneratorWriter<JavaWriter> {
// [#4021] For Scala interoperability, we better also import
// java.lang types
if (isJava && imp.startsWith("java.lang."))
if ((isJava || isKotlin) && imp.startsWith("java.lang."))
continue;
// [#6248] java.lang.Integer is converted to kotlin.Int, and shouldn't be imported
if (isKotlin && imp.startsWith("kotlin.") && !imp.substring("kotlin.".length()).contains("."))
continue;
// Don't import the class itself
@ -241,6 +236,8 @@ public class JavaWriter extends GeneratorWriter<JavaWriter> {
// Skip unqualified and primitive types
if (c.contains(".")) {
if (isKotlin && Integer.class.getName().equals(c))
c = "kotlin.Int";
// com.example.Table.TABLE.COLUMN (with keepSegments = 3)
if (fullyQualifiedTypes == null || !fullyQualifiedTypes.matcher(c).matches()) {

View File

@ -0,0 +1,50 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Other licenses:
* -----------------------------------------------------------------------------
* Commercial licenses for this work are available. These replace the above
* ASL 2.0 and offer limited warranties, support, maintenance, and commercial
* database integrations.
*
* For more information, please visit: http://www.jooq.org/licenses
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*/
package org.jooq.codegen;
import static org.jooq.codegen.AbstractGenerator.Language.KOTLIN;
/**
* @author Lukas Eder
*/
public class KotlinGenerator extends JavaGenerator {
public KotlinGenerator() {
super(KOTLIN);
}
}

View File

@ -631,6 +631,7 @@
</modules>