没有太多补充,整个问题在标题中.
考虑Spock规范中使用的这两个Foo类实例.
@Shared Foo foo1 = new Foo()
static Foo foo2 = new Foo()
Run Code Online (Sandbox Code Playgroud)
总的来说,我知道@Shared
注释背后的想法,但我想最好使用语言功能,在这种情况下将是static
字段.
是否有任何特定的案例,其中一个人应该优先于另一个,或者它是一个品味问题?
我刚刚安装了管道插件(在Jenkins 2.1上).当我创建新作业时,缺少" 源代码管理"选项卡(以及其他一些选项).根据描述管道功能的文章,它应该如下所示:
丢失的选项卡在哪里,尤其是源代码管理?它只是一些缺少配置/插件或错误?
我在Jenkins 2.1上
我知道,因为Groovy 2.0有静态编译的注释.然而,偶然省略这样的注释并且仍然遇到麻烦很容易.
是否有任何方法可以实现相反的编译器行为,例如默认情况下编译静态所有项目文件,并编译仅用于某种类型@CompileDynamic注释的目的选择的动态文件?
让我们考虑两个Grails域示例类.
第一节课:
class Person {
String name
Integer counter = 0
static transients = ['counter']
}
Run Code Online (Sandbox Code Playgroud)
第二课:
class Vehicle {
String name
transient Integer counter = 0
}
Run Code Online (Sandbox Code Playgroud)
类Person和Vehicle之间的Integer计数器字段的GORM持久性或域类行为是否会有任何差异?
编辑:我知道Person类是Grails docs引用的好方法.但是我更喜欢Vehicle类的方式,因为在阅读代码时它似乎更容易被忽视.
我知道有QAPlug - Checkstyle,我正在尝试使用它.
在菜单Project Settings - > QAPlug - > Checkstyle中,我也设置了Xml配置文件和Jar文件.然后,当按下Check loaded rules按钮时会弹出这样的消息:
这是一个非常好的插件,它在XML文件中找到了规则.为什么不加载它们呢?
我正在使用的Checkstyle配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE module PUBLIC "-//Puppy Crawl//DTD Check Configuration 1.3//EN" "http://www.puppycrawl.com/dtds/configuration_1_3.dtd">
<module name="Checker">
<property name="severity" value="error"/>
<module name="TreeWalker">
<property name="cacheFile" value="build/checkstyle.cache"/>
<module name="FileContentsHolder"/>
<module name="RegexpSinglelineJava">
<property name="format" value="\s+$"/>
<property name="message" value="Line has trailing spaces."/>
<property name="ignoreComments" value="true"/>
</module>
<module name="RegexpSinglelineJava">
<property name="format" value="^\t* +\t*\S"/>
<property name="message"
value="Line has leading space characters; indentation should be performed with tabs …
Run Code Online (Sandbox Code Playgroud) 在Spock规范中,期望的任何行:或者然后: block被评估并断言为boolean
,除非它具有返回类型的签名void
.
我注意到void
在继承的任何类Navigator
(例如Page
或Module
类)上声明的方法有些奇怪.
假设我们有这样的例子:
class NavigationSpec extends GebSpec {
def 'Collections page has a right header'() {
when:
to CollectionsPage
then:
hasHeaderText('Collections')
}
}
Run Code Online (Sandbox Code Playgroud)
该hasHeaderText()
方法在CollectionsPage
类中定义如下:
class CollectionsPage extends Page {
static url = 'movies/collections'
void hasHeaderText(String expectedText) {
assert true
}
}
Run Code Online (Sandbox Code Playgroud)
故意我只是true
在那里断言所以它永远不会失败.即使它失败并出现错误:
Condition not satisfied:
hasHeaderText('Collections')
|
null
Run Code Online (Sandbox Code Playgroud)
void
方法调用结果的评估方式和原因是null
什么?
我知道如何"修复它".将方法返回类型声明为return boolean
并返回就足够了true …
我知道@ClosureParams
注解。它似乎仅涵盖更复杂的用例。我正在寻找像这里所描述的注解关闭部分。与以下代码段相似:
void doSomething(MyType src, @ClosureParams(MyType) Closure cl) { ... }
Run Code Online (Sandbox Code Playgroud)
不幸的是,此示例不再能与最新的groovy版本一起编译(目前我在2.5.8上)。我知道我可以做到:
void doSomething(MyType src, @ClosureParams(FirstParam) Closure cl) { ... }
Run Code Online (Sandbox Code Playgroud)
我的用例除了闭包本身外没有其他参数:
void doSomething(@ClosureParams(/* how? */) Closure cl) { ... }
Run Code Online (Sandbox Code Playgroud)
我可以像这样修改它:
void doSomething(@ClosureParams(SecondParam) Closure cl, MyType ignore = null) { ... }
Run Code Online (Sandbox Code Playgroud)
距离还很远,不是吗?
我也可以去:
void doSomething(@ClosureParams(value = SimpleType, options = ['com.somepackage.MyType']) Closure cl) { ... }
Run Code Online (Sandbox Code Playgroud)
它不仅丑陋且嘈杂,而且将类型指定为字符串会阻止某些IDE功能正常工作。例如MyType
,此处不会选择refactor-rename或搜索用法。
我猜,没有任何更干净的方法可以实现此目的,因此可以将类型指定为不是字符串的类型,并且没有多余的不必要参数,是吗?
像CédricChampeau最初在上面链接的博客文章中所发布的东西是理想的。在我的情况下看起来像:
void doSomething(@ClosureParams(MyType) Closure cl) { ... }
Run Code Online (Sandbox Code Playgroud) groovy ×4
spock ×2
annotations ×1
checkstyle ×1
closures ×1
compilation ×1
geb ×1
grails ×1
grails-orm ×1
ide ×1
jenkins ×1
persistence ×1
static ×1
transient ×1
unit-testing ×1