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

Cerrado
#126 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
4/5
Tiempo estimado
3-5 días
Aptitud para principiantes
49/100
Tipo de issue
Nueva funcionalidad
Claridad
Bastante claro
Estado de actividad
Activo
Stack tecnológico
java

Línea de trabajo

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.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

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.

Lenguaje dominante
Java
Estrellas
1
Forks
1
Merge medio
1 h 13 min
PR fusionados (30 d)
6

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de CodeLaser/maddi

Todos los issues de CodeLaser/maddi

Issues similares

Más issues de Java

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.