diff --git a/jOOQ-codegen/src/main/java/org/jooq/codegen/Files.java b/jOOQ-codegen/src/main/java/org/jooq/codegen/Files.java new file mode 100644 index 0000000000..ffe64b95c8 --- /dev/null +++ b/jOOQ-codegen/src/main/java/org/jooq/codegen/Files.java @@ -0,0 +1,84 @@ +/* + * 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 java.io.File; +import java.io.FilenameFilter; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +/** + * A cache for directories and their contents. + * + * @author Lukas Eder + */ +public final class Files { + + private final Map lists; + private final Set mkdirs; + + public Files() { + this.lists = new HashMap(); + this.mkdirs = new HashSet(); + } + + public final String[] list(File dir, FilenameFilter filter) { + String[] list = lists.get(dir); + + if (list == null) { + list = dir.list(); + lists.put(dir, list); + } + + List result = new ArrayList(); + for (String s : list) + if (filter.accept(dir, s)) + result.add(s); + + return result.toArray(new String[0]); + } + + public final void mkdirs(File dir) { + if (mkdirs.add(dir)) + dir.mkdirs(); + } +} diff --git a/jOOQ-codegen/src/main/java/org/jooq/codegen/GeneratorWriter.java b/jOOQ-codegen/src/main/java/org/jooq/codegen/GeneratorWriter.java index bf5fc5e1bb..6fb2170e41 100644 --- a/jOOQ-codegen/src/main/java/org/jooq/codegen/GeneratorWriter.java +++ b/jOOQ-codegen/src/main/java/org/jooq/codegen/GeneratorWriter.java @@ -76,7 +76,7 @@ public abstract class GeneratorWriter> { "(?:\\[(.*)\\])" + "\\]", Pattern.DOTALL); - + private final Files files; private final File file; private final String encoding; private final StringBuilder sb; @@ -86,15 +86,24 @@ public abstract class GeneratorWriter> { private boolean newline = true; protected GeneratorWriter(File file) { - this(file, null); + this(file, null, null); } protected GeneratorWriter(File file, String encoding) { - file.getParentFile().mkdirs(); + this(file, encoding, null); + } + protected GeneratorWriter(File file, Files files) { + this(file, null, files); + } + + protected GeneratorWriter(File file, String encoding, Files files) { + this.files = files == null ? new Files() : files; this.file = file; this.encoding = encoding; this.sb = new StringBuilder(); + + this.files.mkdirs(file.getParentFile()); } public void tabString(String string) { @@ -265,7 +274,7 @@ public abstract class GeneratorWriter> { // [#5892] On Windows FAT or NTFS and other case-insensitive file systems, we must // explicitly replace files whose case-sensitive file name has changed - String[] list = file.getParentFile().list(new FilenameFilter() { + String[] list = files.list(file.getParentFile(), new FilenameFilter() { @Override public boolean accept(File dir, String name) { return name.equalsIgnoreCase(file.getName()) && !name.equals(file.getName()); diff --git a/jOOQ-codegen/src/main/java/org/jooq/codegen/JavaGenerator.java b/jOOQ-codegen/src/main/java/org/jooq/codegen/JavaGenerator.java index 6700fe11f0..93358d3e06 100644 --- a/jOOQ-codegen/src/main/java/org/jooq/codegen/JavaGenerator.java +++ b/jOOQ-codegen/src/main/java/org/jooq/codegen/JavaGenerator.java @@ -244,6 +244,7 @@ public class JavaGenerator extends AbstractGenerator { private final boolean scala; private final String tokenVoid; + private final Files fileCache; static { SQLDATATYPE_LITERAL_LOOKUP = new IdentityHashMap, String>(); @@ -287,6 +288,7 @@ public class JavaGenerator extends AbstractGenerator { this.scala = (language == SCALA); this.tokenVoid = (scala ? "Unit" : "void"); + this.fileCache = new Files(); } @Override @@ -6504,7 +6506,7 @@ public class JavaGenerator extends AbstractGenerator { // [#3880] Users may need to call this method protected JavaWriter newJavaWriter(File file) { file = fixSuffix(file); - JavaWriter result = new JavaWriter(file, generateFullyQualifiedTypes(), targetEncoding, generateJavadoc()); + JavaWriter result = new JavaWriter(file, generateFullyQualifiedTypes(), targetEncoding, generateJavadoc(), fileCache); if (generateIndentation != null) result.tabString(generateIndentation); diff --git a/jOOQ-codegen/src/main/java/org/jooq/codegen/JavaWriter.java b/jOOQ-codegen/src/main/java/org/jooq/codegen/JavaWriter.java index eee405bd30..60ea43383f 100644 --- a/jOOQ-codegen/src/main/java/org/jooq/codegen/JavaWriter.java +++ b/jOOQ-codegen/src/main/java/org/jooq/codegen/JavaWriter.java @@ -48,7 +48,22 @@ public class JavaWriter extends GeneratorWriter { } public JavaWriter(File file, String fullyQualifiedTypes, String encoding, boolean javadoc) { - super(file, encoding); + 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(" "); + } + + public JavaWriter(File file, String fullyQualifiedTypes, String encoding, boolean javadoc, Files files) { + super(file, encoding, files); this.className = file.getName().replaceAll("\\.(java|scala)$", ""); this.isJava = file.getName().endsWith(".java");