Skip to content

Commit

Permalink
Improvements to AnnotatedTypeFactory (#6144)
Browse files Browse the repository at this point in the history
  • Loading branch information
mernst authored Aug 31, 2023
1 parent 0257fee commit 962cb0f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -320,14 +320,14 @@ public class AnnotatedTypeFactory implements AnnotationProvider {
* Caches the supported type qualifier classes. Call {@link #getSupportedTypeQualifiers()} instead
* of using this field directly, as it may not have been initialized.
*/
private final Set<Class<? extends Annotation>> supportedQuals;
private @MonotonicNonNull Set<Class<? extends Annotation>> supportedQuals = null;

/**
* Caches the fully-qualified names of the classes in {@link #supportedQuals}. Call {@link
* #getSupportedTypeQualifierNames()} instead of using this field directly, as it may not have
* been initialized.
*/
private final Set<@CanonicalName String> supportedQualNames;
private @MonotonicNonNull Set<@CanonicalName String> supportedQualNames = null;

/** Parses stub files and stores annotations on public elements from stub files. */
public final AnnotationFileElementTypes stubTypes;
Expand Down Expand Up @@ -567,8 +567,6 @@ public AnnotatedTypeFactory(BaseTypeChecker checker) {
this.elements = processingEnv.getElementUtils();
this.types = processingEnv.getTypeUtils();

this.supportedQuals = new HashSet<>();
this.supportedQualNames = new HashSet<>();
this.stubTypes = new AnnotationFileElementTypes(this);
this.ajavaTypes = new AnnotationFileElementTypes(this);
this.currentFileAjavaTypes = null;
Expand Down Expand Up @@ -709,14 +707,14 @@ public AnnotatedTypeFactory(BaseTypeChecker checker) {
}

/**
* Requires that supportedQuals is non-empty and each element is a type qualifier. That is, no
* element has a {@code @Target} meta-annotation that contains something besides TYPE_USE or
* TYPE_PARAMETER. (@Target({}) is allowed.) @
* Requires that supportedQuals is non-null and non-empty and each element is a type qualifier.
* That is, no element has a {@code @Target} meta-annotation that contains something besides
* TYPE_USE or TYPE_PARAMETER. (@Target({}) is allowed.) @
*
* @throws BugInCF If supportedQuals is empty or contaions a non-type qualifier
*/
private void checkSupportedQualsAreTypeQuals() {
if (supportedQuals.isEmpty()) {
if (supportedQuals == null || supportedQuals.isEmpty()) {
throw new TypeSystemError("Found no supported qualifiers.");
}
for (Class<? extends Annotation> annotationClass : supportedQuals) {
Expand Down Expand Up @@ -1242,11 +1240,11 @@ public AnnotationFormatter getAnnotationFormatter() {
* supported
*/
public final Set<Class<? extends Annotation>> getSupportedTypeQualifiers() {
if (this.supportedQuals.isEmpty()) {
supportedQuals.addAll(createSupportedTypeQualifiers());
if (this.supportedQuals == null) {
supportedQuals = createSupportedTypeQualifiers();
checkSupportedQualsAreTypeQuals();
}
return Collections.unmodifiableSet(supportedQuals);
return supportedQuals;
}

/**
Expand All @@ -1261,12 +1259,14 @@ public final Set<Class<? extends Annotation>> getSupportedTypeQualifiers() {
* supported
*/
public final Set<@CanonicalName String> getSupportedTypeQualifierNames() {
if (this.supportedQualNames.isEmpty()) {
if (this.supportedQualNames == null) {
supportedQualNames = new HashSet<>();
for (Class<?> clazz : getSupportedTypeQualifiers()) {
supportedQualNames.add(clazz.getCanonicalName());
}
supportedQualNames = Collections.unmodifiableSet(supportedQualNames);
}
return Collections.unmodifiableSet(supportedQualNames);
return supportedQualNames;
}

// **********************************************************************
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,8 @@ protected Map<DefaultQualifierKind, Set<DefaultQualifierKind>> createDirectSuper
DefaultQualifierKind superQualifier = nameToQualifierKind.get(superName);
if (superQualifier == null) {
throw new TypeSystemError(
"%s @Subtype argument %s isn't in the hierarchy. Qualifiers: [%s]",
"In %s, @SubtypeOf(%s) argument isn't in the hierarchy."
+ " Have you mis-defined createSupportedTypeQualifiers()? Qualifiers: [%s]",
qualifierKind, superName, StringsPlume.join(", ", qualifierKinds));
}
directSupers.add(superQualifier);
Expand Down

0 comments on commit 962cb0f

Please sign in to comment.