Skip to content

Commit

Permalink
Clean up getTypeFactoryOfSubchecker. (#4729)
Browse files Browse the repository at this point in the history
  • Loading branch information
smillst authored Jun 17, 2021
1 parent 8ae3a75 commit fe32414
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ public NullnessTransfer(NullnessAnalysis analysis) {
} else {
// It is error-prone to put a type factory in a field. It is OK here because
// keyForTypeFactory is used only to call methods isMapGet() and isKeyForMap().
this.keyForTypeFactory = checker.getTypeFactoryOfSubchecker(KeyForSubchecker.class);
this.keyForTypeFactory =
nullnessTypeFactory.getTypeFactoryOfSubchecker(KeyForSubchecker.class);
}

NONNULL = AnnotationBuilder.fromClass(elements, NonNull.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -402,18 +402,17 @@ public <T extends BaseTypeChecker> T getSubchecker(Class<T> checkerClass) {
* Returns the type factory used by a subchecker. Returns null if no matching subchecker was found
* or if the type factory is null. The caller must know the exact checker class to request.
*
* @param checkerClass the class of the subchecker
* <p>Because the visitor state is copied, call this method each time a subfactory is needed
* rather than store the returned subfactory in a field.
*
* @param subCheckerClass the class of the subchecker
* @param <T> the type of {@code subCheckerClass}'s {@link AnnotatedTypeFactory}
* @return the type factory of the requested subchecker or null if not found
*/
@SuppressWarnings({"unchecked", "TypeParameterUnusedInFormals"}) // Intentional abuse
public <T extends GenericAnnotatedTypeFactory<?, ?, ?, ?>, U extends BaseTypeChecker>
T getTypeFactoryOfSubchecker(Class<U> checkerClass) {
BaseTypeChecker checker = getSubchecker(checkerClass);
if (checker != null) {
return (T) checker.getTypeFactory();
}

return null;
@SuppressWarnings("TypeParameterUnusedInFormals") // Intentional abuse
public <T extends GenericAnnotatedTypeFactory<?, ?, ?, ?>> @Nullable T getTypeFactoryOfSubchecker(
Class<? extends BaseTypeChecker> subCheckerClass) {
return getTypeFactory().getTypeFactoryOfSubchecker(subCheckerClass);
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2021,17 +2021,29 @@ public Store getEmptyStore() {
}

/**
* Returns the AnnotatedTypeFactory of the subchecker and copies the current visitor state to the
* sub-factory so that the types are computed properly. Because the visitor state is copied, call
* this method each time a subfactory is needed rather than store the returned subfactory in a
* field.
* Returns the type factory used by a subchecker. Returns null if no matching subchecker was found
* or if the type factory is null. The caller must know the exact checker class to request.
*
* @see BaseTypeChecker#getTypeFactoryOfSubchecker(Class)
* <p>Because the visitor state is copied, call this method each time a subfactory is needed
* rather than store the returned subfactory in a field.
*
* @param subCheckerClass the exact class of the subchecker
* @param <T> the type of {@code subCheckerClass}'s {@link AnnotatedTypeFactory}
* @return the AnnotatedTypeFactory of the subchecker or null if no subchecker exists
*/
@SuppressWarnings("TypeParameterUnusedInFormals") // Intentional abuse
public <T extends GenericAnnotatedTypeFactory<?, ?, ?, ?>, U extends BaseTypeChecker>
T getTypeFactoryOfSubchecker(Class<U> checkerClass) {
T subFactory = checker.getTypeFactoryOfSubchecker(checkerClass);
public <T extends GenericAnnotatedTypeFactory<?, ?, ?, ?>> @Nullable T getTypeFactoryOfSubchecker(
Class<? extends BaseTypeChecker> subCheckerClass) {
BaseTypeChecker subchecker = checker.getSubchecker(subCheckerClass);
if (subchecker == null) {
return null;
}

@SuppressWarnings(
"unchecked" // This might not be safe, but the caller of the method should use the correct
// type.
)
T subFactory = (T) subchecker.getTypeFactory();
if (subFactory != null && subFactory.getVisitorState() != null) {
// Copy the visitor state so that the types are computed properly.
VisitorState subFactoryVisitorState = subFactory.getVisitorState();
Expand Down

0 comments on commit fe32414

Please sign in to comment.