Skip to content

Remove annotations from classes that aren't visible to users, even through non-exported modules. - #179

Merged
cpovirk merged 2 commits into
mainfrom
internals
Nov 10, 2026
Merged

cpovirk merged 2 commits into
mainfrom
internals

Conversation

@cpovirk

@cpovirk cpovirk commented Nov 10, 2026

Copy link
Copy Markdown
Collaborator

#178 got me to finally do this.

As a test of this change, I built the Javadoc (including for non-exported modules) before and after it. The only differences in the generated files were in serialized-form.html.

Some interesting cases arise with non-publicly accessible classes that act as supertypes of publicly visible classes. I handled them on a case-by-case basis:

  • I kept the annotations on AbstractStringBuilder. AbstractStringBuilder is the supertype of StringBuilder and StringBuffer. Its subclasses override almost all its methods, but there are some that they do not override, and a few of those (at least codePoints() and the two substring methods) include non-primitive types in their signatures, which means that they're affected by the @NullMarked annotation on AbstractStringBuilder. It would technically "work" to remove all the other annotations from that class, but that would be misleading and fragile.

  • I did remove the annotation from ChronoLocalDateImpl. ChronoLocalDateImpl is the supertype of some public classes, but:

    • Its only annotation is on equals, which is overridden by those subclasses.
    • We haven't made it @NullMarked. Of course, another option
    • I haven't looked into it extremely deeply; maybe we should "should" annotate it.
  • I kept the annotations on ConcurrentHashMap.CollectionView: CollectionView is the supertype of KeySetView, and its toArray methods are available to users through that type. Plus, it's @NullMarked by virtue of being nested inside ConcurrentHashMap, so we "need" to annotate it correctly.

    • I now notice a few "missing" annotations in that area (CollectionView.containsAll, KeySetView.equals). I'll come back for them.

…rough non-exported modules.

#178 got me to finally do this.

As a test of this change, I built the Javadoc (including for non-exported modules) before and after it. The only differences in the generated files were in `serialized-form.html`.

Some interesting cases arise with non-publicly accessible classes that act as supertypes of publicly visible classes. I handled them on a case-by-case basis:

- I kept the annotations on `AbstractStringBuilder`. `AbstractStringBuilder` is the supertype of `StringBuilder` and `StringBuffer`. Its subclasses override _almost_ all its methods, but there are some that they do not override, and a few of those (at least `codePoints()` and the two `substring` methods) include non-primitive types in their signatures, which means that they're affected by the `@NullMarked` annotation on `AbstractStringBuilder`. It would technically "work" to remove all the _other_ annotations from that class, but that would be misleading and fragile.

- I _did remove_ the annotation from `ChronoLocalDateImpl`. `ChronoLocalDateImpl` is the supertype of some public classes, but:
  - Its only annotation is on `equals`, which is overridden by those subclasses.
  - We haven't made it `@NullMarked`. Of course, another option
  - I haven't looked into it extremely deeply; maybe we should "should" annotate it.

- I kept the annotations on `ConcurrentHashMap.CollectionView`: `CollectionView` is the supertype of `KeySetView`, and its `toArray` methods are available to users through that type. Plus, it's `@NullMarked` by virtue of being nested inside `ConcurrentHashMap`, so we "need" to annotate it correctly.
  - I now notice a few "missing" annotations in that area (`CollectionView.containsAll`, `KeySetView.equals`). I'll come back for them.
@msridhar

Copy link
Copy Markdown
Collaborator

@cpovirk Can you say again what the motivation is here? I see from #178 this somehow enables better stripping of an ABI jar artifact, but I don't fully follow. If this change makes maintenance of annotations fragile, and it only ends up slightly shrinking the ABI jar, is it worth it?

@cpovirk

cpovirk commented Nov 10, 2026

Copy link
Copy Markdown
Collaborator Author

Yes, sorry, I should have said more.

