Eclipse JDT ASTParser 错误地转换枚举声明节点 [英] Eclipse JDT ASTParser convert enum declaration node incorrectly

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

问题描述

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

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天全站免登陆