Skip to content

Refactor cache to determine if an element is annotatedfor#1331

Open
aosen-xiong wants to merge 15 commits intoeisop:masterfrom
aosen-xiong:code-refactor-annotatedfor
Open

Refactor cache to determine if an element is annotatedfor#1331
aosen-xiong wants to merge 15 commits intoeisop:masterfrom
aosen-xiong:code-refactor-annotatedfor

Conversation

@aosen-xiong
Copy link
Copy Markdown
Collaborator

@aosen-xiong aosen-xiong commented Jul 10, 2025

In this PR, I refactored the isElementAnnotatedForThisChecker and its cache from QualifierDefaults to AnnotatedTypeFactory to foster code reuse in both determining the default and whether error should be suppressed. I am making the method SourceChecker#isElementAnnotatedForThisChecker abstract to reduce code duplication.

@aosen-xiong aosen-xiong marked this pull request as ready for review August 26, 2025 04:00
@aosen-xiong aosen-xiong requested a review from wmdietl August 26, 2025 04:35
Copy link
Copy Markdown
Member

@wmdietl wmdietl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@wmdietl wmdietl assigned aosen-xiong and unassigned wmdietl Sep 1, 2025
&& !isElementAnnotatedForThisChecker(annotationScope);
&& !atypeFactory
.getChecker()
.isElementAnnotatedForThisCheckerOrUpstreamChecker(annotationScope);
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This call is causing the problem :-1: warning: junit-assertions.astub: Parse problem: org.checkerframework.javacutil.TypeSystemError: Called getTypeFactory() before initialization was complet.

It is interesting I can not get the type factory from the checker itself because the visitor is not initialized, even though I already had atypeFactory.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to stacktrace, the method call went wrong because the chain initialization is init_checker -> init_visitor -> init_atf -> postinit in atf's constructor -> parsestubfiles -> determine default -> atf.getchecker().getTypeFactory(). We can get the checker from the atf successfully because it is assigned in the AnnotatedTypeFactory's constructor. But we don't have the visitor yet from the checker because it is the one underinitialization. Looks like this error too conservative because a partially initialized atf is able to determine whether an element is annotated or not.

at org.checkerframework.framework.stub.AnnotationFileElementTypes.parseStubFiles(AnnotationFileElementTypes.java:210)
at org.checkerframework.framework.type.AnnotatedTypeFactory.parseAnnotationFiles(AnnotatedTypeFactory.java:4097)
at org.checkerframework.framework.type.GenericAnnotatedTypeFactory.postInit(GenericAnnotatedTypeFactory.java:438)
at org.checkerframework.checker.initialization.InitializationAnnotatedTypeFactory.<init>(InitializationAnnotatedTypeFactory.java:60)
at org.checkerframework.checker.initialization.InitializationVisitor.createTypeFactory(InitializationVisitor.java:84)
at org.checkerframework.checker.initialization.InitializationVisitor.createTypeFactory(InitializationVisitor.java:52)
at org.checkerframework.common.basetype.BaseTypeVisitor.<init>(BaseTypeVisitor.java:343)
at org.checkerframework.common.basetype.BaseTypeVisitor.<init>(BaseTypeVisitor.java:329)
at org.checkerframework.checker.initialization.InitializationVisitor.<init>(InitializationVisitor.java:77)
at org.checkerframework.checker.initialization.InitializationChecker.createSourceVisitor(InitializationChecker.java:149)
at org.checkerframework.checker.initialization.InitializationChecker.createSourceVisitor(InitializationChecker.java:73)
at org.checkerframework.framework.source.SourceChecker.initChecker(SourceChecker.java:1175)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previous version works with code duplication because works flow is init_checker -> init_visitor -> init_atf -> postinit in atf's constructor -> parsestubfiles -> determine default -> atf. I am going to revert to previous version with some comments.

@aosen-xiong aosen-xiong requested a review from wmdietl September 4, 2025 02:25
@aosen-xiong aosen-xiong assigned wmdietl and unassigned aosen-xiong Sep 4, 2025
@aosen-xiong aosen-xiong force-pushed the code-refactor-annotatedfor branch from c10565f to eb741f4 Compare October 7, 2025 23:47
Copilot AI review requested due to automatic review settings December 19, 2025 06:28
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR refactors the cache and method for determining if an element is annotated for a checker, moving it from QualifierDefaults to AnnotatedTypeFactory to enable code reuse. The method isElementAnnotatedForThisChecker is renamed to isElementAnnotatedForThisCheckerOrUpstreamChecker and made abstract in SourceChecker.

  • Moved the isElementAnnotatedForThisChecker method and its cache from QualifierDefaults to AnnotatedTypeFactory
  • Made SourceChecker#isElementAnnotatedForThisCheckerOrUpstreamChecker an abstract method
  • Implemented the abstract method in all subclasses (BaseTypeChecker, AggregateChecker, and utility checkers)

Reviewed changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 6 comments.

Show a summary per file
File Description
QualifierDefaults.java Removed private isElementAnnotatedForThisChecker method and elementAnnotatedFors cache; updated calls to use the factory method
AnnotatedTypeFactory.java Added public isElementAnnotatedForThisCheckerOrUpstreamChecker method with the cache implementation
SourceChecker.java Changed method from private to protected abstract; removed implementation and imports; updated method calls
BaseTypeChecker.java Implemented abstract method by delegating to the type factory
AggregateChecker.java Implemented abstract method to return false (delegates to subcheckers)
SignaturePrinter.java Implemented abstract method to throw BugInCF (not expected to be called)
JavaCodeStatistics.java Implemented abstract method to throw BugInCF (not expected to be called)
AnnotationStatistics.java Implemented abstract method to throw BugInCF (not expected to be called)

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@wmdietl
Copy link
Copy Markdown
Member

wmdietl commented Mar 22, 2026

@aosen-xiong Please go through the copilot suggestions and see what should be addressed.

@wmdietl wmdietl assigned aosen-xiong and unassigned wmdietl Mar 22, 2026
aosen-xiong and others added 3 commits March 25, 2026 18:35
@aosen-xiong aosen-xiong assigned wmdietl and unassigned aosen-xiong Mar 26, 2026
@aosen-xiong
Copy link
Copy Markdown
Collaborator Author

aosen-xiong commented Mar 27, 2026

Weird, CI failed again.

@aosen-xiong
Copy link
Copy Markdown
Collaborator Author

@wmdietl ping for review.

@aosen-xiong
Copy link
Copy Markdown
Collaborator Author

aosen-xiong commented Apr 2, 2026

@wmdietl Hi Werner, can you please review this PR? Thanks. After this one, we can move to #1304.

@aosen-xiong
Copy link
Copy Markdown
Collaborator Author

@thisisalexandercook Hi Alex, can you also review this PR? Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants