Eclipse JDT ASTParser incorrectly converts enum node declaration

I am working on parsing Java code using JDT and am going to create a standalone parsing tool, dependent on the org.eclipse.jdt.core package instead of the eclipse plugin. But I found that my tool does not work correctly in the enum node declaration, which appeared in Java code. In my AST, which was created by jdt, the enum keyword was considered typename instead of the enum declaration. Therefore, I want to know how I must be sure that my tool can correctly process an enumeration declaration.

The jdt package I use is "org.eclipse.jdt.core_3.8.3.v20130121-145325.jar". CreateAST code:

char[] javaprogram=getJavaFile(javaFileName);
ASTParser parser = ASTParser.newParser(AST.JLS4);
parser.setSource(javaprogram);
parser.setKind(ASTParser.K_COMPILATION_UNIT);
final CompilationUnit cu = (CompilationUnit) parser.createAST(null);

java input looks like this:

package test;

enum Color
{
  RED(255, 0, 0),  BLUE(0, 0, 255),  BLACK(0, 0, 0),    YELLOW(255, 255, 0),  GREEN(0, 255, 0);

  private int redValue;
  private int greenValue;
  private int blueValue;

  private Color(int rv, int gv, int bv)
  {
    this.redValue = rv;
    this.greenValue = gv;
    this.blueValue = bv;
  }

  public String toString()
  {
    return super.toString() + "(" + this.redValue + "," + this.greenValue + "," + this.blueValue + ")";
  }
}

astparser.createAST(), CompilationUnit node, , :

package test;

CompilerOptions, :

Map options = JavaCore.getOptions();
options.put(JavaCore.COMPILER_COMPLIANCE, JavaCore.VERSION_1_5);
options.put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, JavaCore.VERSION_1_5);
options.put(JavaCore.COMPILER_SOURCE, JavaCore.VERSION_1_5);
parser.setCompilerOptions(options);
+4
1

, , 1,3

Map options = JavaCore.getOptions();
System.out.println(options.get(JavaCore.COMPILER_SOURCE)); //outputs 1.3

( ) enum ​​ 1.5, 1.5 . , COMPILER_SOURCE

Map options = JavaCore.getOptions();
options.put(JavaCore.COMPILER_SOURCE, JavaCore.VERSION_1_5); //or newer version
parser.setCompilerOptions(options);
+3

Source: https://habr.com/ru/post/1528022/


All Articles