我已经开始注释我的程序,编写空值注释。按照指导方针,我注释了我的一个类并将其标记为@AnnotatedFor({"nullness"})
. 该程序正在Java 7平台下编译,使用Oracle JDK 1.7.u21(由于一些技术限制,无法切换到JDK 8)。该类使用了一堆未注释的第三方(Google Guava)和 JDK 库方法,因此我在 Maven 编译器插件参数中包含该选项-AskipUses=...
,-AuseSafeDefaultsForUnnanotatedSourceCode
如下所示:
<properties>
<project.checker.annotatedJdk>${org.checkerframework:jdk7:jar}</project.checker.annotatedJdk>
<project.checker.typeAnnotationsJavac>${org.checkerframework:compiler:jar}</project.checker.typeAnnotationsJavac>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.8</version>
<executions>
<execution>
<goals>
<goal>properties</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<fork>true</fork>
<annotationProcessors>
<annotationProcessor>org.checkerframework.checker.nullness.NullnessChecker</annotationProcessor>
</annotationProcessors>
<compilerArgs>
<arg>-Xbootclasspath/p:${project.checker.annotatedJdk}</arg>
<arg>-J-Xbootclasspath/p:${project.checker.typeAnnotationsJavac}</arg>
<arg>-AuseSafeDefaultsForUnannotatedSourceCode</arg>
<arg>-AskipUses=com\.google\.common\.|java\.math</arg>
</compilerArgs>
</configuration>
</plugin>
</plugins>
</build>
类的源代码:
package pkg;
import java.util.*;
import java.math.BigDecimal;
import com.google.common.base.*;
import com.google.common.collect.Ordering;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.checkerframework.framework.qual.AnnotatedFor;
@AnnotatedFor({"nullness"})
public class Indicator implements Comparable<Indicator> {
private static final BigDecimal DEFAULT_SORT_ORDER;
private static final Ordering<Indicator> ORDERING;
public static enum IndicatorComparator implements Comparator<Indicator> {
INSTANCE;
@Override
public final int compare(Indicator left, Indicator right) {
/* nulls-last */
if (left.sortOrder != null && right.sortOrder != null) {
return left.sortOrder.compareTo(right.sortOrder);
} else if (left.sortOrder != null && right.sortOrder == null) {
return -1;
} else if (left.sortOrder == null && right.sortOrder != null) {
return 1;
} else {
return 0;
}
}
}
static {
DEFAULT_SORT_ORDER = BigDecimal.valueOf(0.0);
ORDERING = Ordering.from(IndicatorComparator.INSTANCE).nullsLast();
}
private final int id;
private @Nullable String code;
private @Nullable BigDecimal sortOrder;
private boolean isGroup;
private Indicator(Builder builder) {
id = builder.id;
code = builder.code;
sortOrder = builder.sortOrder;
isGroup = builder.isGroup;
}
/* equals and hashcode are not overriden by purpose -- the default version will do */
@Override
public int compareTo(final Indicator other) {
return ORDERING.compare(this, other);
}
@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("id", id)
.add("code", code)
.add("sortOrder", sortOrder)
.add("isGroup", isGroup)
.toString();
}
public int getId() {
return id;
}
public Optional<String> getCode() {
return Optional.fromNullable(code);
}
public Optional<BigDecimal> getSortOrder() {
return Optional.fromNullable(sortOrder);
}
public boolean isGroup() {
return isGroup;
}
public void setCode(@Nullable String code) {
this.code = code;
}
public void setSortOrder(@Nullable BigDecimal sortOrder) {
this.sortOrder = sortOrder;
}
public void setGroup(boolean group) {
this.isGroup = group;
}
public static class Builder {
private int id;
private @Nullable String code;
private @Nullable BigDecimal sortOrder;
private boolean isGroup;
public Builder() {
sortOrder = DEFAULT_SORT_ORDER;
}
public Builder id(int id) {
this.id = id;
return this;
}
public Builder code(@Nullable String code) {
this.code = code;
return this;
}
public Builder sortOrder(@Nullable BigDecimal sortOrder) {
this.sortOrder = sortOrder;
return this;
}
public Builder isGroup(boolean isGroup) {
this.isGroup = isGroup;
return this;
}
public Indicator build() {
return new Indicator(this);
}
}
}
当我构建项目时,检查器似乎忽略了“跳过”选项,因为我最终遇到了类似的错误
COMPILATION ERROR :
-------------------------------------------------------------
pkg/Indicator.java:[38,48] error: [argument.type.incompatible] incompatible types in argument.
found : @UnknownKeyFor double
required: @KeyForBottom double
将我们带到第 38 行:
DEFAULT_SORT_ORDER = BigDecimal.valueOf(0.0);
或 39:
pkg/Indicator.java:[39,52] error: [argument.type.incompatible] incompatible types in argument.
found : IndicatorComparator
required: Comparator<Indicator>
或 38 再次:
pkg/Indicator.java:[38,47] error: [assignment.type.incompatible] incompatible types in assignment.
found : @UnknownInitialization @Nullable BigDecimal
required: @Initialized @NonNull BigDecimal
-AskipUses=
依此类推——日志中充满了各种检查器错误,尽管我认为我通过使用该选项摆脱了它们。
难道我做错了什么?
UPD 07.12.2015:附上课程的完整源代码。UPD 14.12.2015:更新了源代码,因此现在重现该案例要容易得多。