[jOOQ/jOOQ#13432] Add <visibilityModifier/> code generation option to <forcedType/>

This commit is contained in:
Lukas Eder 2022-04-07 14:56:07 +02:00
parent 59be6f5f23
commit f7e72cd17d
8 changed files with 186 additions and 25 deletions

View File

@ -347,7 +347,10 @@ abstract class AbstractGenerator implements Generator {
@Override
public void setGenerateVisibilityModifier(VisibilityModifier generateVisibilityModifier) {
this.generateVisibilityModifier = generateVisibilityModifier;
if (generateVisibilityModifier == VisibilityModifier.PRIVATE)
log.warn("Visibility", "The private visibility modifier cannot be used globally, to be applied to classes. It can only be used on <forcedType/> configurations.");
else
this.generateVisibilityModifier = generateVisibilityModifier;
}
@Override

View File

@ -179,7 +179,9 @@ import org.jooq.meta.TableDefinition;
import org.jooq.meta.TypedElementDefinition;
import org.jooq.meta.UDTDefinition;
import org.jooq.meta.UniqueKeyDefinition;
import org.jooq.meta.jaxb.ForcedType;
import org.jooq.meta.jaxb.GeneratedAnnotationType;
import org.jooq.meta.jaxb.VisibilityModifier;
// ...
// ...
// ...
@ -369,27 +371,28 @@ public class JavaGenerator extends AbstractGenerator {
}
private String visibility() {
if (visibility == null) {
switch (generateVisibilityModifier()) {
case NONE:
visibility = "";
break;
case PUBLIC:
visibility = scala ? "" : "public ";
break;
case INTERNAL:
visibility = scala ? "" : kotlin ? "internal " : "public ";
break;
case DEFAULT:
default:
visibility = scala || kotlin ? "" : "public ";
break;
}
}
if (visibility == null)
visibility = visibility(generateVisibilityModifier());
return visibility;
}
private String visibility(VisibilityModifier modifier) {
switch (modifier) {
case NONE:
return "";
case PUBLIC:
return scala ? "" : "public ";
case INTERNAL:
return scala ? "" : kotlin ? "internal " : "public ";
case PRIVATE:
return "private ";
case DEFAULT:
default:
return scala || kotlin ? "" : "public ";
}
}
@Override
public final void generate0(Database db) {
this.isoDate = Instant.now().toString();
@ -5808,12 +5811,21 @@ public class JavaGenerator extends AbstractGenerator {
final String columnVisibility =
final String columnVisibility;
visibility();
columnVisibility = visibility();
if (!printDeprecationIfUnknownType(out, columnTypeFull))
out.javadoc("The column <code>%s</code>.[[before= ][%s]]", column.getQualifiedOutputName(), list(escapeEntities(comment(column))));

View File

@ -329,6 +329,18 @@ public abstract class AbstractDatabase implements Database {
return connection;
}
public boolean commercial() {
return create().configuration().commercial();
}
public boolean commercial(Supplier<String> logMessage) {
return create().configuration().commercial(logMessage);
}
public boolean requireCommercial(Supplier<String> logMessage) {
return create().configuration().requireCommercial(logMessage);
}
@Override
public final DSLContext create() {
return create(false);
@ -1404,17 +1416,27 @@ public abstract class AbstractDatabase implements Database {
if (StringUtils.isBlank(type.getName())) {
if (StringUtils.isBlank(type.getUserType()) && StringUtils.isBlank(type.getGenerator())) {
log.warn("Bad configuration for <forcedType/>. Either <name/>, <userType/>, or <generator/> is required: " + type);
if (type.getVisibilityModifier() == null) {
log.warn("Bad configuration for <forcedType/>. Either <name/>, <userType/>, <generator/>, or <visibilityModifier/> is required: " + type);
it2.remove();
continue;
it2.remove();
continue;
}
else if (!commercial()) {
log.warn("<visibilityModifier/> is a commercial only feature. Please upgrade to the jOOQ Professional Edition or jOOQ Enterprise Edition: " + type);
it2.remove();
continue;
}
}
if (StringUtils.isBlank(type.getBinding()) &&
StringUtils.isBlank(type.getConverter()) &&
StringUtils.isBlank(type.getGenerator()) &&
type.getVisibilityModifier() == null &&
!Boolean.TRUE.equals(type.isEnumConverter()) &&
type.getLambdaConverter() == null) {
type.getLambdaConverter() == null
) {
log.warn("Bad configuration for <forcedType/>. Either <binding/>, <converter/>, <enumConverter/>, <lambdaConverter/>, or <generator/> is required: " + type);
it2.remove();

View File

@ -348,6 +348,7 @@ public abstract class AbstractTypedElementDefinition<T extends Definition>
.withBinding(forcedType.getBinding())
.withEnumConverter(forcedType.isEnumConverter())
.withLambdaConverter(forcedType.getLambdaConverter())
.withVisibilityModifier(forcedType.getVisibilityModifier())
.withGenerator(forcedType.getGenerator())
.withConverter(forcedType.getConverter())
.withName(name)

View File

@ -5,6 +5,7 @@ import java.io.Serializable;
import jakarta.xml.bind.annotation.XmlAccessType;
import jakarta.xml.bind.annotation.XmlAccessorType;
import jakarta.xml.bind.annotation.XmlElement;
import jakarta.xml.bind.annotation.XmlSchemaType;
import jakarta.xml.bind.annotation.XmlType;
import jakarta.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import org.jooq.util.jaxb.tools.StringAdapter;
@ -35,6 +36,8 @@ public class CustomType implements Serializable, XMLAppendable
protected String name;
@XmlJavaTypeAdapter(StringAdapter.class)
protected String type;
@XmlSchemaType(name = "string")
protected VisibilityModifier visibilityModifier;
@XmlJavaTypeAdapter(StringAdapter.class)
protected String generator;
@XmlJavaTypeAdapter(StringAdapter.class)
@ -80,6 +83,24 @@ public class CustomType implements Serializable, XMLAppendable
this.type = value;
}
/**
* @deprecated Use ForcedType only
*
*/
@Deprecated
public VisibilityModifier getVisibilityModifier() {
return visibilityModifier;
}
/**
* @deprecated Use ForcedType only
*
*/
@Deprecated
public void setVisibilityModifier(VisibilityModifier value) {
this.visibilityModifier = value;
}
/**
* @deprecated Use ForcedType only
*
@ -198,6 +219,16 @@ public class CustomType implements Serializable, XMLAppendable
return this;
}
/**
* @deprecated Use ForcedType only
*
*/
@Deprecated
public CustomType withVisibilityModifier(VisibilityModifier value) {
setVisibilityModifier(value);
return this;
}
/**
* @deprecated Use ForcedType only
*
@ -247,6 +278,7 @@ public class CustomType implements Serializable, XMLAppendable
public final void appendTo(XMLBuilder builder) {
builder.append("name", name);
builder.append("type", type);
builder.append("visibilityModifier", visibilityModifier);
builder.append("generator", generator);
builder.append("converter", converter);
builder.append("enumConverter", enumConverter);
@ -291,6 +323,15 @@ public class CustomType implements Serializable, XMLAppendable
return false;
}
}
if (visibilityModifier == null) {
if (other.visibilityModifier!= null) {
return false;
}
} else {
if (!visibilityModifier.equals(other.visibilityModifier)) {
return false;
}
}
if (generator == null) {
if (other.generator!= null) {
return false;
@ -345,6 +386,7 @@ public class CustomType implements Serializable, XMLAppendable
int result = 1;
result = ((prime*result)+((name == null)? 0 :name.hashCode()));
result = ((prime*result)+((type == null)? 0 :type.hashCode()));
result = ((prime*result)+((visibilityModifier == null)? 0 :visibilityModifier.hashCode()));
result = ((prime*result)+((generator == null)? 0 :generator.hashCode()));
result = ((prime*result)+((converter == null)? 0 :converter.hashCode()));
result = ((prime*result)+((enumConverter == null)? 0 :enumConverter.hashCode()));

View File

@ -36,6 +36,8 @@ public class ForcedType implements Serializable, XMLAppendable
protected String name;
@XmlJavaTypeAdapter(StringAdapter.class)
protected String userType;
@XmlSchemaType(name = "string")
protected VisibilityModifier visibilityModifier;
@XmlJavaTypeAdapter(StringAdapter.class)
protected String generator;
@XmlJavaTypeAdapter(StringAdapter.class)
@ -121,9 +123,35 @@ public class ForcedType implements Serializable, XMLAppendable
this.userType = value;
}
/**
* The visibility modifier to be used in generated code for the column that is matched by this forced type, if applicable.
* <p>
* This has no effect on matched objects that are not columns.
* <p>
* This feature is available in the commercial distribution only.
*
*/
public VisibilityModifier getVisibilityModifier() {
return visibilityModifier;
}
/**
* The visibility modifier to be used in generated code for the column that is matched by this forced type, if applicable.
* <p>
* This has no effect on matched objects that are not columns.
* <p>
* This feature is available in the commercial distribution only.
*
*/
public void setVisibilityModifier(VisibilityModifier value) {
this.visibilityModifier = value;
}
/**
* A {@link org.jooq.Generator} implementation used for client-side computed columns.
* <p>
* This has no effect on matched objects that are not columns.
* <p>
* This feature is available in the commercial distribution only.
*
*/
@ -134,6 +162,8 @@ public class ForcedType implements Serializable, XMLAppendable
/**
* A {@link org.jooq.Generator} implementation used for client-side computed columns.
* <p>
* This has no effect on matched objects that are not columns.
* <p>
* This feature is available in the commercial distribution only.
*
*/
@ -417,9 +447,24 @@ public class ForcedType implements Serializable, XMLAppendable
return this;
}
/**
* The visibility modifier to be used in generated code for the column that is matched by this forced type, if applicable.
* <p>
* This has no effect on matched objects that are not columns.
* <p>
* This feature is available in the commercial distribution only.
*
*/
public ForcedType withVisibilityModifier(VisibilityModifier value) {
setVisibilityModifier(value);
return this;
}
/**
* A {@link org.jooq.Generator} implementation used for client-side computed columns.
* <p>
* This has no effect on matched objects that are not columns.
* <p>
* This feature is available in the commercial distribution only.
*
*/
@ -562,6 +607,7 @@ public class ForcedType implements Serializable, XMLAppendable
builder.append("priority", priority);
builder.append("name", name);
builder.append("userType", userType);
builder.append("visibilityModifier", visibilityModifier);
builder.append("generator", generator);
builder.append("converter", converter);
builder.append("enumConverter", enumConverter);
@ -625,6 +671,15 @@ public class ForcedType implements Serializable, XMLAppendable
return false;
}
}
if (visibilityModifier == null) {
if (other.visibilityModifier!= null) {
return false;
}
} else {
if (!visibilityModifier.equals(other.visibilityModifier)) {
return false;
}
}
if (generator == null) {
if (other.generator!= null) {
return false;
@ -770,6 +825,7 @@ public class ForcedType implements Serializable, XMLAppendable
result = ((prime*result)+((priority == null)? 0 :priority.hashCode()));
result = ((prime*result)+((name == null)? 0 :name.hashCode()));
result = ((prime*result)+((userType == null)? 0 :userType.hashCode()));
result = ((prime*result)+((visibilityModifier == null)? 0 :visibilityModifier.hashCode()));
result = ((prime*result)+((generator == null)? 0 :generator.hashCode()));
result = ((prime*result)+((converter == null)? 0 :converter.hashCode()));
result = ((prime*result)+((enumConverter == null)? 0 :enumConverter.hashCode()));

View File

@ -16,6 +16,7 @@ import jakarta.xml.bind.annotation.XmlType;
* &lt;enumeration value="NONE"/&gt;
* &lt;enumeration value="PUBLIC"/&gt;
* &lt;enumeration value="INTERNAL"/&gt;
* &lt;enumeration value="PRIVATE"/&gt;
* &lt;/restriction&gt;
* &lt;/simpleType&gt;
* </pre>
@ -28,7 +29,8 @@ public enum VisibilityModifier {
DEFAULT,
NONE,
PUBLIC,
INTERNAL;
INTERNAL,
PRIVATE;
public String value() {
return name();

View File

@ -1339,6 +1339,18 @@ This feature is available in the commercial distribution only.]]></jxb:javadoc><
</annotation>
</element>
<element name="visibilityModifier" type="tns:VisibilityModifier" minOccurs="0" maxOccurs="1">
<annotation>
<appinfo>
<jxb:property>
<jxb:javadoc><![CDATA[@deprecated Use ForcedType only]]></jxb:javadoc>
</jxb:property>
<annox:annotate target="getter">@java.lang.Deprecated</annox:annotate>
<annox:annotate target="setter">@java.lang.Deprecated</annox:annotate>
</appinfo>
</annotation>
</element>
<element name="generator" type="string" minOccurs="0" maxOccurs="1">
<annotation>
<appinfo>
@ -1486,9 +1498,19 @@ If provided, {@link #getName()} will be ignored, and either {@link #getConverter
or {@link #getBinding()} is required]]></jxb:javadoc></jxb:property></appinfo></annotation>
</element>
<element name="visibilityModifier" type="tns:VisibilityModifier" minOccurs="0" maxOccurs="1">
<annotation><appinfo><jxb:property><jxb:javadoc><![CDATA[The visibility modifier to be used in generated code for the column that is matched by this forced type, if applicable.
<p>
This has no effect on matched objects that are not columns.
<p>
This feature is available in the commercial distribution only.]]></jxb:javadoc></jxb:property></appinfo></annotation>
</element>
<element name="generator" type="string" minOccurs="0" maxOccurs="1">
<annotation><appinfo><jxb:property><jxb:javadoc><![CDATA[A {@link org.jooq.Generator} implementation used for client-side computed columns.
<p>
This has no effect on matched objects that are not columns.
<p>
This feature is available in the commercial distribution only.]]></jxb:javadoc></jxb:property></appinfo></annotation>
</element>
@ -2121,6 +2143,7 @@ e.g. org.jooq.generated.schema1, org.jooq.generated.schema2]]></jxb:javadoc></jx
<enumeration value="NONE"/>
<enumeration value="PUBLIC"/>
<enumeration value="INTERNAL"/>
<enumeration value="PRIVATE"/>
</restriction>
</simpleType>