Hacktoberfest 2026: los issues que los mantenedores marcaron para octubre, abiertos y aptos para principiantes. Explorar issues de Hacktoberfest

shortenFullyQualifiedTypes corrupts method references on fields (x.field::m -> bogus import x.field)

Cerrado
#3,133 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

Ya se ha fusionado un pull request relacionado.

  • #3134 de @maxandersen — fusionado

Evaluación

Dificultad
3/5
Tiempo estimado
1-2 días
Aptitud para principiantes
25/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Estancado
Stack tecnológico
java
Área
build-system

Línea de trabajo

Start by reading the implementation of shortenFullyQualifiedTypes() added in #3005 and the related tests; the issue does not name their file paths. Reproduce with the supplied Java example and run spotlessApply followed by compileJava. Done means catalog.aliases::get remains unchanged, no bogus import is added, and a regression test covers the case. A linked open pull request (#3134) indicates work is already underway.

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

Descripción

Summary

shortenFullyQualifiedTypes() (added in #3005) corrupts a method reference on a field of a local variable. It treats localVar.field::method as if localVar.field were a fully-qualified type, shortens it to field::method, and injects a bogus import localVar.field; — which does not compile.

Because it only manifests on spotlessApply (not the read-only spotlessCheck), a developer runs spotlessApply, commits, and then the project no longer builds.

Version
  • com.diffplug.spotless 8.10.3 (Gradle plugin)
  • JDK 11 / 17 (reproduces on both)
Minimal reproducer

build.gradle:

plugins {
    id 'java'
    id 'com.diffplug.spotless' version '8.10.3'
}
repositories { mavenCentral() }
spotless { java { shortenFullyQualifiedTypes() } }

src/main/java/Example.java:

import java.util.Map;
import java.util.function.Function;

public class Example {
    static class Catalog {
        Map<String, String> aliases;
    }

    String demo(Catalog catalog) {
        return find(catalog.aliases::get);   // method ref on a field of a local var
    }

    String find(Function<String, String> f) {
        return f.apply("x");
    }
}
Steps
./gradlew spotlessApply
./gradlew compileJava
Actual

spotlessApply rewrites the file to:

import java.util.Map;
import java.util.function.Function;
import catalog.aliases;          // <-- bogus import of a non-existent package/type

public class Example {
    ...
    String demo(Catalog catalog) {
        return find(aliases::get);   // <-- qualifier dropped, now unresolved
    }
    ...
}

and compilation fails:

Example.java: error: package catalog does not exist
Expected

catalog.aliases::get is a method reference on a field access of a local variable, not a fully-qualified type reference. It must be left untouched (no shortening, no import added).

Root cause (guess)

The qualifier-resolution doesn't verify that the left-hand side of a qualified name / method-reference scope actually resolves to a type before shortening. Field accesses on locals/params (x.y, x.y::m, x.y.z) are being misclassified as fully-qualified type names. Checking that the qualifier is a type (not a local variable / field expression) before rewriting should fix it.

Found while enabling the step on a real codebase (jbangdev/jbang), where catalog.aliases::get (a Catalog local + Map field) broke the build.

Lenguaje dominante
Java
Estrellas
5.7k
Forks
565
Merge medio
2 d 14 h
PR fusionados (30 d)
66

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 diffplug/spotless

Todos los issues de diffplug/spotless

Issues similares

Más issues de Java

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.