groovy @CompileStatic是否暗示@TypeChecked? [英] Does groovy @CompileStatic imply @TypeChecked?
问题描述
我一直在将 @TypeChecked
和 @CompileStatic
都添加到常规代码中.
I have been adding both @TypeChecked
and @CompileStatic
to groovy code.
编译器接受这一点,但这是否多余?TypeChecked是否添加了CompileStatic不添加的任何内容?该文档似乎暗示CompileStatic是一个超集,但请不要直言不讳.我想在偶尔禁用严格的类型检查的同时进行静态编译,以使groovy自动进行类型转换,同时仍能捕获拼写错误的引用或缺少的方法.
The compiler accepts this, but is it redundant? Does TypeChecked add anything that CompileStatic does not? The docs seems to imply that CompileStatic is a superset, but stop short of saying so outright. I would like to compile statically while occasionally disabling strict type checking, to let groovy auto-typecast while still catching misspelled references or missing methods.
一个例子:
@TypeChecked
@CompileStatic
class MyClass {
void simple() {
// compiles fine statically
}
@TypeChecked(SKIP)
void complicated() {
// would require too many ugly typecasts
}
@CompileDynamic
void evenworse() {
// need time to untangle this
}
}
我阅读了 @TypeChecked与@CompileStatic之间的区别,但没有给出答案具体问题.它建议TypeCast允许一些其他检查,但我不知道默认情况下是否进行其他检查.
I read Difference between @TypeChecked and @CompileStatic and it did not answer this specific question. It suggested that TypeCast allows some additional checks, but I don't know if it does additional checks by default.
推荐答案
我会说是的, CompileStatic
表示 TypeChecked
.查看注释接口 TypeChecked
和 CompileStatic
的常规来源,我们看到:
I would say yes, CompileStatic
implies TypeChecked
. Looking into the groovy source for the annotation interfaces TypeChecked
and CompileStatic
, we see:
// TypeChecked.java
@GroovyASTTransformationClass("org.codehaus.groovy.transform.StaticTypesTransformation")
public @interface TypeChecked {
和
// CompileStatic.java
@GroovyASTTransformationClass("org.codehaus.groovy.transform.sc.StaticCompileTransformation")
public @interface CompileStatic {
...
告诉我们进行实际AST转换(注释的工作)的类是 StaticCompileTransformation
和 StaticTypesTransformation
.
which tells us that the classes doing the actual AST transformations (the work of the annotations) are StaticCompileTransformation
and StaticTypesTransformation
.
查看我们得到的代码:
// Transformation for TypeChecked
public class StaticTypesTransformation implements ASTTransformation, CompilationUnitAware {
和
// Transformation for CompileStatic
public class StaticCompileTransformation extends StaticTypesTransformation {
即 CompileStatic
的转换扩展了 TypeChecked
的转换,因此,看来CompileStatic的行为确实是TypeChecked行为的超集.
i.e. the transformation for CompileStatic
extends the transformation for TypeChecked
, thus it would seem that the behavior of CompileStatic is indeed a superset of the behavior of TypeChecked.
<<编辑>>
<< edit >>
深入研究,我们看到转换类使用访问者模式,并且它们分别具有以下方法:
Digging even deeper, we see that the transformation classes use a visitor pattern and that they have respectively the following methods:
// StaticTypesTransformation, transformation for TypeChecked
protected StaticTypeCheckingVisitor newVisitor(SourceUnit unit, ClassNode node) {
return new StaticTypeCheckingVisitor(unit, node);
}
和
// StaticCompileTransformation, transformation for CompileStatic
protected StaticTypeCheckingVisitor newVisitor(final SourceUnit unit, final ClassNode node) {
return new StaticCompilationVisitor(unit, node);
}
,将为每种情况返回一个自定义的访问者类.
which return a custom visitor class for each of the cases.
在 StaticCompilationVisitor
上,我们看到:
public class StaticCompilationVisitor extends StaticTypeCheckingVisitor {
...
public void visitClass(final ClassNode node) {
...
super.visitClass(node);
...
}
...
}
换句话说, CompileStatic
的访问者类扩展了 TypeChecked
的访问者类,此外还扩展了 visitClass
方法(实际工作)在 CompileStatic
访问者调用 super.visitClass(node)
中,其中 super
是 TypeChecked
的访问者类.
in other words, the visitor class for CompileStatic
extends the visitor class for TypeChecked
and in addition, the visitClass
method (which does the actual work) in the CompileStatic
visitor calls super.visitClass(node)
where super
is the visitor class for TypeChecked
.
我认为这或多或少证明了这一点.实际上, CompileStatic
的行为是 TypeChecked
行为的超集.
I think this more or less proves it. The behavior of CompileStatic
is in fact a superset of the behavior of TypeChecked
.
这篇关于groovy @CompileStatic是否暗示@TypeChecked?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!