Eclipse代码不能为Java泛型代码工作? [英] Eclipse code fomatter not working for Java Generics code?

查看:110
本文介绍了Eclipse代码不能为Java泛型代码工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using eclipse code format jar file to format java code and used below maven dependecy

<dependency>
    <groupId>org.eclipse.jdt</groupId>
    <artifactId>org.eclipse.jdt.core</artifactId>
    <version>3.7.1</version>  
</dependency>

当我试图格式化下面的代码

And when i am trying to format the below code

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();
        }
    }
}

但eclipse代码格式不能当我添加泛型时格式化代码。任何人都可以告诉这个问题的解决方法。

But eclipse code format not able to format code when i added generics . Can anyone tell what will be solution of this problem.

推荐答案

您没有将任何源级别或合规级别选项指定为 ToolFactory.createCodeFormatter 所以你可能会得到一个格式化程序,它只支持原始的Java,没有泛型。

You are not specifying any source level or compliance level options to ToolFactory.createCodeFormatter so you are probably getting a formatter that only supports the original Java without generics.

JavaDoc for ToolFactory.createCodeFormatter 说:

The JavaDoc for ToolFactory.createCodeFormatter says:


给定的选项应至少提供源级
(JavaCore.COMPILER_SOURCE),编译器合规性级别
(JavaCore.COMPILER_COMPLIANCE)和目标平台
(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM)。没有这些选项,它
是不可能的代码格式化程序知道什么样的源
需要格式化。

The given options should at least provide the source level (JavaCore.COMPILER_SOURCE), the compiler compliance level (JavaCore.COMPILER_COMPLIANCE) and the target platform (JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM). Without these options, it is not possible for the code formatter to know what kind of source it needs to format.

所以你需要这样做。

这篇关于Eclipse代码不能为Java泛型代码工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