TypeInfo: a way to reach the anonymous and local classes declared in a type's bodies

クローズ
#126 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
49/100
issue の種類
機能追加
明瞭さ
おおむね明確
活発さ
活発
技術スタック
java

調査の方向性

Start by tracing how ParseConstructorCall and ParseTypeDeclaration create TypeInfo for anonymous and local classes, then inspect TypeInfo.subTypes() and recursiveSubTypeStream(). The issue proposes storing those classes in declaration order or exposing a body-walking stream; choose an approach that preserves subTypes() as member types only. Done when callers can discover nested anonymous and local classes recursively, including the example loop, without each writing its own body walk.

索引モデルが issue の本文から書いたものです。

説明

enhancement
What is missing

TypeInfo.subTypes() returns the member types, and recursiveSubTypeStream() is built on it. Anonymous classes (new T() { … }) and local classes (declared in a method body) are not in either: ParseConstructorCall and ParseTypeDeclaration give them a TypeInfo with an enclosing type and a stable index (getAndIncrementAnonymousTypes), but nothing adds them to the enclosing type, so the only way to find them is to visit every method body and field initializer for ConstructorCall.anonymousClass() and LocalTypeDeclaration.typeInfo(), recursively.

Every tool that wants "all the code in this compilation unit" (a search over methods, a rewrite of loop bodies, a census of calls) rediscovers this the hard way: a loop inside

return new HttpResponse() {
    @Override
    public void generateResponse(StaplerRequest2 req, StaplerResponse2 rsp, Object node) {
        for (Map.Entry<String, String> header : headers.entrySet()) {
            rsp.setHeader(header.getKey(), header.getValue());
        }
    }
};

is simply never seen when the tool walks primary.recursiveSubTypeStream().flatMap(TypeInfo::constructorAndMethodStream). In a large code base (Jenkins core) this hid a few percent of the sites of one rewrite.

Proposal

Either of:

  • TypeInfo.localTypes(): the anonymous and local classes whose enclosing type this is, in declaration order, filled by the parser at the two places that mint them; and a recursiveSubTypeStreamIncludingLocal() (or a boolean parameter) that descends into them as it does into member types; or
  • a documented TypeInfo.anonymousAndLocalTypeStream() that performs the body walk once, in the API, so that callers do not each write it.

The first is cheaper for callers and keeps subTypes() as it is (member types only), which some callers surely depend on.

Workaround used

A helper that walks constructorAndMethodStream() bodies and field initializers with Element.visit, collecting ConstructorCall.anonymousClass() and LocalTypeDeclaration.typeInfo() into a queue, deduplicated by identity, until the queue is empty.

Seen on 0.9.5-alpha.13.

主要言語
Java
スター
1
フォーク
1
平均マージ
1時間 44分
マージ済み PR(30日)
6

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

CodeLaser/maddi のほかの issue

CodeLaser/maddi の issue をすべて見る

似ている issue

Java の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。