Question about closing ICompilationUnit? [message #255645] |
Thu, 14 August 2008 02:21 |
Eclipse User |
|
|
|
Originally posted by: k.mitov.xxx.xxx
Hi all. I have a problem with refreshing the content of an
ICompilationUnit as described in
https://bugs.eclipse.org/bugs/show_bug.cgi?id=237770.
With the following snippet
ICompilationUnit unit = JavaCore.createCompilationUnitFrom(file);
if (unit == null)
return;
IType rootType = unit.findPrimaryType();
if (rootType == null) {
rootType = forceCompilationUnitRefresh(file, unit);
}
I am trying to find the primary type in a file. The above snippet is
called in a resource change listener which is registered before JDT in the
platform resource change listeners. So when the snippet is executed JDT
hasn`t still processed the changed file and I think this is way
unit.findPrimaryType() returns "null". I have also noticed that in this
case
unit.getBuffer().getContents() and
unit.getSource() both return empty strings,
despite the fact that unit.isConsistent() returns "true".
So I had to find a way to directly refresh the compilation unit from the
file system and I have implemented the forceCompilationUnitRefresh()
method which looks like this:
private IType forceCompilationUnitRefresh(IFile file, ICompilationUnit
unit) throws JavaModelException {
unit.discardWorkingCopy();
unit.close();
unit = JavaCore.createCompilationUnitFrom(file);
return unit.findPrimaryType();
}
This method is currently working for me.
My questions is whether this is the right way to refresh the compilation
unit from the file content?
As stated in the javadoc of unit.close() it should not be used by clients,
but this is the only way I have managed to refresh the content.
Using unit.makeConsistent() does not work since unit.isConsistent()
returns true.
Using unit.reconcile() also does not refresh the compilation unit and its
buffer. Don`t know why.
Thanks in advance.
Best Regards,
Kiril
|
|
|
Powered by
FUDForum. Page generated in 0.03975 seconds