Skip to content

Commit b1ff230

Browse files
authored
Change Nullness Checker new class error
1 parent 3eac89a commit b1ff230

File tree

3 files changed

+12
-33
lines changed

3 files changed

+12
-33
lines changed

checker/src/main/java/org/checkerframework/checker/nullness/NullnessVisitor.java

Lines changed: 10 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,7 @@
3333
import com.sun.source.tree.VariableTree;
3434
import com.sun.source.tree.WhileLoopTree;
3535
import com.sun.source.util.TreePath;
36-
import java.lang.annotation.Annotation;
3736
import java.util.List;
38-
import java.util.Set;
3937
import javax.annotation.processing.ProcessingEnvironment;
4038
import javax.lang.model.element.AnnotationMirror;
4139
import javax.lang.model.element.Element;
@@ -55,7 +53,6 @@
5553
import org.checkerframework.framework.type.AnnotatedTypeFactory;
5654
import org.checkerframework.framework.type.AnnotatedTypeMirror;
5755
import org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedArrayType;
58-
import org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType;
5956
import org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedExecutableType;
6057
import org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedPrimitiveType;
6158
import org.checkerframework.javacutil.AnnotationMirrorSet;
@@ -147,16 +144,6 @@ public boolean isValidUse(AnnotatedPrimitiveType type, Tree tree) {
147144
return true;
148145
}
149146

150-
private boolean containsSameByName(
151-
Set<Class<? extends Annotation>> quals, AnnotationMirror anno) {
152-
for (Class<? extends Annotation> q : quals) {
153-
if (atypeFactory.areSameByClass(anno, q)) {
154-
return true;
155-
}
156-
}
157-
return false;
158-
}
159-
160147
@Override
161148
protected boolean commonAssignmentCheck(
162149
Tree varTree,
@@ -751,27 +738,19 @@ public Void visitNewClass(NewClassTree tree, Void p) {
751738
if (enclosingExpr != null) {
752739
checkForNullability(enclosingExpr, DEREFERENCE_OF_NULLABLE);
753740
}
754-
AnnotatedDeclaredType type = atypeFactory.getAnnotatedType(tree);
755-
ExpressionTree identifier = tree.getIdentifier();
756-
if (identifier instanceof AnnotatedTypeTree) {
757-
AnnotatedTypeTree t = (AnnotatedTypeTree) identifier;
758-
for (AnnotationMirror a : atypeFactory.getAnnotatedType(t).getPrimaryAnnotations()) {
759-
// is this an annotation of the nullness checker?
760-
boolean nullnessCheckerAnno = containsSameByName(atypeFactory.getNullnessAnnotations(), a);
761-
if (nullnessCheckerAnno && !AnnotationUtils.areSame(NONNULL, a)) {
762-
// The type is not non-null => warning
763-
checker.reportWarning(tree, "new.class", type.getPrimaryAnnotations());
764-
// Note that other consistency checks are made by isValid.
741+
742+
AnnotationMirrorSet explicitAnnos = atypeFactory.getExplicitNewClassAnnos(tree);
743+
AnnotationMirror nullnessAnno =
744+
qualHierarchy.findAnnotationInSameHierarchy(explicitAnnos, NONNULL);
745+
if (nullnessAnno != null) {
746+
if (atypeFactory.areSameByClass(nullnessAnno, NonNull.class)) {
747+
if (warnRedundantAnnotations) {
748+
checker.reportWarning(tree, "redundant.anno", NONNULL);
765749
}
766-
}
767-
if (t.toString().contains("@PolyNull")) {
768-
// TODO: this is a hack, but PolyNull gets substituted
769-
// afterwards
770-
checker.reportWarning(tree, "new.class", type.getPrimaryAnnotations());
750+
} else {
751+
checker.reportWarning(tree, "new.class");
771752
}
772753
}
773-
// TODO: It might be nicer to introduce a framework-level
774-
// isValidNewClassType or some such.
775754
return super.visitNewClass(tree, p);
776755
}
777756

checker/src/main/java/org/checkerframework/checker/nullness/messages.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ nulltest.redundant=redundant test against null; "%s" is non-null
2222
instanceof.nullable=instanceof is only true for a non-null expression
2323
instanceof.nonnull.redundant=redundant @NonNull annotation on instanceof
2424
new.array=annotations %s may not be applied as component type for array "%s"
25-
new.class=the annotations %s do not need be applied in object creations
25+
new.class=do not write nullness annotations on a constructor invocation; the result is always non-null
2626
nullness.on.constructor=do not write nullness annotations on a constructor, whose result is always non-null
2727
nullness.on.enum=do not write nullness annotations on an enum constant, which is always non-null
2828
nullness.on.exception.parameter=do not write nullness annotations on an exception parameter, which is always non-null

framework/src/main/java/org/checkerframework/common/basetype/BaseTypeVisitor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ public class BaseTypeVisitor<Factory extends GenericAnnotatedTypeFactory<?, ?, ?
271271
private final boolean checkCastElementType;
272272

273273
/** True if "-AwarnRedundantAnnotations" was passed on the command line */
274-
private final boolean warnRedundantAnnotations;
274+
protected final boolean warnRedundantAnnotations;
275275

276276
/** The tree of the enclosing method that is currently being visited, if any. */
277277
protected @Nullable MethodTree methodTree = null;

0 commit comments

Comments
 (0)