首页 > 解决方案 > 如何解决 JLS3 无法解决或不是 Eclipse 中的字段错误?

问题描述

import java.util.Map;

import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.dom.ASTParser;
import org.eclipse.jdt.core.dom.*;

public class AST {
    public static void maain(String arge[]) {
        String str = "someString"; 
        char[] source = str.toCharArray();
     ASTParser parser = ASTParser.newParser(AST.JLS3);  // handles JDK 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6
     parser.setSource(source);
     // In order to parse 1.5 code, some compiler options need to be set to 1.5
     Map options = JavaCore.getOptions();
     JavaCore.setComplianceOptions(JavaCore.VERSION_1_5, options);
     parser.setCompilerOptions(options);
     CompilationUnit result = (CompilationUnit) parser.createAST(null);
}
}

我正在尝试在 Eclipse 中使用 AST PARSER 解析 java 代码。如何解决“JLS3 无法解析或不是字段”错误?

标签: javaeclipse

解决方案


您已经调用了您的类AST,因此编译器正在JLS3您的AST类中查找,而不是 JDTorg.eclipse.jdt.core.dom.AST类。

您可以将您的AST类重命名为不冲突的名称,也可以使用 JDT 类的全名:

ASTParser.newParser(org.eclipse.jdt.core.dom.AST.JLS3);

推荐阅读