我实现了一个 java 插件,能够使用 Eclipse 快速修复修复计算机错误。要访问最新版本的输入程序,首先刷新在eclipse中打开的项目,然后调用eclipse quick fix来修复现有的编译器错误。 (输入程序重新更改,我需要先刷新项目,然后提取最新版本。)下面的代码(作为一种方法提供)被实现来完成所描述的工作。
//Get workspace
IWorkspace workspace = ResourcesPlugin.getWorkspace();
IWorkspaceRoot root = workspace.getRoot();
//Get project
IProject project = root.getProject(projectName);
//Referesh the project
project.refreshLocal(IResource.DEPTH_INFINITE, null);
IJobManager jobManager = Job.getJobManager();
jobManager.wakeUp(ResourcesPlugin.FAMILY_AUTO_BUILD);
jobManager.join(ResourcesPlugin.FAMILY_AUTO_BUILD, null);
IJavaProject javaProject = JavaCore.create(project);
//Get ICompilationUnit (classFullName is the full name of class that we need to find its compiler error)
IType iType = javaProject.findType(classFullName);
ICompilationUnit iUnit = iType.getCompilationUnit();
/** Create working copy. It is safer to work with a copy.*/
WorkingCopyOwner owner = iUnit.getOwner();
iUnit = (owner == null ? iUnit.getWorkingCopy(null) : iUnit.getWorkingCopy(owner, null));
//Get compilation Unit
ASTParser parser = ASTParser.newParser(AST.JLS8);
parser.setKind(ASTParser.K_COMPILATION_UNIT);
parser.setSource(iCompilationUnit);
parser.setResolveBindings(true);
CompilationUnit cu = (CompilationUnit) parser.createAST(null);
//Get compiler error using eclipse quick fix
for (IProblem iProblem : cu.getProblems()) {
//If it is an error
if (iProblem.isError()) {
int offset = iProblem.getSourceStart();
int length = iProblem.getSourceEnd() + 1 - offset;
IInvocationContext context = new AssistContext(iUnit , offset, length);
ProblemLocation problem = new ProblemLocation(iProblem);
//Extract Eclipse quick fix proposal.
ArrayList<IJavaCompletionProposal> proposals = new ArrayList<IJavaCompletionProposal>();
JavaCorrectionProcessor.collectCorrections(context, new IProblemLocation[] { problem }, proposals);
}}
最后,我们在 proposals 变量中有 eclipse 提案列表。该程序运行良好,直到今天我遇到了一个新的编译器错误。如果我有以下示例作为输入程序:
class Test {
public void foo(){
Scanner in = new Scanner(System.in);
}
}
当它想要考虑行 Scanner in = new Scanner(System.in);
!MESSAGE 从插件调用代码时出现问题:“org.eclipse.jdt.ui”。
!堆栈 1
Java 模型异常:在 org.eclipse.jdt.internal.core.JavaElement.newJavaModelException(JavaElement.java:544)
我调试程序,发现问题发生在 JavaCorrectionProcessor 类的方法 collectCorrections 中。
public static IStatus collectCorrections(IInvocationContext context, IProblemLocation[] locations, Collection<IJavaCompletionProposal> proposals) {
ContributedProcessorDescriptor[] processors= getCorrectionProcessors();
SafeCorrectionCollector collector= new SafeCorrectionCollector(context, proposals);
for (int i= 0; i < processors.length; i++) {
ContributedProcessorDescriptor curr= processors[i];
IProblemLocation[] handled= getHandledProblems(locations, curr);
if (handled != null) {
collector.setProblemLocations(handled);
collector.process(curr);
}
}
return collector.getStatus();
}
重要提示:我只针对特定类型的编译器错误收到此错误。例如,如果未定义 i 时 i = 0 这一行发生编译器错误,那么我会得到 unresolve 变量,程序会建议一个没有任何问题的解决方案。因此,我认为刷新部分应该可以正常工作,否则我应该得到程序中所有类型的编译器错误的错误。
最佳答案
我发现了问题。发生这种情况是因为运行时错误 Java 模型状态 [[Working copy] Test.java [in src [in SortExample]]] 不存在]
中所示的工作副本。在我评论以下几行之后,一切正常。
WorkingCopyOwner owner = iUnit.getOwner();
iUnit = (owner == null ? iUnit.getWorkingCopy(null) : iUnit.getWorkingCopy(owner, null));
正如我所说,问题发生在某些特定错误上。例如,当我在没有定义的情况下使用整数变量 (i = 0) 时,它不会发生,但是当一个类的实体在第二个类中使用时,它会发生,但它的 import 语句没有添加到第二个类中。
发生这种情况是因为使用的类没有添加到工作副本中,当 Eclipse 想要找到使用的类时,因为它不在工作副本中,它会抛出异常。然而,对于第一个错误 (i = 0) 没有任何反应,因为所有信息都可以从工作副本中获取。
关于java - 以编程方式调用 Eclipse 快速修复时出现运行时错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27986813/