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

Eclipse-Formatter-Plugin fails due to wrong assumptions

Abierto
#1,172 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
4/5
Tiempo estimado
3-5 días
Aptitud para principiantes
45/100
Tipo de issue
Error
Claridad
Bastante claro
Estado de actividad
Estancado
Stack tecnológico
java
Área
tooling

Línea de trabajo

Empieza reproduciendo la clase Test mínima con el fragmento de la API de Eclipse; después, lee SnippetFormatter.java y su método toReplacements(). Rastrea cómo la comparación sin espacios en blanco hace que el formateador de Eclipse devuelva null. Se considera completado cuando el código fuente de ejemplo se pueda formatear mediante el plugin sin este fallo y el comportamiento esté verificado mediante una prueba de regresión.

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

Descripción

Eclipse

The Eclipse-Plugin has sometimes a problem when I call the formatter programatically inside Eclipse. I am using the Snippet from https://help.eclipse.org/latest/topic/org.eclipse.jdt.doc.isv/guide/jdt_api_codeformatter.htm to format a complete Java-File. I have created a minimal Test-Class:

package test;

/**
 * Test Class
 * 
 * Generated
 * 
 */
public class Test
{
}

The standalone-Formatter-Jar adds a HTML-Tag

before the "Generated". Unfortunately, when I call the plugin via the Eclipse-APIs:

    final TextEdit edit =
        codeFormatter.format(
            CodeFormatter.K_COMPILATION_UNIT, // format a compilation unit
            content, // source to format
            0, // starting position
            content.length(), // length
            0, // initial indentation
            "\r\n" // line separator
            );

the method returns a null because it can not format the content. I have tracked the problem back to the class SnippetFormatter.java, Method toReplacements(). This method has the condition which checks the unformatted and the formatted source:

if (!NOT_WHITESPACE.retainFrom(source).equals(NOT_WHITESPACE.retainFrom(replacement))) {
      throw new IllegalArgumentException(
          "source = \"" + source + "\", replacement = \"" + replacement + "\"");
    }

This condition checks that there are only whitespace-changes in the new formatted source. The Javadoc says:

under the assumption that they differ in whitespace alone.

Unfortunately, this assumption is not correct. The source has (as mentioned above) an additional non-whitespace change and the IllegalArgumentException is thrown. This Exception causes the Eclipse-Formatter-Class to fail and to return only null, thereby denying to format the source.

Lenguaje dominante
Java
Estrellas
6.2k
Forks
936
Merge medio
6 min
PR fusionados (30 d)
3

Guía de contribución

Abrir la guía de contribución

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 google/google-java-format

Todos los issues de google/google-java-format

Issues similares

Más issues de Java

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.