Skip to content

Commit

Permalink
#140 Add @dataobject for generated POJOs
Browse files Browse the repository at this point in the history
  • Loading branch information
jklingsporn committed Jul 3, 2020
1 parent 7cc8ed5 commit bdf0184
Show file tree
Hide file tree
Showing 58 changed files with 293 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@
import org.jooq.codegen.JavaWriter;
import org.jooq.meta.*;
import org.jooq.tools.JooqLogger;
import org.jooq.tools.StringUtils;

import java.io.File;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -540,6 +542,138 @@ protected void generateDao(TableDefinition table, JavaWriter out1) {
generateDAO(key, table, out);
}

//TODO --> remove copy from JavaGenerator start

@Override
protected void generatePojo(TableDefinition tableOrUDT, JavaWriter out) {
final String className = getStrategy().getJavaClassName(tableOrUDT, GeneratorStrategy.Mode.POJO);
final String interfaceName = generateInterfaces()
? out.ref(getStrategy().getFullJavaClassName(tableOrUDT, GeneratorStrategy.Mode.INTERFACE))
: "";
final String superName = out.ref(getStrategy().getJavaClassExtends(tableOrUDT, GeneratorStrategy.Mode.POJO));
final List<String> interfaces = out.ref(getStrategy().getJavaClassImplements(tableOrUDT, GeneratorStrategy.Mode.POJO));
final List<String> superTypes = list(superName, interfaces);

if (generateInterfaces())
interfaces.add(interfaceName);

printPackage(out, tableOrUDT, GeneratorStrategy.Mode.POJO);

if (tableOrUDT instanceof TableDefinition)
generatePojoClassJavadoc((TableDefinition) tableOrUDT, out);
else
generateUDTPojoClassJavadoc((UDTDefinition) tableOrUDT, out);

printClassAnnotations(out, tableOrUDT.getSchema());
generatePojoClassAnnotations(out,tableOrUDT);

if (tableOrUDT instanceof TableDefinition)
printTableJPAAnnotation(out, (TableDefinition) tableOrUDT);

int maxLength = 0;
for (TypedElementDefinition<?> column : getTypedElements(tableOrUDT))
maxLength = Math.max(maxLength, out.ref(getJavaType(column.getType(resolver(GeneratorStrategy.Mode.POJO)), GeneratorStrategy.Mode.POJO)).length());

out.println("public class %s[[before= extends ][%s]][[before= implements ][%s]] {", className, list(superName), interfaces);

if (generateSerializablePojos() || generateSerializableInterfaces())
out.printSerial();

out.println();

for (TypedElementDefinition<?> column : getTypedElements(tableOrUDT)) {
out.tab(1).println("private %s%s %s;",
generateImmutablePojos() ? "final " : "",
StringUtils.rightPad(out.ref(getJavaType(column.getType(resolver(GeneratorStrategy.Mode.POJO)), GeneratorStrategy.Mode.POJO)), maxLength),
getStrategy().getJavaMemberName(column, GeneratorStrategy.Mode.POJO));
}

// Constructors
// ---------------------------------------------------------------------

// Default constructor
if (!generateImmutablePojos())
generatePojoDefaultConstructor(tableOrUDT, out);

// [#1363] [#7055] copy constructor
generatePojoCopyConstructor(tableOrUDT, out);

// Multi-constructor
generatePojoMultiConstructor(tableOrUDT, out);

List<? extends TypedElementDefinition<?>> elements = getTypedElements(tableOrUDT);
for (int i = 0; i < elements.size(); i++) {
TypedElementDefinition<?> column = elements.get(i);

if (tableOrUDT instanceof TableDefinition)
generatePojoGetter(column, i, out);
else
generateUDTPojoGetter(column, i, out);

// Setter
if (!generateImmutablePojos())
if (tableOrUDT instanceof TableDefinition)
generatePojoSetter(column, i, out);
else
generateUDTPojoSetter(column, i, out);
}

if (generatePojosEqualsAndHashCode())
generatePojoEqualsAndHashCode(tableOrUDT, out);

if (generatePojosToString())
generatePojoToString(tableOrUDT, out);

if (generateInterfaces() && !generateImmutablePojos())
printFromAndInto(out, tableOrUDT);

if (tableOrUDT instanceof TableDefinition)
generatePojoClassFooter((TableDefinition) tableOrUDT, out);
else
generateUDTPojoClassFooter((UDTDefinition) tableOrUDT, out);

out.println("}");
closeJavaWriter(out);
}

private static final <T> List<T> list(T... objects) {
List<T> result = new ArrayList<>();

if (objects != null)
for (T object : objects)
if (object != null && !"".equals(object))
result.add(object);

return result;
}

private static final <T> List<T> list(T first, List<T> remaining) {
List<T> result = new ArrayList<>();

result.addAll(list(first));
result.addAll(remaining);

return result;
}

private List<? extends TypedElementDefinition<? extends Definition>> getTypedElements(Definition definition) {
if (definition instanceof TableDefinition)
return ((TableDefinition) definition).getColumns();
else if (definition instanceof EmbeddableDefinition)
return ((EmbeddableDefinition) definition).getColumns();
else if (definition instanceof UDTDefinition)
return ((UDTDefinition) definition).getAttributes();
else if (definition instanceof RoutineDefinition)
return ((RoutineDefinition) definition).getAllParameters();
else
throw new IllegalArgumentException("Unsupported type : " + definition);
}

