gpt4 book ai didi

java - Maven:未指定来源...但为什么呢?

转载 作者:行者123 更新时间:2023-11-30 05:21:28 25 4
gpt4 key购买 nike

好的,这是我的第一个 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.classASTParser$1.class 我还没有完全理解,但我不知道我认为这还不足以打破它。有没有有经验的人指导一下?

最佳答案

问题不在于您的 Maven 设置。您的代码使用 AST 解析器的方式存在错误。您在第 25 行收到 IllegalStateException:

final CompilationUnit cu = (CompilationUnit) parser.createAST(null);

根据API specs for the Eclipse AST parser ,在以下情况下,createAST 会引发此异常:

IllegalStateException - if the settings provided are insufficient, contradictory, or otherwise unsupported

进一步的线索在异常消息中:“未指定源”。您需要在调用 createAST 之前使用 parser.setSource() 设置源。

关于java - Maven:未指定来源...但为什么呢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59524211/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com