Eclipse JDT ASTParser转换枚举声明节点不正确 [英] Eclipse JDT ASTParser convert enum declaration node incorrectly

查看:151
本文介绍了Eclipse JDT ASTParser转换枚举声明节点不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JDT分析Java代码,并依赖于org.eclipse.jdt.core包而不是eclipse插件构建独立的分析工具。但是我发现我的工具在Java代码中出现的枚举声明节点上工作不正常。在由jdt创建的AST中,关键字枚举被认为是一个类型名称而不是枚举声明。所以我想知道我应该如何确保我的工具可以正确地处理枚举声明。

I am working on analyzing Java code by using JDT and going to build a standalone analysis tool depend on org.eclipse.jdt.core package instead of an eclipse plug-in. But I found that my tool did not work correctly on enum declaration node which appeared in Java code. In my AST which created by jdt, keyword enum was regarded as a typename instead of an enum declaration. So I want to know how I should be can ensure that my tool can deal the enum declaration correctly.

我使用的jdt包是org.eclipse.jdt。 core_3.8.3.v20130121-145325.jar。
createAST代码是:

The jdt package that I used is "org.eclipse.jdt.core_3.8.3.v20130121-145325.jar". The createAST code is:

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输入如下:

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节点刚刚得到的代码只包含包代码:

But using astparser.createAST() to get CompilationUnit node just got the code which is just contained the package code:

package test;






问题是通过添加CompilerOptions来解决的如下所示:


The problem is solved by adding the CompilerOptions which code is shown below:

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


推荐答案

当您回答自己时,您必须将编译器版本设置为一个较新的一个,因为默认值是1.3

As you answered yourself you have to set compiler version to a newer one because the default one is 1.3

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

但是(我认为)枚举声明只添加在1.5中,所以你必须设置它到1.5以上。另外我相信只需设置 COMPILER_SOURCE

However (I think) enum declaration was added only in 1.5 so you have to set it to 1.5 or above. Also I believe it's enough to set only COMPILER_SOURCE

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

这篇关于Eclipse JDT ASTParser转换枚举声明节点不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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