[#4021] Add a ScalaGenerator

This commit is contained in:
lukaseder 2015-02-08 19:02:26 +01:00
parent 0a30844ab6
commit 7cdbbe29f6
4 changed files with 1475 additions and 447 deletions

View File

@ -496,7 +496,7 @@ public class GenerationTool {
// [#2801]
catch (ClassNotFoundException e) {
if (className.startsWith("org.jooq.util.")) {
if (className.startsWith("org.jooq.util.") && className.endsWith("Database")) {
log.warn("Licensing", "With jOOQ 3.2, licensing has changed, and your database may no longer be supported with jOOQ Open Source Edition. See http://www.jooq.org/licensing for details");
}

File diff suppressed because it is too large Load Diff

View File

@ -31,11 +31,17 @@ public class JavaWriter extends GeneratorWriter<JavaWriter> {
private final Set<String> qualifiedTypes = new TreeSet<String>();
private final Map<String, String> unqualifiedTypes = new TreeMap<String, String>();
private final String className;
private final boolean isJava;
private final boolean isScala;
private final Pattern REF_PATTERN = Pattern
.compile("((?:[\\p{L}_$][\\p{L}\\p{N}_$]*\\.)*[\\p{L}_$][\\p{L}\\p{N}_$]*)((?:<.*>|\\[.*\\])*)"); ;
public JavaWriter(File file, String fullyQualifiedTypes) {
super(file);
this.className = file.getName().replace(".java", "");
this.isJava = file.getName().endsWith(".java");
this.isScala = file.getName().endsWith(".scala");
this.fullyQualifiedTypes = fullyQualifiedTypes == null ? null : Pattern.compile(fullyQualifiedTypes);
}
@ -97,8 +103,10 @@ public class JavaWriter extends GeneratorWriter<JavaWriter> {
}
public void printSerial() {
println();
println("\tprivate static final long serialVersionUID = %s;", SERIAL_STATEMENT);
if (isJava) {
println();
println("\tprivate static final long serialVersionUID = %s;", SERIAL_STATEMENT);
}
}
public void printImports() {
@ -111,7 +119,10 @@ public class JavaWriter extends GeneratorWriter<JavaWriter> {
String previous = "";
for (String imp : qualifiedTypes) {
if (imp.startsWith("java.lang."))
// [#4021] For Scala interoperability, we better also import
// java.lang types
if (isJava && imp.startsWith("java.lang."))
continue;
String topLevelPackage = imp.split("\\.")[0];
@ -121,7 +132,7 @@ public class JavaWriter extends GeneratorWriter<JavaWriter> {
importString.append("import ")
.append(imp)
.append(";\n");
.append(isScala ? "\n" : ";\n");
previous = topLevelPackage;
}
@ -143,12 +154,7 @@ public class JavaWriter extends GeneratorWriter<JavaWriter> {
// com.example.Table.TABLE.COLUMN (with keepSegments = 3)
if (fullyQualifiedTypes == null || !fullyQualifiedTypes.matcher(c).matches()) {
// That's a unicode-safe \w, more or less:
// - http://stackoverflow.com/a/4307261/521799
// - http://stackoverflow.com/a/5205467/521799
Pattern p = Pattern.compile("((?:[\\p{L}_$][\\p{L}\\p{N}_$]*\\.)*[\\p{L}_$][\\p{L}\\p{N}_$]*)((?:<.*>|\\[\\])*)");
Matcher m = p.matcher(c);
Matcher m = REF_PATTERN.matcher(c);
if (m.find()) {
@ -167,10 +173,10 @@ public class JavaWriter extends GeneratorWriter<JavaWriter> {
if (!className.equals(unqualifiedType) &&
(!unqualifiedTypes.containsKey(unqualifiedType) || qualifiedType.equals(unqualifiedTypes.get(unqualifiedType)))) {
unqualifiedTypes.put(unqualifiedType, qualifiedType);
qualifiedTypes.add(qualifiedType);
c = remainder + m.group(2);
}
unqualifiedTypes.put(unqualifiedType, qualifiedType);
qualifiedTypes.add(qualifiedType);
c = remainder + m.group(2);
}
}
}
}

View File

@ -0,0 +1,53 @@
/**
* Copyright (c) 2009-2015, Data Geekery GmbH (http://www.datageekery.com)
* All rights reserved.
*
* This work is dual-licensed
* - under the Apache Software License 2.0 (the "ASL")
* - under the jOOQ License and Maintenance Agreement (the "jOOQ License")
* =============================================================================
* You may choose which license applies to you:
*
* - If you're using this work with Open Source databases, you may choose
* either ASL or jOOQ License.
* - If you're using this work with at least one commercial database, you must
* choose jOOQ License
*
* For more information, please visit http://www.jooq.org/licenses
*
* Apache Software License 2.0:
* -----------------------------------------------------------------------------
* 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.
*
* jOOQ License and Maintenance Agreement:
* -----------------------------------------------------------------------------
* Data Geekery grants the Customer the non-exclusive, timely limited and
* non-transferable license to install and use the Software under the terms of
* the jOOQ License and Maintenance Agreement.
*
* This library is distributed with a LIMITED WARRANTY. See the jOOQ License
* and Maintenance Agreement for more details: http://www.jooq.org/licensing
*/
package org.jooq.util;
import static org.jooq.util.JavaGenerator.Language.SCALA;
/**
* @author Lukas Eder
*/
public class ScalaGenerator extends JavaGenerator {
public ScalaGenerator() {
super(SCALA);
}
}