Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modernize bean generation code #411

Merged
merged 1 commit into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 21 additions & 27 deletions src/main/java/org/joda/beans/gen/BeanCodeGen.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -37,6 +45,8 @@
*/
public class BeanCodeGen {

private static final Pattern PATTERN_OVERRIDE = Pattern.compile(" *[@]Override");

/**
* Main method.
* <p>
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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<File> files = findFiles(file, recurse);

if (config == null) {
Expand All @@ -183,29 +192,15 @@ public static BeanCodeGen createFromArgs(String[] args) {
* @param recurse whether to recurse
* @return the files, not null
*/
private static List<File> findFiles(final File parent, boolean recurse) {
final List<File> 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<File> findFiles(Path parent, boolean recurse) {
try (Stream<Path> 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;
}

//-----------------------------------------------------------------------
Expand Down Expand Up @@ -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<String> content, List<String> original) {
Pattern overridePattern = Pattern.compile(" *[@]Override");
int contentIndex = 0;
int originalIndex = 0;
while (contentIndex < content.size() && originalIndex < original.size()) {
Expand All @@ -361,14 +355,14 @@ private boolean contentDiffers(List<String> content, List<String> 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();
}

//-----------------------------------------------------------------------
Expand Down
26 changes: 12 additions & 14 deletions src/main/java/org/joda/beans/gen/BeanData.java
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,16 @@ public SortedSet<String> 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);
}
}

Expand Down Expand Up @@ -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);
}

//-----------------------------------------------------------------------
Expand Down Expand Up @@ -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);
}

/**
Expand Down
27 changes: 7 additions & 20 deletions src/main/java/org/joda/beans/gen/BeanGen.java
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down Expand Up @@ -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() +
Expand Down Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/joda/beans/gen/PropertyDefinition.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@
* '$field' for the field to copy into.<br/>
* '$value' for the value to copy from.<br/>
* '&lt;&gt;' for the generics of the type including angle brackets.<br/>
* '\n' for a new line (all lines must then include semi-colons).<br/>
* '\n' for a new line (all lines must then include semicolons).<br/>
*
* @return the setter style, defaulted to 'smart'
*/
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/joda/beans/gen/PropertyParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ private String parseFieldInitializer(List<String> 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();
}
Expand Down