First: Despite all my overthinking about, I don't think I'm making things more fragile. (Let me know if you end up disagreeing!) The closest I'm coming to that is by removing the annotation from ChronoLocalDateImpl.equals: That means that, if the JDK were someday to stop overriding that method in one of the ChronoLocalDateImpl subclasses, then users would see a parameter with unspecified nullness instead of @Nullable. Users of most nullness checkers wouldn't notice, given lenient treatment of unspecified nullness. I'm still very open to leaving the annotation and even fully annotating ChronoLocalDateImpl, and in fact I hope to do so "eventually"; it just wasn't clearly worth it here. But it would be faster to annotate it than for us to agonize over it, so let me know if you'd prefer that.

As for benefits:

We have very rarely annotated non-publicly visible APIs in this repo. I look at this PR more as enacting that policy more widely than as changing anything. The improved consistency should help make clear to readers that we're not trying to annotate internals.

But the thing usually gets me thinking about this is merge conflicts: Every annotation in our clone is a potential merge conflict when I pull in changes from OpenJDK upstream. Obviously that's a cost that I'm happy to pay for "real" nullness information. But it's sad when we get a merge conflict on an internal API that isn't adding any value. (On that front, I would also like to fix up some unnecessary formatting deviations that are left over from my sloppy removal of Checker-Framework-specific annotations years ago. I might get some of the easy cases later today.)

@msridhar msridhar left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Didn't review every line carefully, but if there are issues we can fix in follow-ups

@cpovirk

cpovirk commented Nov 10, 2026

Copy link
Copy Markdown
Collaborator Author

Thanks!

And I should have actually replied to this:

this somehow enables better stripping of an ABI jar artifact

The underlying principle is that we can omit class files that contain no JSpecify annotations. So, the fewer the classes that contain them, the better.

The connection to #178 is that that PR observes that we could omit even classes that do contain JSpecify annotations if those classes aren't user-visible. However, because of cases like AbstractStringBuilder, it's not completely trivial to identify whether a given class is user-visible. (Not that it's super complicated once you know to consider that case in the first place.) So I see that as another reason to double down on our principle of not annotating internals in the first place, rather than trying to identify what the internals are after the fact during the build process.

