Eclipse code fomatter not working for Java Generics code? -
I am using the eclipse code format jar file formatted Java code and Maven dependence
& lt; Dependency & gt; & Lt; Group & gt; Org.eclipse.jdt & lt; / Group & gt; & Lt; ArtifactId & gt; Org.eclipse.jdt.core & lt; / ArtifactId> & Lt; Version & gt; 3.7.1 & lt; / Edition & gt; & Lt; / Dependencies & gt;
And when I'm trying to format the code below
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.documentation; Import org.eclipse.jface.text.idocument; Import org.eclipse.text.edits.MalformedTreeException; Import org.eclipse.text.edits.TextEdit; Public class FormatterTest {public static void (String [] args) {string code = "import java.util.Map; public class TestFormatter {public static void (String [] args) {Map & lt; string, object & gt; map = Null; System.out.println (\ "Hello World \");}} "; CodeFormer CodeFormer = ToolFactory .createCodeFormatter (empty); TextEdit textEdit = codeFormatter.format (CodeFormatter.K_COMPILATION_UNIT, code, 0, code.length (), 0, zero); ID document doc = new document (code); {Try TextEdit.apply (doc); Println (doc.get ()); } Grip (Deformed tree exception e) {e.printStackTrace (); } Catch (Badlocation Exposition E) {e.printStackTrace (); }}}
But the eclipse code format when I was added generic, I was not able to format the code. Anyone can tell what will solve this problem.
You are not specifying any source level or compliance level. The option for ToolFactory.createCodeFormatter
is probably a formatter that only supports basic Java without generics.
ToolFactory.createCodeFormatter
For JavaDoc says:
At least source level should be provided (JavaCore.COMPILER_SOURCE), compiler compliance level (JavaCore.COMPILER_COMPLIANCE) and target platform (JavaCore. COMPILER_CODEGEN_TARGET_PLATFORM). Without these options, it is not possible to know the code formatter that type of source should be formatted.
So you need to do this.
Comments
Post a Comment