[jOOQ/jOOQ#15209] Support ad-hoc compilation of programmatic objects in the code generator

- Add support for GeneratorStrategy
This commit is contained in:
Lukas Eder 2023-06-12 17:26:09 +02:00
parent 21da371056
commit 97311cafda
3 changed files with 52 additions and 0 deletions

View File

@ -96,6 +96,7 @@ import org.jooq.meta.jaxb.Target;
import org.jooq.tools.JooqLogger;
import org.jooq.tools.StringUtils;
import org.jooq.tools.jdbc.JDBCUtils;
import org.jooq.tools.reflect.Reflect;
import org.jooq.util.jaxb.tools.MiniJAXB;
@ -412,6 +413,14 @@ public class GenerationTool {
g.getStrategy().setName(null);
}
}
else if (!isBlank(g.getStrategy().getJava())) {
Object o = Reflect.compile(g.getStrategy().getName(), g.getStrategy().getJava()).create().get();
if (o instanceof GeneratorStrategy s)
strategy = s;
else
throw new GeneratorException("GeneratorStrategy must implement org.jooq.codegen.GeneratorStrategy");
}
else {
Class<GeneratorStrategy> strategyClass = (Class<GeneratorStrategy>) (!isBlank(g.getStrategy().getName())
? loadClass(trim(g.getStrategy().getName()))

View File

@ -21,6 +21,7 @@ import org.jooq.util.jaxb.tools.XMLBuilder;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Strategy", propOrder = {
"name",
"java",
"matchers"
})
@SuppressWarnings({
@ -33,6 +34,8 @@ public class Strategy implements Serializable, XMLAppendable
@XmlElement(defaultValue = "org.jooq.codegen.DefaultGeneratorStrategy")
@XmlJavaTypeAdapter(StringAdapter.class)
protected String name = "org.jooq.codegen.DefaultGeneratorStrategy";
@XmlJavaTypeAdapter(StringAdapter.class)
protected String java;
protected Matchers matchers;
/**
@ -51,6 +54,22 @@ public class Strategy implements Serializable, XMLAppendable
this.name = value;
}
/**
* A self-contained, inline implementation of {@link org.jooq.codegen.GeneratorStrategy} that will be compiled and class-loaded on the fly by the code generator.
*
*/
public String getJava() {
return java;
}
/**
* A self-contained, inline implementation of {@link org.jooq.codegen.GeneratorStrategy} that will be compiled and class-loaded on the fly by the code generator.
*
*/
public void setJava(String value) {
this.java = value;
}
/**
* The matcher strategy configuration used when applying an XML-based strategy. This cannot be combined with a named strategy configuration.
*
@ -76,6 +95,15 @@ public class Strategy implements Serializable, XMLAppendable
return this;
}
/**
* A self-contained, inline implementation of {@link org.jooq.codegen.GeneratorStrategy} that will be compiled and class-loaded on the fly by the code generator.
*
*/
public Strategy withJava(String value) {
setJava(value);
return this;
}
/**
* The matcher strategy configuration used when applying an XML-based strategy. This cannot be combined with a named strategy configuration.
*
@ -88,6 +116,7 @@ public class Strategy implements Serializable, XMLAppendable
@Override
public final void appendTo(XMLBuilder builder) {
builder.append("name", name);
builder.append("java", java);
builder.append("matchers", matchers);
}
@ -119,6 +148,15 @@ public class Strategy implements Serializable, XMLAppendable
return false;
}
}
if (java == null) {
if (other.java!= null) {
return false;
}
} else {
if (!java.equals(other.java)) {
return false;
}
}
if (matchers == null) {
if (other.matchers!= null) {
return false;
@ -136,6 +174,7 @@ public class Strategy implements Serializable, XMLAppendable
final int prime = 31;
int result = 1;
result = ((prime*result)+((name == null)? 0 :name.hashCode()));
result = ((prime*result)+((java == null)? 0 :java.hashCode()));
result = ((prime*result)+((matchers == null)? 0 :matchers.hashCode()));
return result;
}

View File

@ -157,6 +157,10 @@
<element name="name" type="string" minOccurs="0" maxOccurs="1" default="org.jooq.codegen.DefaultGeneratorStrategy">
<annotation><appinfo><jxb:property><jxb:javadoc><![CDATA[The class used to provide a naming strategy for generated source code. You may override this with your custom naming strategy. This cannot be combined with a matcher configuration.]]></jxb:javadoc></jxb:property></appinfo></annotation>
</element>
<element name="java" type="string" minOccurs="0" maxOccurs="1">
<annotation><appinfo><jxb:property><jxb:javadoc><![CDATA[A self-contained, inline implementation of {@link org.jooq.codegen.GeneratorStrategy} that will be compiled and class-loaded on the fly by the code generator.]]></jxb:javadoc></jxb:property></appinfo></annotation>
</element>
<element name="matchers" type="tns:Matchers" minOccurs="0" maxOccurs="1">
<annotation><appinfo><jxb:property><jxb:javadoc><![CDATA[The matcher strategy configuration used when applying an XML-based strategy. This cannot be combined with a named strategy configuration.]]></jxb:javadoc></jxb:property></appinfo></annotation>