我正在使用注释处理来生成一些类...我有两个模块,处理器本身和使用它的“客户端”模块。我想通过客户端将参数传递给处理器,这是我喜欢的
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
<generatedSourcesDirectory>${project.build.directory}/generated-sources/</generatedSourcesDirectory>
<annotationProcessors>
<annotationProcessor>org.rapster.xxx.xxx.xxComponentProcessor</annotationProcessor>
</annotationProcessors>
<compilerArgs>
<arg>-Awidget=something</arg>
</compilerArgs>
</configuration>
</plugin>
</plugins>
</build>
Run Code Online (Sandbox Code Playgroud)
以及如何在处理器端检索这个参数?
为了让增量构建支持在 gradle 中正确运行,需要在每个(自定义)任务中定义“输入”和“输出”。
这是一种非常简洁的 gradle 方法来检查任务是否可以因为它是最新的而被跳过。样本:
task myTaskA {
def someOutputDir = file("$buildDir/gen")
// define task outputs (input not required here)
outputs.dir someOutputDir
doLast{
// generate something into the defined output directory
new File(someOutputDir, "dummy.txt").text = "Gradle sample"
}
}
task myTaskB {
// Input of this task is dependent on the output of myTaskA
inputs.files myTaskA
doLast{
// do something
}
}
Run Code Online (Sandbox Code Playgroud)
是的,这非常好,另外好处是,我们不需要在任务“myTaskB”中声明显式依赖指令(dependsOn)。
dependsOn myTaskA
Run Code Online (Sandbox Code Playgroud)
该指令不是必需的,因为我们有由输入声明定义的隐式依赖项。
我认为在自定义任务中始终提供输入/输出定义以支持增量构建是一种很好的风格。
但是:这也意味着我们可以完全忽略 dependentOn。
SO:我们什么时候应该dependsOn选择inputs/outputs?
也许如果没有输入或输出。是的,但是还有其他可以想到的用例吗?我一直在使用dependsOn,而这对我来说现在已经过时了。你怎么认为?
JSR 308建议向Java添加类型注释.在批准之后,程序员将能够在当前允许Java类型的任何地方添加注释.这不仅包括方法/字段/本地/参数装饰,还包括构造函数调用,类型转换以及最奇怪的实例检查.所述检查器框架使用JSR 308来实现类型限定符像@NonNull上的对象类型,或@Regex对字符串.
现在,Checkers所做的就是静态分析你的代码.那是所有编译时间检查.没关系.但我想要的是一种可以在运行时进行检查的机制.你可以声明:
@Regex String p1 = "[a-z]+";
@Regex String p1 = "[a-z)+"; // compile time error from annotation processor
Run Code Online (Sandbox Code Playgroud)
我也可以写:
if (x instanceof @Regex String) ...
Run Code Online (Sandbox Code Playgroud)
但这没有什么不同x instanceof String,没有执行运行时检查.我需要一个编译时注释处理器或运行时字节码操纵器,它允许我在instanceof检查上运行任意代码并返回一个布尔值.这可能与Java有关吗?
java code-generation annotations bytecode-manipulation annotation-processing
Java 8类型注释(JSR 308)允许类型检查器执行静态代码分析。例如,Checker Framework可以通过注释检查可能的无效性@NonNull。
各种项目定义了自己的NonNull注释,例如:
org.checkerframework.checker.nullness.qual.NonNulledu.umd.cs.findbugs.annotations.NonNulljavax.annotation.Nonnulljavax.validation.constraints.NotNulllombok.NonNullorg.eclipse.jdt.annotation.NonNull对于此类注释,我希望它@interface具有@Retention(RetentionPolicy.CLASS),因为在运行时通常不需要它们。最重要的是,代码在相应的库上没有任何运行时依赖项。
虽然org.eclipse.jdt.annotation.NonNull采用这种方法,但其他大多数NonNull注释(如javax.annotation.Nonnull(JSR 305)及其org.checkerframework.checker.nullness.qual.NonNull本身)也具有@Retention(RetentionPolicy.RUNTIME)。RetentionPolicy.RUNTIME这些注释中是否有任何特殊原因?
澄清:Checker Framework支持注释中的注释,以实现向后兼容。但是,在Java 8中使用它们只是为了避免运行时依赖性,这似乎是一个肮脏的技巧。