英文:
Strange result of eclipse AST with inner enum class
问题
以下是翻译好的内容:
我正在尝试使用 Eclipse JDT AST 进行实验,但遇到了一个奇怪的行为,我找不到任何解释。
这是我的示例代码:
public static void main(String[] args) {
StringBuilder content = new StringBuilder();
content.append("class Foo {");
content.append(" enum Bar {");
content.append(" VALUE;");
content.append(" int getValue() {");
content.append(" return 4;");
content.append(" }");
content.append(" }");
content.append(" int getValue() {");
content.append(" return 42;");
content.append(" }");
content.append("}");
ASTParser parser = ASTParser.newParser(AST.JLS13);
parser.setKind(ASTParser.K_COMPILATION_UNIT);
parser.setSource(content.toString().toCharArray());
CompilationUnit astNode = (CompilationUnit) parser.createAST(null);
Visitor rtVisitor = new Visitor();
astNode.accept(rtVisitor);
}
private static class Visitor extends ASTVisitor {
@Override
public boolean visit(TypeDeclaration node) {
System.out.println(node);
return super.visit(node);
}
}
正如你所见,我定义了一个非常简单的示例类,其中包含一个内部枚举类,两个类都具有相同签名的方法。
但奇怪的是,此代码的输出(即解析的 TypeDeclaration
)是:
class Foo {
enum Bar;
{
}
int getValue(){
return 4;
}
{
}
int getValue(){
return 42;
}
}
由某种原因,TypeDeclaration
的主体包括:
FieldDeclaration
:enum Bar;
Initializer
:{}
MethodDeclaration
:int getValue(){ return 4; }
- 另一个
Initializer
:{}
- 另一个
MethodDeclaration
:int getValue(){ return 42; }
这导致我的实际代码抛出错误,因为看起来存在两个具有相同签名的方法。
为什么枚举不是实际的 EnumDeclaration
,而是看起来枚举内部的方法实际上是在外部类中声明的?
我认为这不是一个 bug,因为 Eclipse 中的 AST 视图可以很好地处理类似的类,但我无法弄清楚我做错了什么。启用绑定解析也没有帮助。
英文:
I am experimenting with eclipse jdt AST and am running into a strange behaviour that I can find no explanation for.
Here is my example code:
public static void main(String[] args) {
StringBuilder content = new StringBuilder();
content.append("class Foo {");
content.append(" enum Bar {");
content.append(" VALUE;");
content.append(" int getValue() {");
content.append(" return 4;");
content.append(" }");
content.append(" }");
content.append(" int getValue() {");
content.append(" return 42;");
content.append(" }");
content.append("}");
ASTParser parser = ASTParser.newParser(AST.JLS13);
parser.setKind(ASTParser.K_COMPILATION_UNIT);
parser.setSource(content.toString().toCharArray());
CompilationUnit astNode = (CompilationUnit) parser.createAST(null);
Visitor rtVisitor = new Visitor();
astNode.accept(rtVisitor);
}
private static class Visitor extends ASTVisitor {
@Override
public boolean visit(TypeDeclaration node) {
System.out.println(node);
return super.visit(node);
}
}
As you can see, I am defining a very simple example class that has an inner enum class where both classes have a method with the same signature.
Strangely though the output of this code (i.e. the parsed TypeDeclaration
) is
class Foo {
enum Bar;
{
}
int getValue(){
return 4;
}
{
}
int getValue(){
return 42;
}
}
For some reason, the body of the TypeDeclaration
consists of:
- a
FieldDeclaration
:enum Bar;
- an
Initializer
:{}
- a
MethodDeclaration
:int getValue(){ return 4; }
- another
Initializer
:{}
- another
MethodDeclaration
:int getValue(){ return 42; }
This leads to my actual code throwing an error because it looks like there are two methods with identical signature.
Why am I not getting the enum as an actual EnumDeclaration
with inner methods but rather it looks like the method inside the enum is actually declared in the outer class itself?
I do not think that this is a bug because the AST View in eclipse handles a similar class perfectly fine, but I cannot figure out what I am doing wrong. Enabling binding resolution did not help.
答案1
得分: 2
你需要通过调用 parser.setCompilerOptions
来设置编译器选项,以便正确处理源文件。<br>
由于你正在使用 enum
关键字,你至少需要使用 Java 5 兼容性:
ASTParser parser = ASTParser.newParser(AST.JLS13);
Map options = JavaCore.getOptions();
JavaCore.setComplianceOptions(JavaCore.VERSION_1_5, options);
parser.setCompilerOptions(options);
parser.setKind(ASTParser.K_COMPILATION_UNIT);
parser.setSource(content.toString().toCharArray());
英文:
You need to set compiler options by calling parser.setCompilerOptions
, so that the source file is processed correctly.<br>
Since you are using the enum
keyword, you need at least Java 5 compliance:
ASTParser parser = ASTParser.newParser(AST.JLS13);
Map options = JavaCore.getOptions();
JavaCore.setComplianceOptions(JavaCore.VERSION_1_5, options);
parser.setCompilerOptions(options);
parser.setKind(ASTParser.K_COMPILATION_UNIT);
parser.setSource(content.toString().toCharArray());
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论