Hacktoberfest 2026: le issue che i maintainer hanno segnato per ottobre, aperte e adatte ai principianti. Sfoglia le issue Hacktoberfest

Eclipse-Formatter-Plugin fails due to wrong assumptions

Aperta
#1,172 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
45/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Ferma
Stack tecnologico
java
Ambito
tooling

Direzione di ricerca

Inizia riproducendo la classe Test minimale con lo snippet dell’API Eclipse, quindi leggi SnippetFormatter.java e il suo metodo toReplacements(). Traccia come il confronto senza spazi bianchi porta il formatter Eclipse a restituire null. Il lavoro è completato quando il codice sorgente di esempio può essere formattato tramite il plugin senza questo errore, con il comportamento verificato da un test di regressione.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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.

Lingua principale
Java
Stelle
6.2k
Fork
936
Merge medio
6m
PR unite (30g)
3

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di google/google-java-format

Tutte le issue di google/google-java-format

Issue simili

Altre issue su Java

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.