gpt4 book ai didi

Groovy - 扩展结构

转载 作者:行者123 更新时间:2023-12-02 06:53:36 25 4
gpt4 key购买 nike

我想扩展 StringasType 方法来处理 LocalDateTime。我知道如何重写此方法,但是我不知道应该将其放在项目结构中的何处以便在全局范围内工作 - 对于我的项目中的所有字符串。将这样的扩展名放在类路径中的任何位置是否就足够了?我知道扩展有一个特殊的约定(META-INF/services),它如何用于方法重写?

最佳答案

有关此主题的所有文档均可在 here 中找到。和here完全可以找到相关部分。

模块扩展和模块描述符

For Groovy to be able to load your extension methods, you must declare your extension helper classes. You must create a file named org.codehaus.groovy.runtime.ExtensionModule into the META-INF/services directory:

org.codehaus.groovy.runtime.ExtensionModule moduleName=Test module for specifications moduleVersion=1.0-test extensionClasses=support.MaxRetriesExtension staticExtensionClasses=support.StaticStringExtension The module descriptor requires 4 keys:

moduleName : the name of your module

moduleVersion: the version of your module. Note that version number is only used to check that you don’t load the same module in two different versions.

extensionClasses: the list of extension helper classes for instance methods. You can provide several classes, given that they are comma separated.

staticExtensionClasses: the list of extension helper classes for static methods. You can provide several classes, given that they are comma separated.

Note that it is not required for a module to define both static helpers and instance helpers, and that you may add several classes to a single module. You can also extend different classes in a single module without problem. It is even possible to use different classes in a single extension class, but it is recommended to group extension methods into classes by feature set.

模块扩展和类路径

It’s worth noting that you can’t use an extension which is compiled at the same time as code using it. That means that to use an extension, it has to be available on classpath, as compiled classes, before the code using it gets compiled. Usually, this means that you can’t have the test classes in the same source unit as the extension class itself. Since in general, test sources are separated from normal sources and executed in another step of the build, this is not an issue.

关于Groovy - 扩展结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31112044/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com