//TODO remove copy from JavaGenerator end <--

protected void generatePojoClassAnnotations(JavaWriter out, TableDefinition schema) {
}

private void generateDAO(UniqueKeyDefinition key, TableDefinition table, VertxJavaWriter out) {
final String className = getStrategy().getJavaClassName(table, GeneratorStrategy.Mode.DAO);
final List<String> interfaces = out.ref(getStrategy().getJavaClassImplements(table, GeneratorStrategy.Mode.DAO));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,52 @@
package io.github.jklingsporn.vertx.jooq.generate.builder;

import java.util.EnumSet;
import java.util.Set;

public class BuildOptions {

public enum BuildFlag {
DATA_OBJECT
}

private ConverterInstantiationMethod converterInstantiationMethod;
private Set<BuildFlag> buildFlags;

public BuildOptions() {
this(ConverterInstantiationMethod.SINGLETON);
this(ConverterInstantiationMethod.SINGLETON, EnumSet.noneOf(BuildFlag.class));
addBuildFlags(BuildFlag.DATA_OBJECT);
}

public BuildOptions(ConverterInstantiationMethod converterInstantiationMethod) {
public BuildOptions(ConverterInstantiationMethod converterInstantiationMethod,Set<BuildFlag> buildFlags) {
this.converterInstantiationMethod = converterInstantiationMethod;
this.buildFlags = buildFlags;
}

public ConverterInstantiationMethod getConverterInstantiationMethod() {
return converterInstantiationMethod;
}

public Set<BuildFlag> getBuildFlags() {
return buildFlags;
}

public BuildOptions withConverterInstantiationMethod(ConverterInstantiationMethod converterInstantiationMethod) {
this.converterInstantiationMethod = converterInstantiationMethod;
return this;
}

public BuildOptions withBuildFlags(Set<BuildFlag> buildFlags) {
this.buildFlags = buildFlags;
return this;
}

public BuildOptions addBuildFlags(BuildFlag buildFlag) {
this.buildFlags.add(buildFlag);
return this;
}

boolean isEnabled(BuildFlag flag){
return buildFlags.contains(flag);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,15 @@
import io.vertx.core.json.JsonObject;
import org.jooq.codegen.JavaWriter;
import org.jooq.meta.SchemaDefinition;
import org.jooq.meta.TableDefinition;
import org.jooq.meta.TypedElementDefinition;
import org.jooq.tools.JooqLogger;
import org.jooq.tools.json.JSONArray;

import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.function.*;
import java.util.stream.Collectors;

/**
Expand All @@ -38,6 +36,8 @@ class ComponentBasedVertxGenerator extends VertxGenerator {
Consumer<JavaWriter> writeDAOClassAnnotationDelegate = (w)->{};
Consumer<JavaWriter> writeDAOConstructorAnnotationDelegate = (w)->{};
Collection<BiFunction<SchemaDefinition,Function<File,JavaWriter>,JavaWriter>> writeExtraDataDelegates = new ArrayList<>();
Collection<BiConsumer<JavaWriter, TableDefinition>> pojoClassAnnotationsDelegates = new ArrayList<>();

NamedInjectionStrategy namedInjectionStrategy = PredefinedNamedInjectionStrategy.DISABLED;
BuildOptions buildOptions = new BuildOptions();
VertxGenerator activeGenerator = this;
Expand Down Expand Up @@ -212,6 +212,11 @@ public ComponentBasedVertxGenerator setNamedInjectionStrategy(NamedInjectionStra
return this;
}

ComponentBasedVertxGenerator addGeneratePojoClassAnnotationDelegate(BiConsumer<JavaWriter,TableDefinition> consumer) {
this.pojoClassAnnotationsDelegates.add(consumer);
return this;
}

/**
*
* @return The {@code VertxGenerator} that is actually used. When using a {@code ComponentBasedVertxGenerator} inside a {@code DelegatingVertxGenerator}
Expand All @@ -238,5 +243,8 @@ final public File generateTargetFile(SchemaDefinition schema, String pkg, String
return p.resolve(name).toFile();
}


@Override
protected void generatePojoClassAnnotations(JavaWriter out, TableDefinition schema) {
pojoClassAnnotationsDelegates.forEach(c -> c.accept(out,schema));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.jooq.codegen.GeneratorStrategy;
import org.jooq.codegen.JavaWriter;
import org.jooq.meta.SchemaDefinition;
import org.jooq.meta.TableDefinition;
import org.jooq.meta.TypedElementDefinition;

import java.io.File;
Expand Down Expand Up @@ -104,5 +105,10 @@ public void setStrategy(GeneratorStrategy strategy) {
super.setStrategy(strategy);
delegate.setStrategy(strategy);
}

@Override
protected void generatePojoClassAnnotations(JavaWriter out, TableDefinition schema) {
delegate.generatePojoClassAnnotations(out,schema);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,9 @@ public ComponentBasedVertxGenerator build(BuildOptions buildOptions) {
return out;
});
}
if(buildOptions.isEnabled(BuildOptions.BuildFlag.DATA_OBJECT)){
base.addGeneratePojoClassAnnotationDelegate((out,td)->out.println("@io.vertx.codegen.annotations.DataObject"));
}
return base;
}

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit bdf0184

Please sign in to comment.