Hacktoberfest 2026: the issues maintainers tagged for October, open and beginner-friendly. Browse Hacktoberfest issues

Dependent type annotations are not viewpoint-adapted at an anonymous class creation

Open
#8,377 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 2 days

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
48/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Active
Tech stack
java
Domain
backend

Research direction

Start by reading DependentTypesHelper.atInvocation and trace how TreeUtils.elementFromUse(tree) and viewpointAdaptedCopier handle anonymous class constructors. Compare the invocation and super constructor types, including return types, type variables, and the extra enclosing-instance parameter for outer.new Inner(...) {}. Done means javac -processor nullness accepts the anonymous example while preserving the behavior of plain.

Written by the indexing model from the issue text.

Description

Dependent type annotations on a constructor's parameters are not viewpoint-adapted when the constructor is invoked by creating an anonymous class.

import java.util.Map;
import org.checkerframework.checker.nullness.qual.KeyFor;

public class DependentAnon {
  static class C {
    C(Map<String, Object> m, @KeyFor("#1") String k) {}
  }

  void plain(Map<String, Object> map, @KeyFor("#1") String key) {
    new C(map, key);
  }

  void anonymous(Map<String, Object> map, @KeyFor("#1") String key) {
    new C(map, key) {};
  }
}

javac -processor nullness DependentAnon.java accepts plain but issues a false positive for anonymous:

DependentAnon.java:14: error: [argument] incompatible argument for parameter k of C constructor.
    new C(map, key) {};
               ^
  found   : @KeyFor("map") String
  required: @KeyFor("#1") String

The cause is in DependentTypesHelper.atInvocation, which uses TreeUtils.elementFromUse(tree) as the declared executable. For an anonymous class, that is the anonymous constructor, which has no dependent type annotations, so the method returns early. AnnotatedTypeFactory.constructorFromUse copies the super constructor's parameter types, including @KeyFor("#1"), onto the anonymous constructor, but they are never viewpoint-adapted.

Using TreeUtils.getSuperConstructor(tree) there is not enough by itself. atInvocation copies annotations from the declared type to the invocation type position by position (viewpointAdaptedCopier), and the super constructor's type differs from the anonymous constructor's type in its return type, possibly its type variables, and, for outer.new Inner(...) {}, its number of parameters (the anonymous constructor has an extra first parameter for the enclosing instance).

Dominant language
Java
Stars
1.1k
Forks
440
Avg merge
3d 18h
Merged PRs (30d)
61

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from typetools/checker-framework

All issues in typetools/checker-framework

Similar issues

More Java issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.