(I admit that it also didn't hurt that I noticed that, by putting together the Error Prone check that automated most of this PR, I was able to get further testing of the isLikelyToBePublicApi logic that I'd introduced in google/error-prone#6206 and used in some of my recent PRs here :) I think Gemini found a small bug or two.)

@vlsi

vlsi commented Nov 10, 2026

Copy link
Copy Markdown
Contributor

What is the point of removing annotations? I guess they could be removed at the "build" time.
However, having all the classes annotated might probably help verifying jdk annotation themselves.
I don't mind though.

@cpovirk

cpovirk commented Nov 10, 2026

Copy link
Copy Markdown
Collaborator Author

It would be a noble project to apply them consistently, especially if we had a strong reason to believe that the JDK could someday incorporate our nullness data. But the current setup of partial annotations for internals leads to merge conflicts without providing enough data to verify the annotations in the JDK. (Verifying the annotations would of course also require suppressions or code changes for cases in which the checker can't identify that something is safe, so it would be a larger project still, with correspondingly big costs and potential benefits.)

cpovirk added a commit that referenced this pull request Nov 10, 2026
This should address the error that just popped up, as [seen](https://github.com/jspecify/jdk/actions/runs/42861855071/job/129989794061?pr=179) in #179:

```
Reading package lists...
Building dependency tree...
Reading state information...
Package g++-10 is not available, but is referred to by another package.
This may mean that the package is missing, has been obsoleted, or
is only available from another source

E: Unable to locate package gcc-10
E: Package 'g++-10' has no installation candidate
```

It turns out that we haven't been using GCC 10 all this time, anyway, so it's safe to remove, Gemini explains:

> Yes, this is directly caused by GitHub's `ubuntu-latest` migration from Ubuntu 24.04 to Ubuntu 26.04 ([actions/runner-images#14748](actions/runner-images#14748), rolling out October 19 – November 19, 2026):
>
> 1. **Why it failed:** [jspecify.yml](https://github.com/jspecify/jdk/blob/main/.github/workflows/jspecify.yml#L11-L30) uses `runs-on: ubuntu-latest` and runs `sudo apt-get install gcc-10 g++-10 ...`. Ubuntu 26.04 dropped `gcc-10` and `g++-10` from its APT repositories.
> 2. **Why we don't even need `gcc-10` / `g++-10`:** When [jspecify.yml](https://github.com/jspecify/jdk/blob/main/.github/workflows/jspecify.yml) was originally created (#114), the `apt-get install gcc-10 g++-10` line was copied from upstream OpenJDK's [build-linux.yml](https://github.com/jspecify/jdk/blob/main/.github/workflows/build-linux.yml#L128-L141)—**without** the accompanying `update-alternatives` command (or `CC`/`CXX` configure arguments). As a result, `./configure` in `jspecify.yml` has never actually used `gcc-10`/`g++-10`; it has always just used the runner's pre-installed default `/usr/bin/gcc` and `/usr/bin/g++` (GCC 13 on Ubuntu 24.04, and GCC 15 on Ubuntu 26.04).
> 3. **Recommended fix:** Simply drop `gcc-10 g++-10` from the `apt-get install` line in [jspecify.yml](https://github.com/jspecify/jdk/blob/main/.github/workflows/jspecify.yml#L29). Current OpenJDK 28 sources in this branch already support GCC 15 (which is also what your local workstation has configured in `build/linux-x86_64-server-release`), and `jspecify.yml` already passes `--disable-warnings-as-errors`. (If anything else in Ubuntu 26.04 ever causes trouble, you could also pin `runs-on: ubuntu-24.04` to match upstream [main.yml](https://github.com/jspecify/jdk/blob/main/.github/workflows/main.yml#L60).)

(Maybe we *should* find the version that upstream uses, install that, and run `update-alternatives` for that. But for now, I'm happy to get the build working again, and we can cross that bridge when we come to it.)
@cpovirk
cpovirk merged commit cf58ff9 into main Nov 10, 2026
29 of 30 checks passed
@cpovirk
cpovirk deleted the internals branch November 10, 2026 22:15
copybara-service Bot pushed a commit to google/error-prone that referenced this pull request Nov 14, 2026
Some of these don't even matter to its existing users. For example, we're never considering adding a nullness annotation to a module based on whether it "looks like public API." But it's good to future-proof, especially since the two overloads have behavior inconsistent with each other before this CL.

Other cases probably _could_ matter even now. Maybe I should rerun some of our annotation-adding tools over the JSpecify JDK clone again....

(This came up during jspecify/jdk#179.)

Some specifics:
- A record parameter is always visible (if the enclosing record type is), but it shows up as in the AST as the backing `FIELD`, which is `private`.
- Starting directly on a method or field (rather than on, say, a type somewhere inside it) was wrongly returning "not a public API" instead of looking at the visibility of that symbol.

PiperOrigin-RevId: 1021439393
cpovirk added a commit that referenced this pull request Nov 18, 2026
(noticed during #179)

Interesting cases:

- I notice that `removeAll` can handle null elements if it falls into the `c instanceof Set<?> && c.size() > t.length` case or the `map.table` null case. I'm not sure whether to consider that enough for the parameter's type argument to be nullable or not. Is it fundamentally different from noticing how the method can handle an argument with a nullable element type _if that argument happens to contain no elements_? (By _that_ logic, even `ImmutableList.copyOf` should use a nullable type argument for its parameter!) I ended up moving to including a non-null bound on the wildcard.
- `contains` and `remove` are defined in the superclass `CollectionView`, and each can handle `null` in at least one package-private subclasses, despite the blanket `@throws` clause in the documentation. But they do throw for `KeySetView`, which is the only user-visible type. So I went with a non-null type there, as well.
- Instead of declaring the type parameter of `KeySetView` as `V extends @nullable Object`, we could leave it as `V` but declare the obscure `getMappedValue()` as returning `@Nullable V`. (I see no calls to that method in our depot, and I don't think I knew it existed before today.) I decided to go with the principled `V extends @nullable Object` approach, which might require a handful of updates in Google's Kotlin code (to declare `ConcurrentHashMap.KeySetView<K, V?>` instead of `ConcurrentHashMap.KeySetView<K, V>`, but they should probably use `Set<K>` instead, anyway).
cpovirk added a commit that referenced this pull request Nov 19, 2026
The primary goal here is to make future merges easier. (I have had trouble in past merges that I traced to spurious diffs.)

Most of the changes are about removing whitespace that I had regrettably left behind when removing Checker-Framework-specific annotations from typetools/checker-framework.

There are other changes, too, including restoring the `ThreadGroup.destroy()` method that I'd removed in a prior bad merge. (This doesn't matter for nullness purposes, but it, too, could reduce the chances of problems in future merges.)

Gemini (which did the work) elaborates:

> ### Summary of Changes
>
> 1. **Top-level files (3 files)**
>    - Removed Checker Framework leftovers `azure-pipelines.yml` and `README-rewritten-jdk-history` (`git rm`).
>    - Reverted [.gitignore](https://github.com/jspecify/jdk/blob/main/.gitignore) to match `openjdk/master`.
>
> 2. **Merge-conflict restoration & CF leftovers**
>    - [ThreadGroup.java](https://github.com/jspecify/jdk/blob/main/src/java.base/share/classes/java/lang/ThreadGroup.java): Restored the broken/commented-out `destroy()` Javadoc, `@Deprecated(since="16", forRemoval=true)`, and `public final void destroy() {}` from `openjdk/master`, and removed its 3 `@SuppressWarnings("index:array.access.unsafe.high")` annotations.
>    - Removed all remaining 38 leftover `@SuppressWarnings(...)` annotations across 14 files, reverted the modified `@SuppressWarnings` in [EnumMap.java](https://github.com/jspecify/jdk/blob/main/src/java.base/share/classes/java/util/EnumMap.java) back to `@SuppressWarnings("unchecked")`, reverted the temporary `result` variable in [ResourceBundle.java](https://github.com/jspecify/jdk/blob/main/src/java.base/share/classes/java/util/ResourceBundle.java) back to `return sb.toString();`, and removed the 2 CF comments in [HashMap.java](https://github.com/jspecify/jdk/blob/main/src/java.base/share/classes/java/util/HashMap.java) and [ListIterator.java](https://github.com/jspecify/jdk/blob/main/src/java.base/share/classes/java/util/ListIterator.java).
>    - Removed all 18 leftover `@Override` annotations ([AbstractCollection.java](https://github.com/jspecify/jdk/blob/main/src/java.base/share/classes/java/util/AbstractCollection.java), [PrimitiveIterator.java](https://github.com/jspecify/jdk/blob/main/src/java.base/share/classes/java/util/PrimitiveIterator.java), and [Thread.java](https://github.com/jspecify/jdk/blob/main/src/java.base/share/classes/java/lang/Thread.java)).
>    - Reverted all 7 explicit diamond type arguments in [TemporalQueries.java](https://github.com/jspecify/jdk/blob/main/src/java.base/share/classes/java/time/temporal/TemporalQueries.java) back to `new TemporalQuery<>()`.
>
> 3. **Unused & duplicate imports**
>    - Removed 98 unused `org.jspecify.annotations` imports (95 `Nullable`, 3 `NonNull`) while keeping the placement/surrounding blank lines of retained JSpecify imports unchanged.
>    - Removed the duplicate `import org.jspecify.annotations.Nullable;` (and its extra blank line) at the top of [AbstractChronology.java](https://github.com/jspecify/jdk/blob/main/src/java.base/share/classes/java/time/chrono/AbstractChronology.java#L62-L64) (keeping the grouped JSpecify imports further down the import block).
>    - Removed the 4 unused non-JSpecify imports in [InheritableThreadLocal.java](https://github.com/jspecify/jdk/blob/main/src/java.base/share/classes/java/lang/InheritableThreadLocal.java), [Reference.java](https://github.com/jspecify/jdk/blob/main/src/java.base/share/classes/java/lang/ref/Reference.java), and [UUID.java](https://github.com/jspecify/jdk/blob/main/src/java.base/share/classes/java/util/UUID.java), and removed the duplicate `import java.lang.ref.WeakReference;` in [WeakHashMap.java](https://github.com/jspecify/jdk/blob/main/src/java.base/share/classes/java/util/WeakHashMap.java) (restoring upstream import order).
>
> 4. **SQL postfix array annotations & `CompletableFuture` wraps & whitespace normalization**
>    - Standardized [CallableStatement.java](https://github.com/jspecify/jdk/blob/main/src/java.sql/share/classes/java/sql/CallableStatement.java#L974) (`setBytes`) and [ResultSet.java](https://github.com/jspecify/jdk/blob/main/src/java.sql/share/classes/java/sql/ResultSet.java#L2099) (`updateBytes`) to `byte x @nullable []`.
>    - Undid all 9 line-wrap/unwrap diffs in both directions in [CompletableFuture.java](https://github.com/jspecify/jdk/blob/main/src/java.base/share/classes/java/util/concurrent/CompletableFuture.java).
>    - Normalized 2,776 unchanged-token whitespace gaps, 147 gaps around `@Nullable`/`@NonNull` (including tabs in Swing/AWT parameter lists and trailing-space newlines in [Attributes.java](https://github.com/jspecify/jdk/blob/main/src/java.naming/share/classes/javax/naming/directory/Attributes.java#L102) and [NamespaceContext.java](https://github.com/jspecify/jdk/blob/main/src/java.xml/share/classes/javax/xml/namespace/NamespaceContext.java#L207)), 69 gaps around `@NullMarked`, and `extends  Object` in [ConcurrentHashMap.java](https://github.com/jspecify/jdk/blob/main/src/java.base/share/classes/java/util/concurrent/ConcurrentHashMap.java).
>
> ### Verification
>
> - **Exact token & whitespace verification vs `openjdk/master`** across all 879 modified `.java` files:
>   - `0` unchanged-token whitespace diffs
>   - `0` inserted-region whitespace anomalies (outside kept JSpecify import placement)
>   - `0` unused JSpecify imports
>   - `0` non-JSpecify annotations added or removed (exact preserved counts: `4038` `@Nullable`, `578` `@NullMarked`, `43` `@NonNull`, `8` `@NullUnmarked`)
>   - Only remaining non-annotation / non-import / non-`extends Object` hunks are the 25 intentional ones: [module-info.java](https://github.com/jspecify/jdk/blob/main/src/java.base/share/classes/module-info.java#L138), [Provider.java](https://github.com/jspecify/jdk/blob/main/src/java.base/share/classes/java/security/Provider.java#L573) (3 `?>`), [Objects.java](https://github.com/jspecify/jdk/blob/main/src/java.base/share/classes/java/util/Objects.java#L70) (J2CL overload), and the 20 intentional JSpecify comments.
> - **Build**: `make jdk` completed with exit code `0` (`Finished building target 'jdk' in configuration 'linux-x86_64-server-release'`).

I tested the results with the same Javadoc-based methodology as in #179. I saw some changes in "auxiliary" files like the `class-use` files, but the only change to "actual Javadoc" I saw was in `ThreadGroup`, where `destroy()` returns, as expected.
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.

3 participants