[jOOQ/jOOQ#10677¨Add <onUnused/> to code generation configuration to specify behaviour when encountering unused objects

This commit is contained in:
Lukas Eder 2022-05-11 16:05:07 +02:00
parent 124640771f
commit 78792b0659
4 changed files with 77 additions and 12 deletions

View File

@ -137,6 +137,14 @@ public class Plugin extends AbstractMojo {
)
private org.jooq.meta.jaxb.OnError onError;
/**
* The on-unused behavior.
*/
@Parameter(
property = "jooq.codegen.onUnused"
)
private org.jooq.meta.jaxb.OnError onUnused;
/**
* The jdbc settings.
*/
@ -197,6 +205,7 @@ public class Plugin extends AbstractMojo {
Configuration configuration = new Configuration();
configuration.setLogging(logging);
configuration.setOnError(onError);
configuration.setOnUnused(onUnused);
configuration.setJdbc(jdbc);
configuration.setGenerator(generator);
configuration.setBasedir(actualBasedir);
@ -239,6 +248,7 @@ public class Plugin extends AbstractMojo {
Configuration configuration = GenerationTool.load(in);
logging = MiniJAXB.append(logging, configuration.getLogging());
onError = MiniJAXB.append(onError, configuration.getOnError());
onUnused = MiniJAXB.append(onUnused, configuration.getOnUnused());
jdbc = MiniJAXB.append(jdbc, configuration.getJdbc());
generator = MiniJAXB.append(generator, configuration.getGenerator());
}

View File

@ -937,17 +937,24 @@ public class GenerationTool {
verifyVersions();
generator.generate(database);
logUnused("forced type", "forced types", database.getUnusedForcedTypes());
logUnused("embeddable", "embeddables", database.getUnusedEmbeddables());
logUnused("comment", "comments", database.getUnusedComments());
logUnused("synthetic column", "synthetic columns", database.getUnusedSyntheticColumns());
logUnused("synthetic readonly column", "synthetic readonly columns", database.getUnusedSyntheticReadonlyColumns());
logUnused("synthetic readonly rowid", "synthetic readonly rowids", database.getUnusedSyntheticReadonlyRowids());
logUnused("synthetic identity", "synthetic identities", database.getUnusedSyntheticIdentities());
logUnused("synthetic primary key", "synthetic primary keys", database.getUnusedSyntheticPrimaryKeys());
logUnused("synthetic unique key", "synthetic unique keys", database.getUnusedSyntheticUniqueKeys());
logUnused("synthetic foreign key", "synthetic foreign keys", database.getUnusedSyntheticForeignKeys());
logUnused("synthetic view", "synthetic views", database.getUnusedSyntheticViews());
if (configuration.getOnUnused() != OnError.SILENT) {
boolean anyUnused = false;
anyUnused = anyUnused | logUnused("forced type", "forced types", database.getUnusedForcedTypes());
anyUnused = anyUnused | logUnused("embeddable", "embeddables", database.getUnusedEmbeddables());
anyUnused = anyUnused | logUnused("comment", "comments", database.getUnusedComments());
anyUnused = anyUnused | logUnused("synthetic column", "synthetic columns", database.getUnusedSyntheticColumns());
anyUnused = anyUnused | logUnused("synthetic readonly column", "synthetic readonly columns", database.getUnusedSyntheticReadonlyColumns());
anyUnused = anyUnused | logUnused("synthetic readonly rowid", "synthetic readonly rowids", database.getUnusedSyntheticReadonlyRowids());
anyUnused = anyUnused | logUnused("synthetic identity", "synthetic identities", database.getUnusedSyntheticIdentities());
anyUnused = anyUnused | logUnused("synthetic primary key", "synthetic primary keys", database.getUnusedSyntheticPrimaryKeys());
anyUnused = anyUnused | logUnused("synthetic unique key", "synthetic unique keys", database.getUnusedSyntheticUniqueKeys());
anyUnused = anyUnused | logUnused("synthetic foreign key", "synthetic foreign keys", database.getUnusedSyntheticForeignKeys());
anyUnused = anyUnused | logUnused("synthetic view", "synthetic views", database.getUnusedSyntheticViews());
if (anyUnused && configuration.getOnUnused() == OnError.FAIL)
throw new GeneratorException("Unused configuration elements encountered");
}
}
finally {
if (database != null)
@ -1012,7 +1019,7 @@ public class GenerationTool {
}
}
private void logUnused(String objectType, String objectTypes, List<?> list) {
private boolean logUnused(String objectType, String objectTypes, List<?> list) {
if (!list.isEmpty() && Boolean.parseBoolean(System.getProperty("jooq.codegen.logunused", "true"))) {
unusedLogger.warn(
"Unused " + objectTypes,
@ -1027,7 +1034,11 @@ public class GenerationTool {
for (Object o : list)
unusedLogger.warn("Unused " + objectType, o);
return true;
}
else
return false;
}
private static void setGlobalLoggingThreshold(Configuration configuration) {

View File

@ -26,6 +26,7 @@ import org.jooq.util.jaxb.tools.XMLBuilder;
* &lt;all&gt;
* &lt;element name="logging" type="{http://www.jooq.org/xsd/jooq-codegen-3.17.0.xsd}Logging" minOccurs="0"/&gt;
* &lt;element name="onError" type="{http://www.jooq.org/xsd/jooq-codegen-3.17.0.xsd}OnError" minOccurs="0"/&gt;
* &lt;element name="onUnused" type="{http://www.jooq.org/xsd/jooq-codegen-3.17.0.xsd}OnError" minOccurs="0"/&gt;
* &lt;element name="jdbc" type="{http://www.jooq.org/xsd/jooq-codegen-3.17.0.xsd}Jdbc" minOccurs="0"/&gt;
* &lt;element name="generator" type="{http://www.jooq.org/xsd/jooq-codegen-3.17.0.xsd}Generator"/&gt;
* &lt;element name="basedir" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/&gt;
@ -54,6 +55,9 @@ public class Configuration implements Serializable, XMLAppendable
@XmlElement(defaultValue = "FAIL")
@XmlSchemaType(name = "string")
protected OnError onError = OnError.FAIL;
@XmlElement(defaultValue = "LOG")
@XmlSchemaType(name = "string")
protected OnError onUnused = OnError.LOG;
protected Jdbc jdbc;
@XmlElement(required = true)
protected Generator generator;
@ -92,6 +96,22 @@ public class Configuration implements Serializable, XMLAppendable
this.onError = value;
}
/**
* The action to be taken by the generator as the consequence of unused objects being encountered. Defaults to LOG.
*
*/
public OnError getOnUnused() {
return onUnused;
}
/**
* The action to be taken by the generator as the consequence of unused objects being encountered. Defaults to LOG.
*
*/
public void setOnUnused(OnError value) {
this.onUnused = value;
}
/**
* The JDBC configuration element contains information about how to set up the database connection used for source code generation.
*
@ -158,6 +178,15 @@ public class Configuration implements Serializable, XMLAppendable
return this;
}
/**
* The action to be taken by the generator as the consequence of unused objects being encountered. Defaults to LOG.
*
*/
public Configuration withOnUnused(OnError value) {
setOnUnused(value);
return this;
}
/**
* The JDBC configuration element contains information about how to set up the database connection used for source code generation.
*
@ -189,6 +218,7 @@ public class Configuration implements Serializable, XMLAppendable
public final void appendTo(XMLBuilder builder) {
builder.append("logging", logging);
builder.append("onError", onError);
builder.append("onUnused", onUnused);
builder.append("jdbc", jdbc);
builder.append("generator", generator);
builder.append("basedir", basedir);
@ -231,6 +261,15 @@ public class Configuration implements Serializable, XMLAppendable
return false;
}
}
if (onUnused == null) {
if (other.onUnused!= null) {
return false;
}
} else {
if (!onUnused.equals(other.onUnused)) {
return false;
}
}
if (jdbc == null) {
if (other.jdbc!= null) {
return false;
@ -267,6 +306,7 @@ public class Configuration implements Serializable, XMLAppendable
int result = 1;
result = ((prime*result)+((logging == null)? 0 :logging.hashCode()));
result = ((prime*result)+((onError == null)? 0 :onError.hashCode()));
result = ((prime*result)+((onUnused == null)? 0 :onUnused.hashCode()));
result = ((prime*result)+((jdbc == null)? 0 :jdbc.hashCode()));
result = ((prime*result)+((generator == null)? 0 :generator.hashCode()));
result = ((prime*result)+((basedir == null)? 0 :basedir.hashCode()));

View File

@ -19,6 +19,10 @@
<annotation><appinfo><jxb:property><jxb:javadoc><![CDATA[The action to be taken by the generator as the consequence of an encountered exception. Defaults to FAIL.]]></jxb:javadoc></jxb:property></appinfo></annotation>
</element>
<element name="onUnused" type="tns:OnError" minOccurs="0" maxOccurs="1" default="LOG">
<annotation><appinfo><jxb:property><jxb:javadoc><![CDATA[The action to be taken by the generator as the consequence of unused objects being encountered. Defaults to LOG.]]></jxb:javadoc></jxb:property></appinfo></annotation>
</element>
<element name="jdbc" type="tns:Jdbc" minOccurs="0" maxOccurs="1">
<annotation><appinfo><jxb:property><jxb:javadoc><![CDATA[The JDBC configuration element contains information about how to set up the database connection used for source code generation.]]></jxb:javadoc></jxb:property></appinfo></annotation>
</element>