Hacktoberfest 2026:メンテナが10月に向けて印を付けた、オープンで初心者向けの issue。 Hacktoberfest の issue を見る

Eclipse-Formatter-Plugin fails due to wrong assumptions

オープン
#1,172 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

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

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
45/100
issue の種類
バグ
明瞭さ
おおむね明確
活発さ
停滞
技術スタック
java
領域
tooling

調査の方向性

まず Eclipse API スニペットを使って最小限の Test クラスを再現し、次に SnippetFormatter.java とその toReplacements() メソッドを読みます。空白を除いた比較によって Eclipse formatter が null を返すに至る流れを追跡します。サンプルソースをこの失敗なしに plugin 経由でフォーマットでき、その動作が regression test で検証されれば完了です。

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

説明

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.

主要言語
Java
スター
6.2k
フォーク
936
平均マージ
6分
マージ済み PR(30日)
3

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

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

google/google-java-format のほかの issue

google/google-java-format の issue をすべて見る

似ている issue

Java の issue をもっと見る

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

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