Skip to content

Commit

Permalink
Odds and ends related to defaulting.
Browse files Browse the repository at this point in the history
  • Loading branch information
cpovirk committed Oct 9, 2020
1 parent 8e46bea commit 89725d4
Showing 1 changed file with 41 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import static java.util.Arrays.asList;
import static javax.lang.model.element.ElementKind.PACKAGE;
import static javax.lang.model.type.TypeKind.WILDCARD;
import static org.checkerframework.framework.qual.TypeUseLocation.OTHERWISE;
import static org.checkerframework.javacutil.AnnotationUtils.areSame;

Expand Down Expand Up @@ -46,6 +47,7 @@
import org.checkerframework.framework.type.StructuralEqualityComparer;
import org.checkerframework.framework.type.StructuralEqualityVisitHistory;
import org.checkerframework.framework.type.TypeVariableSubstitutor;
import org.checkerframework.framework.type.typeannotator.TypeAnnotator;
import org.checkerframework.framework.type.visitor.AnnotatedTypeScanner;
import org.checkerframework.framework.util.AnnotationFormatter;
import org.checkerframework.framework.util.DefaultAnnotationFormatter;
Expand Down Expand Up @@ -241,6 +243,12 @@ protected AnnotatedTypeMirror substituteTypeVariable(AnnotatedTypeMirror argumen
}
}

@Override
public AnnotatedDeclaredType getSelfType(Tree tree) {
AnnotatedDeclaredType superResult = super.getSelfType(tree);
return superResult == null ? null : withNoAdditionalNullness(superResult);
}

@Override
protected QualifierDefaults createQualifierDefaults() {
return new NullSpecQualifierDefaults(elements, this);
Expand Down Expand Up @@ -336,6 +344,23 @@ protected boolean shouldBeAnnotated(AnnotatedTypeMirror type,
// TODO(cpovirk): Should I override applyConservativeDefaults to always return false?
}

@Override
protected void addComputedTypeAnnotations(Tree tree, AnnotatedTypeMirror type,
boolean iUseFlow) {
// TODO(cpovirk): This helps, but why?
super.addComputedTypeAnnotations(tree, type, iUseFlow && type.getKind() != WILDCARD);
}

@Override
protected TypeAnnotator createTypeAnnotator() {
/*
* Override to do nothing. The supermethod adds the top type (@Nullable/unionNull) to the
* bound of unbounded wildcards, but we want the ability to sometimes add
* @NullnessUnspecified/codeNotNullnessAware instead.
*/
return new TypeAnnotator(this) {};
}

@Override
protected AnnotationFormatter createAnnotationFormatter() {
return new DefaultAnnotationFormatter() {
Expand All @@ -361,4 +386,20 @@ protected AnnotatedTypeFormatter createAnnotatedTypeFormatter() {
// TODO(cpovirk): Permit configuration of these booleans?
/*printVerboseGenerics=*/ false, /*defaultPrintInvisibleAnnos=*/ false);
}

@SuppressWarnings("unchecked") // safety guaranteed by API docs
private <T extends AnnotatedTypeMirror> T withNoAdditionalNullness(T type) {
// Remove the annotation from the *root* type, but preserve other annotations.
type = (T) type.deepCopy(/*copyAnnotations=*/ true);
type.replaceAnnotation(noAdditionalNullness);
return type;
}

@SuppressWarnings("unchecked") // safety guaranteed by API docs
private <T extends AnnotatedTypeMirror> T withUnionNull(T type) {
// Remove the annotation from the *root* type, but preserve other annotations.
type = (T) type.deepCopy(/*copyAnnotations=*/ true);
type.replaceAnnotation(unionNull);
return type;
}
}

0 comments on commit 89725d4

Please sign in to comment.