Eclipse代码不适用于Java泛型代码?

Eclipse代码不适用于Java泛型代码?,java,eclipse,Java,Eclipse,我使用eclipse代码格式jar文件来格式化java代码,并在maven dependecy下面使用 org.eclipse.jdt org.eclipse.jdt.core 3.7.1 当我试图格式化下面的代码时 package com.editor.test; import org.eclipse.jdt.core.ToolFactory; import org.eclipse.jdt.core.formatter.CodeFormatter; import org.eclips

我使用eclipse代码格式jar文件来格式化java代码,并在maven dependecy下面使用


org.eclipse.jdt
org.eclipse.jdt.core
3.7.1  
当我试图格式化下面的代码时

package com.editor.test;

import org.eclipse.jdt.core.ToolFactory;
import org.eclipse.jdt.core.formatter.CodeFormatter;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.Document;
import org.eclipse.jface.text.IDocument;
import org.eclipse.text.edits.MalformedTreeException;
import org.eclipse.text.edits.TextEdit;

public class FormatterTest {
    public static void main(String[] args) {
        String code = "import java.util.Map; public class TestFormatter{public static void main(String[] args){Map<String,Object> map=null;System.out.println(\"Hello World\");}}";
        CodeFormatter codeFormatter = ToolFactory.createCodeFormatter(null);
        TextEdit textEdit = codeFormatter.format(CodeFormatter.K_COMPILATION_UNIT, code, 0, code.length(), 0, null);
        IDocument doc = new Document(code);

        try {
            textEdit.apply(doc);
            System.out.println(doc.get());
        } catch (MalformedTreeException e) {
            e.printStackTrace();
        } catch (BadLocationException e) {
            e.printStackTrace();
        }
    }
}
package com.editor.test;
导入org.eclipse.jdt.core.ToolFactory;
导入org.eclipse.jdt.core.formatter.CodeFormatter;
导入org.eclipse.jface.text.BadLocationException;
导入org.eclipse.jface.text.Document;
导入org.eclipse.jface.text.IDocument;
导入org.eclipse.text.edits.MalformedTreeException;
导入org.eclipse.text.edits.TextEdit;
公共类格式化程序测试{
公共静态void main(字符串[]args){
String code=“import java.util.Map;公共类TestFormatter{public static void main(String[]args){Map Map=null;System.out.println(\'Hello World\”);}}”;
CodeFormatter CodeFormatter=ToolFactory.createCodeFormatter(null);
TextEdit TextEdit=codeFormatter.format(codeFormatter.K_编译单位,代码,0,代码.length(),0,null);
i文件单据=新文件(代码);
试一试{
text编辑.应用(doc);
System.out.println(doc.get());
}catch(格式错误的tree异常){
e、 printStackTrace();
}捕获(错误位置异常e){
e、 printStackTrace();
}
}
}

但当我添加泛型时,eclipse代码格式无法格式化代码。有谁能告诉你这个问题的解决方案是什么。

你没有为
ToolFactory.createCodeFormatter指定任何源代码级别或合规级别的选项,因此你可能会得到一个只支持原始Java而不支持泛型的格式化程序

ToolFactory.createCodeFormatter的JavaDoc说明:

给定的选项应至少提供源代码级别 (JavaCore.COMPILER_SOURCE),编译器遵从性级别 (JavaCore.COMPILER_COMPLIANCE)和目标平台 (JavaCore.COMPILER\u CODEGEN\u TARGET\u平台)。如果没有这些选项,它就无法实现 代码格式化程序不可能知道它是哪种源代码 需要格式化


所以您需要这样做。

Ok Map options=JavaCore.getOptions();它需要map,所以我必须在map上设置值,然后传递给method?是的,类似这样的东西