user3601784
user3601784

Reputation: 377

refreshLocal in Eclipse does not work properly

I have a program that changes an input Java project loaded in Eclipse. After changes I use the below code to refresh the project and extract compilation unit.

IWorkspace workspace = ResourcesPlugin.getWorkspace();
IWorkspaceRoot root = workspace.getRoot();

//projectName is the name of project loaded in eclipse  
IProject project = root.getProject(projectName);

try {
    project.refreshLocal(IResource.DEPTH_INFINITE, null);
 } catch (CoreException e) {
        e.printStackTrace();
}

IJavaProject iJavaproject = JavaCore.create(project);

/** Extract ICompilationUnit. "classFullName" is the name of class contains new changes.*/
ICompilationUnit iCompilationUnit = getICompilationUnit(javaProject, classFullName);

/** Extract compilation unit.*/
CompilationUnit compilationUnit = getCompilationUnit(iCompilationUnit);

I have these two functions to extract iCompilationUnit and compilationUnit.

private ICompilationUnit getICompilationUnit(IJavaProject javaProject, String classFullName) {

    ICompilationUnit iUnit = null;
    try {
        IType iType = javaProject.findType(classFullName);
        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)); 
    } catch (JavaModelException e) {
        e.printStackTrace();
    }

    return iUnit;
}

CompilationUnit getCompilationUnit(ICompilationUnit iCompilationUnit) {

    @SuppressWarnings("deprecation")
    ASTParser parser = ASTParser.newParser(AST.JLS3);
    parser.setKind(ASTParser.K_COMPILATION_UNIT);
    parser.setSource(iCompilationUnit);

    /** we need bindings later on.*/
    parser.setResolveBindings(true); 
    return (CompilationUnit) parser.createAST(null);
}

However, the problem I am facing is that first call to this method (first time that any changes is applied to the project), the above code cannot detect changes and return original version. However, after that the project is refreshed correctly and final compilationUnit contains applied changes.

I am not sure the problem is for refreshLocal and maybe it is for other two functions: getCompilationUnit and getICompilationUnit.

Please let me know if any one has any idea.

Upvotes: 1

Views: 667

Answers (1)

greg-449
greg-449

Reputation: 111216

I think the JDT is probably running background jobs to recompile and rebuild indices. So you need to wait for those jobs to finish. Try

IJobManager jobManager = Job.getJobManager();

jobManager.join(ResourcesPlugin.FAMILY_MANUAL_BUILD, monitor);

jobManager.join(ResourcesPlugin.FAMILY_AUTO_BUILD, monitor);

do this after the refreshLocal but before you do anything else.

Upvotes: 1

Related Questions