checker.src.org.checkerframework.checker.nullness.NullnessAnnotatedTypeFormatter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of checker Show documentation
Show all versions of checker Show documentation
The Checker Framework enhances Java’s type system to
make it more powerful and useful. This lets software developers
detect and prevent errors in their Java programs.
The Checker Framework includes compiler plug-ins ("checkers")
that find bugs or verify their absence. It also permits you to
write your own compiler plug-ins.
package org.checkerframework.checker.nullness;
import org.checkerframework.framework.type.AnnotatedTypeMirror;
import org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedNullType;
import org.checkerframework.framework.type.DefaultAnnotatedTypeFormatter;
import org.checkerframework.framework.util.AnnotationFormatter;
import org.checkerframework.framework.util.DefaultAnnotationFormatter;
import java.util.Set;
/**
* A DefaultAnnotatedTypeFormatter that prints null literals without their annotations.
*/
public class NullnessAnnotatedTypeFormatter extends DefaultAnnotatedTypeFormatter {
public NullnessAnnotatedTypeFormatter(boolean printVerboseGenerics, boolean printInvisibleQualifiers) {
super(new NullnessFormattingVisitor(new DefaultAnnotationFormatter(), printVerboseGenerics, printInvisibleQualifiers));
}
protected static class NullnessFormattingVisitor extends FormattingVisitor {
public NullnessFormattingVisitor(AnnotationFormatter annoFormatter, boolean printVerboseGenerics,
boolean defaultInvisiblesSetting) {
super(annoFormatter, printVerboseGenerics, defaultInvisiblesSetting);
}
@Override
public String visitNull(AnnotatedNullType type, Set visiting) {
// The null literal will be understood as nullable by readers, therefore omit the annotations
// Note: The visitTypeVariable will still print lower bounds with Null kind as "Void"
if (!currentPrintInvisibleSetting) {
return "null";
} // else
return super.visitNull(type, visiting);
}
}
}