diff --git a/src/main/java/org/joda/beans/gen/BeanCodeGen.java b/src/main/java/org/joda/beans/gen/BeanCodeGen.java
index 40329fb8..33fc4688 100644
--- a/src/main/java/org/joda/beans/gen/BeanCodeGen.java
+++ b/src/main/java/org/joda/beans/gen/BeanCodeGen.java
@@ -15,18 +15,26 @@
*/
package org.joda.beans.gen;
+import static java.util.stream.Collectors.toList;
+
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
+import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
+import java.io.UncheckedIOException;
import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Pattern;
+import java.util.stream.Stream;
import org.joda.beans.JodaBeanUtils;
@@ -37,6 +45,8 @@
*/
public class BeanCodeGen {
+ private static final Pattern PATTERN_OVERRIDE = Pattern.compile(" *[@]Override");
+
/**
* Main method.
*
@@ -99,7 +109,6 @@ public static BeanCodeGen createFromArgs(String[] args) {
boolean generatedAnno = false;
int verbosity = 1;
boolean write = true;
- File file = null;
BeanGenConfig config = null;
if (args.length == 0) {
throw new IllegalArgumentException("No arguments specified");
@@ -160,7 +169,7 @@ public static BeanCodeGen createFromArgs(String[] args) {
throw new IllegalArgumentException("Unknown argument: " + arg);
}
}
- file = new File(args[args.length - 1]);
+ Path file = Paths.get(args[args.length - 1]);
List files = findFiles(file, recurse);
if (config == null) {
@@ -183,29 +192,15 @@ public static BeanCodeGen createFromArgs(String[] args) {
* @param recurse whether to recurse
* @return the files, not null
*/
- private static List findFiles(final File parent, boolean recurse) {
- final List result = new ArrayList<>();
- if (parent.isDirectory()) {
- File[] files = parent.listFiles();
- files = (files != null ? files : new File[0]);
- for (File child : files) {
- if (child.isFile() && child.getName().endsWith(".java")) {
- result.add(child);
- }
- }
- if (recurse) {
- for (File child : files) {
- if (child.isDirectory() && !child.getName().startsWith(".")) {
- result.addAll(findFiles(child, recurse));
- }
- }
- }
- } else {
- if (parent.getName().endsWith(".java")) {
- result.add(parent);
- }
+ private static List findFiles(Path parent, boolean recurse) {
+ try (Stream pathStream = Files.walk(parent, recurse ? Integer.MAX_VALUE : 1)) {
+ return pathStream
+ .filter(path -> path.toString().endsWith(".java"))
+ .map(path -> path.toFile())
+ .collect(toList());
+ } catch (IOException ex) {
+ throw new UncheckedIOException(ex);
}
- return result;
}
//-----------------------------------------------------------------------
@@ -351,7 +346,6 @@ private File processFile(File file) throws Exception {
// checks to see if the content differs from the original
// if the files differ only by @Override lines then they are considered to be equal
private boolean contentDiffers(List content, List original) {
- Pattern overridePattern = Pattern.compile(" *[@]Override");
int contentIndex = 0;
int originalIndex = 0;
while (contentIndex < content.size() && originalIndex < original.size()) {
@@ -361,14 +355,14 @@ private boolean contentDiffers(List content, List original) {
// lines match
contentIndex++;
originalIndex++;
- } else if (overridePattern.matcher(originalLine).matches()) {
+ } else if (PATTERN_OVERRIDE.matcher(originalLine).matches()) {
// original is an @Override line
originalIndex++;
} else {
return true;
}
}
- return contentIndex < content.size() || originalIndex < original.size();
+ return contentIndex < content.size() || originalIndex < original.size();
}
//-----------------------------------------------------------------------
diff --git a/src/main/java/org/joda/beans/gen/BeanData.java b/src/main/java/org/joda/beans/gen/BeanData.java
index 2f74dfe3..7f97b8ba 100644
--- a/src/main/java/org/joda/beans/gen/BeanData.java
+++ b/src/main/java/org/joda/beans/gen/BeanData.java
@@ -123,8 +123,16 @@ public SortedSet getNewImports() {
* @param cls the class, not null
*/
public void ensureImport(Class> cls) {
- if (!currentImports.contains(cls.getName())) {
- newImports.add(cls.getName());
+ ensureImport(cls.getName());
+ }
+
+ /**
+ * Ensures an import is present.
+ * @param className the class name, not null
+ */
+ void ensureImport(String className) {
+ if (!currentImports.contains(className)) {
+ newImports.add(className);
}
}
@@ -447,12 +455,7 @@ public void setCacheHashCode(boolean cacheHashCode) {
* @return the flag
*/
public boolean isPropertyChangeSupport() {
- for (PropertyData prop : properties) {
- if (prop.isBound()) {
- return true;
- }
- }
- return false;
+ return properties.stream().anyMatch(PropertyData::isBound);
}
//-----------------------------------------------------------------------
@@ -1074,12 +1077,7 @@ public String getSuperTypeRaw() {
* @return true if validated
*/
public boolean isValidated() {
- for (PropertyData property : properties) {
- if (property.isValidated()) {
- return true;
- }
- }
- return false;
+ return properties.stream().anyMatch(PropertyData::isValidated);
}
/**
diff --git a/src/main/java/org/joda/beans/gen/BeanGen.java b/src/main/java/org/joda/beans/gen/BeanGen.java
index 8307af16..d286858b 100644
--- a/src/main/java/org/joda/beans/gen/BeanGen.java
+++ b/src/main/java/org/joda/beans/gen/BeanGen.java
@@ -60,22 +60,10 @@ class BeanGen {
static final int CONSTRUCTOR_BY_BUILDER = 1;
/** Constructor style for argument-based. */
static final int CONSTRUCTOR_BY_ARGS = 2;
- /** Class constant, avoiding module dependency in Java 9. */
- private static final Class> CLASS_CONSTRUCTOR_PROPERTIES;
- /** Class constant, avoiding module dependency in Java 9. */
- private static final Class> CLASS_PROPERTY_CHANGE_SUPPORT;
- static {
- Class> cls1 = null;
- Class> cls2 = null;
- try {
- cls1 = Class.forName("java.beans.ConstructorProperties");
- cls2 = Class.forName("java.beans.PropertyChangeSupport");
- } catch (ClassNotFoundException ex) {
- // ignore
- }
- CLASS_CONSTRUCTOR_PROPERTIES = cls1;
- CLASS_PROPERTY_CHANGE_SUPPORT = cls2;
- }
+ /** Class name - this avoids a Class.class reference to the java.desktop module. */
+ private static final String CLASS_CONSTRUCTOR_PROPERTIES = "java.beans.ConstructorProperties";
+ /** Class name - this avoids a Class.class reference to the java.desktop module. */
+ private static final String CLASS_PROPERTY_CHANGE_SUPPORT = "java.beans.PropertyChangeSupport";
/** Line separator. */
private static final String LINE_SEPARATOR = "\t//-----------------------------------------------------------------------";
/** Line separator. */
@@ -960,8 +948,7 @@ private void generateToString() {
if (data.isSubClass()) {
addLine(2, "super.toString(buf);");
}
- for (int i = 0; i < props.size(); i++) {
- PropertyGen prop = props.get(i);
+ for (PropertyGen prop : props) {
String getter = toStringFieldAccessor(prop);
data.ensureImport(JodaBeanUtils.class);
addLine(2, "buf.append(\"" + prop.getData().getPropertyName() +
@@ -1311,8 +1298,8 @@ private void generateBuilderConstructorCopy() {
if (data.isSubClass()) {
addLine(3, "super(beanToCopy);");
}
- for (int i = 0; i < nonDerived.size(); i++) {
- addLines(nonDerived.get(i).generateBuilderConstructorAssign("beanToCopy"));
+ for (PropertyGen propertyGen : nonDerived) {
+ addLines(propertyGen.generateBuilderConstructorAssign("beanToCopy"));
}
addLine(2, "}");
addBlankLine();
diff --git a/src/main/java/org/joda/beans/gen/PropertyDefinition.java b/src/main/java/org/joda/beans/gen/PropertyDefinition.java
index d14eee41..ebc91c1c 100644
--- a/src/main/java/org/joda/beans/gen/PropertyDefinition.java
+++ b/src/main/java/org/joda/beans/gen/PropertyDefinition.java
@@ -108,7 +108,7 @@
* '$field' for the field to copy into.
* '$value' for the value to copy from.
* '<>' for the generics of the type including angle brackets.
- * '\n' for a new line (all lines must then include semi-colons).
+ * '\n' for a new line (all lines must then include semicolons).
*
* @return the setter style, defaulted to 'smart'
*/
diff --git a/src/main/java/org/joda/beans/gen/PropertyParser.java b/src/main/java/org/joda/beans/gen/PropertyParser.java
index 1160cdf8..d27f2230 100644
--- a/src/main/java/org/joda/beans/gen/PropertyParser.java
+++ b/src/main/java/org/joda/beans/gen/PropertyParser.java
@@ -364,7 +364,7 @@ private String parseFieldInitializer(List content) {
if (line.contains(" = ")) {
line = line.substring(line.indexOf(" = ") + 3).trim();
if (!line.endsWith(";")) {
- throw new BeanCodeGenException("Field line does not end with semi-colon", beanParser.getFile(), fieldIndex);
+ throw new BeanCodeGenException("Field line does not end with semicolon", beanParser.getFile(), fieldIndex);
}
return line.substring(0, line.length() - 1).trim();
}