以编程方式调用eclipse快速修复时运行时错误

时间:2015-01-16 15:02:11

标签: java eclipse-plugin runtime-error

我实现了一个java插件,能够使用Eclipse快速修复修复comiper错误。要访问输入程序的最新版本,首先刷新在eclipse中打开的项目,然后调用eclipse快速修复来修复现有的编译器错误。 (输入程序经常更改,我需要首先刷新项目,然后提取最后一个版本。)以下代码(作为一种方法提供)用于执行所描述的工作。

//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);        
 }}

最后,我们在提案变量中列出了eclipse提案。该程序工作正常,直到今天我遇到了新的编译器错误。如果我将以下示例作为输入程序:

class Test {
   public void foo(){
      Scanner in = new Scanner(System.in);
   }
 }

当我想考虑行Scanner in = new Scanner(System.in);

的提案时,我收到以下运行时错误

!MESSAGE Problems occurred when invoking code from plug-in: "org.eclipse.jdt.ui". !STACK 1 Java Model Exception: Java Model Status [[Working copy] Test.java [in src [in SortExample]]] does not exist] at 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 = 0 发生编译器错误,而未定义i,则我得到unresolve变量,程序建议解决任何问题。因此,我认为刷新部分应该可以正常工作,否则我应该在程序中得到所有类型的编译器错误的错误。

1 个答案:

答案 0 :(得分:1)

我发现了问题。这是因为运行时错误Java Model Status [[Working copy] Test.java [in src [in SortExample]]] does not exist]中显示的工作副本。在我评论下面的行后,每件事情都很好。

WorkingCopyOwner owner = iUnit.getOwner();
iUnit = (owner == null ? iUnit.getWorkingCopy(null) : iUnit.getWorkingCopy(owner, null)); 

正如我所说,问题发生在一些特定的错误上。例如,当我使用整数变量(i = 0)而没有定义它时,它不会发生,但它发生在第二个类中使用类的实体时,但它的import语句没有添加到第二个类。

之所以发生这种情况,是因为使用的类没有添加到工作副本中,并且当eclipse想要找到所使用的类时,因为它不在工作副本中,因此会引发异常。但是,对于第一个错误(i = 0),没有任何事情发生,因为所有信息都可以从工作副本中获得。