好的,这是我的第一个Maven项目。这是我需要完成的最后一件事,但它不断使我回到这一点。
java.lang.IllegalStateException: source not specified
at org.eclipse.jdt.core.dom.ASTParser.createAST (ASTParser.java:830)
at com.javaAST.ASTParse.parse (ASTParse.java:25)
at com.javaAST.Main.main (Main.java:59)
at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
at jdk.internal.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke (Method.java:567)
at org.codehaus.mojo.exec.ExecJavaMojo$1.run (ExecJavaMojo.java:282)
at java.lang.Thread.run (Thread.java:830)
最终,它是来自此页面
package com.javaAST;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTParser;
import org.eclipse.jdt.core.dom.ASTVisitor;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.SimpleName;
import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
public class ASTParse
{
public static FileList fileList = new FileList();
public static ASTParser parser = ASTParser.newParser(AST.JLS3);
public static void parse(List<String> results) throws IOException
{
final CompilationUnit cu = (CompilationUnit) parser.createAST(null);
for (String result : results)
{
//Create output file
String addExtension = result.toLowerCase() + ".ast";
System.out.println("Creating File: " + addExtension);
File output = new File(addExtension);
output.createNewFile();
FileWriter fw = new FileWriter(output);
//Begin ASTParser
parser.setSource(result.toCharArray());
parser.setKind(ASTParser.K_COMPILATION_UNIT);
cu.accept(new ASTVisitor() {
Set<String> names = new HashSet<>();
public boolean visit(VariableDeclarationFragment node) {
SimpleName name = node.getName();
this.names.add(name.getIdentifier());
try
{
fw.write("Declaration of '" + name + "' at line" + cu.getLineNumber(name.getStartPosition()) + "\n");
} catch (IOException e)
{
e.printStackTrace();
}
return false; // do not continue to avoid usage info
}
public boolean visit(SimpleName node) {
if (this.names.contains(node.getIdentifier())) {
try
{
fw.write("Usage of '" + node + "' at line " + cu.getLineNumber(node.getStartPosition()) + "\n");
} catch (IOException e)
{
e.printStackTrace();
}
}
return true;
}
});
}
}
}
并且pom.xml
确实包含
<dependency>
<groupId>org.eclipse.jdt</groupId>
<artifactId>org.eclipse.jdt.core</artifactId>
<version>3.20.0</version>
</dependency>
,但我一直收到此错误。现在.jar
正在创建这个文件ASTParser.class
和ASTParser$1.class
,我还不完全了解,但是我认为不足以破坏它。有经验的人有什么指导吗?
答案 0 :(得分:1)
问题不是您的Maven设置。您的代码如何使用AST解析器存在错误。您在第25行获得了IllegalStateException
:
final CompilationUnit cu = (CompilationUnit) parser.createAST(null);
根据API specs for the Eclipse AST parser,在以下情况下会在createAST
上引发此异常:
IllegalStateException-如果提供的设置不足,矛盾或其他不受支持的
异常消息中还有一个线索:“未指定源”。您需要设置一个来源,大概是在调用parser.setSource()
之前 之前使用createAST
。