英文:
Implement interface in POJO if contains field
问题
我使用 jOOQ 生成 Java 类。我想让 POJO 实现一个仅包含字段 getter 方法的接口。显然,我只需要在具有这些字段的 POJO 中使用这个接口。
我需要检查类或表以查看字段是否存在,如果字段存在,则在 POJO 中实现接口。
在 DefaultGeneratorStrategy
中覆盖 getJavaClassImplements
不起作用,因为它会为所有类添加接口。
英文:
I generate java classes with jOOQ. And I want to make POJOs implement an interface which contains only getters for fields. Obviously, I need this interface only in POJOs with such fields.
I need check class or table for field and, if field extists, implement interface in pojo.
Overriding getJavaClassImplements
in DefaultGeneratorStrategy
does not help because it adds the interface for all classes.
答案1
得分: 1
配置生成器策略
你似乎正在实施程序化生成器策略,我想告诉你还有一个配置策略的选项,可能看起来像这样:
<configuration>
<generator>
<strategy>
<matchers>
<tables>
<table>
<expression>MY_TABLE</expression>
<pojoImplements>com.example.MyOptionalCustomInterface</pojoImplements>
</table>
</tables>
...
请参考手册以获取更多详情。要仅应用于包含特定字段的表,只需更新 <expression>
中的正则表达式以匹配那些表。
<expression>MY_TABLE1|MY_TABLE2</expression>
当然,这个表达式必须与架构保持同步。
程序化生成器策略
就像配置策略一样,你需要限制自己只应用于生成的 POJOs。为此,请注意手册中的说明:
/**
* ...
* 注意,大多数方法还会接收一个“Mode”对象,用于告诉您 TableDefinition 是作为 Table、Record、POJO 等进行渲染的。根据这些信息,您可以仅为 TableRecords 添加后缀,而不是为 Tables 添加后缀
* ...
*/
所以,你需要做类似这样的事情:
@Override
public List<String> getJavaClassImplements(Definition definition, Mode mode) {
if (mode == Mode.POJO)
return Arrays.asList("com.example.MyOptionalCustomInterface");
else
return Collections.emptyList();
}
如果要确保此接口仅添加到满足某些条件的表中,可以这样做:
@Override
public List<String> getJavaClassImplements(Definition definition, Mode mode) {
if (mode == Mode.POJO
&& definition instanceof TableDefinition t
&& t.getColumns().stream().anyMatch(c -> conditionHere(c)))
return Arrays.asList("com.example.MyOptionalCustomInterface");
else
return Collections.emptyList();
}
英文:
Configurative generator strategy
You seem to be implementing a programmatic generator strategy, just to let you know that there's also an option of a configurative strategy, which could look like this:
<configuration>
<generator>
<strategy>
<matchers>
<tables>
<table>
<expression>MY_TABLE</expression>
<pojoImplements>com.example.MyOptionalCustomInterface</pojoImplements>
</table>
</tables>
...
Please refer to the manual for more details. In order to apply this only to tables containing certain fields, just update the regular expression in <expression>
to match those tables.
<expression>MY_TABLE1|MY_TABLE2</expression>
This expression has to be updated to be in sync with the schema, of course.
Programmatic genenerator strategy
Just like with the configurative strategy, you'll have to limit yourself to applying your logic only to generated POJOs. For this, notice the remark in the manual:
/**
* ...
* Beware that most methods also receive a "Mode" object, to tell you whether a
* TableDefinition is being rendered as a Table, Record, POJO, etc. Depending on
* that information, you can add a suffix only for TableRecords, not for Tables
* ...
*/
So, you'll have to do something like this:
@Override
public List<String> getJavaClassImplements(Definition definition, Mode mode) {
if (mode == Mode.POJO)
return Arrays.asList("com.example.MyOptionalCustomInterface");
else
return Collections.emptyList();
}
If you want to make sure that this inteface is only added to tables that match certain conditions, do this, instead:
@Override
public List<String> getJavaClassImplements(Definition definition, Mode mode) {
if (mode == Mode.POJO
&& definition instanceof TableDefinition t
&& t.getColumns().stream().anyMatch(c -> conditionHere(c)))
return Arrays.asList("com.example.MyOptionalCustomInterface");
else
return Collections.emptyList();
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论