Skip to content

Commit dffd4e3

Browse files
committed
Load and wait in a job + support no source in the meanwhile
1 parent 5458ce8 commit dffd4e3

2 files changed

Lines changed: 61 additions & 48 deletions

File tree

org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/javaeditor/ClassFileDocumentProvider.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ public ClassFileDocumentProvider() {
209209
protected boolean setDocumentContent(IDocument document, IEditorInput editorInput, String encoding) throws CoreException {
210210
if (editorInput instanceof IClassFileEditorInput) {
211211
IClassFile classFile= ((IClassFileEditorInput) editorInput).getClassFile();
212-
String source= classFile.getSource();
212+
String source= getSourceIfPresent(classFile);
213213
if (source == null)
214214
source= ""; //$NON-NLS-1$
215215
document.set(source);
@@ -218,6 +218,15 @@ protected boolean setDocumentContent(IDocument document, IEditorInput editorInpu
218218
return super.setDocumentContent(document, editorInput, encoding);
219219
}
220220

221+
private String getSourceIfPresent(IClassFile classFile) {
222+
try {
223+
return classFile.getSource();
224+
} catch (JavaModelException e) {
225+
//Assume no source...
226+
return null;
227+
}
228+
}
229+
221230
/**
222231
* Creates an annotation model derived from the given class file editor input.
223232
*

org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/javaeditor/ClassFileEditor.java

Lines changed: 51 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import org.eclipse.core.runtime.IPath;
4040
import org.eclipse.core.runtime.IProgressMonitor;
4141
import org.eclipse.core.runtime.IStatus;
42+
import org.eclipse.core.runtime.OperationCanceledException;
4243
import org.eclipse.core.runtime.Status;
4344
import org.eclipse.core.runtime.jobs.Job;
4445

@@ -73,10 +74,12 @@
7374
import org.eclipse.ui.texteditor.ITextEditorActionConstants;
7475

7576
import org.eclipse.jdt.core.ClasspathContainerInitializer;
77+
import org.eclipse.jdt.core.ElementChangedEvent;
7678
import org.eclipse.jdt.core.IClassFile;
7779
import org.eclipse.jdt.core.IClasspathAttribute;
7880
import org.eclipse.jdt.core.IClasspathContainer;
7981
import org.eclipse.jdt.core.IClasspathEntry;
82+
import org.eclipse.jdt.core.IElementChangedListener;
8083
import org.eclipse.jdt.core.IJavaElement;
8184
import org.eclipse.jdt.core.IJavaModelStatusConstants;
8285
import org.eclipse.jdt.core.IJavaProject;
@@ -644,21 +647,45 @@ public boolean isEditorInputReadOnly() {
644647
protected IEditorInput transformEditorInput(IEditorInput input) throws CoreException{
645648
if (input instanceof HandleEditorInput handle) {
646649
IJavaElement element= handle.getElement();
647-
if (!element.exists() && element instanceof IOrdinaryClassFile) {
648-
/*
649-
* Let's try to find the class file,
650-
* see https://bugs.eclipse.org/bugs/show_bug.cgi?id=83221
651-
*/
652-
IOrdinaryClassFile cf= (IOrdinaryClassFile)element;
653-
IType type= cf.getType();
654-
IJavaProject project= element.getJavaProject();
655-
if (project != null) {
656-
type= project.findType(type.getFullyQualifiedName());
657-
if (type == null)
658-
return null;
659-
element= type.getParent();
650+
new Job("Load in the background") { //$NON-NLS-1$
651+
652+
@Override
653+
protected IStatus run(IProgressMonitor monitor) {
654+
try {
655+
Job.getJobManager().join(ClasspathContainerInitializer.class, null);
656+
} catch (OperationCanceledException | InterruptedException e) {
657+
// Ignore
658+
}
659+
try {
660+
if (!element.exists() && element instanceof IOrdinaryClassFile) {
661+
/*
662+
* Let's try to find the class file,
663+
* see https://bugs.eclipse.org/bugs/show_bug.cgi?id=83221
664+
*/
665+
IOrdinaryClassFile cf= (IOrdinaryClassFile)element;
666+
IType type= cf.getType();
667+
IJavaProject project= element.getJavaProject();
668+
if (project != null) {
669+
type= project.findType(type.getFullyQualifiedName());
670+
if (type == null) {
671+
JavaCore.addElementChangedListener(new IElementChangedListener() {
672+
@Override
673+
public void elementChanged(ElementChangedEvent e) {
674+
JavaCore.removeElementChangedListener(this);
675+
schedule();
676+
}
677+
});
678+
} else {
679+
setInput(EditorUtility.getEditorInput(type.getParent()));
680+
}
681+
}
682+
}
683+
} catch (CoreException e) {
684+
return e.getStatus();
685+
}
686+
return Status.OK_STATUS;
660687
}
661-
}
688+
}.schedule();
662689
input= EditorUtility.getEditorInput(element);
663690
}
664691
if (input instanceof IFileEditorInput) {
@@ -694,21 +721,6 @@ private void doSetInputCached(IEditorInput input) throws CoreException {
694721
message,
695722
null));
696723
}
697-
698-
JavaModelException e= probeInputForSource(input);
699-
if (e != null) {
700-
IClassFileEditorInput classFileEditorInput= (IClassFileEditorInput) input;
701-
IClassFile file= classFileEditorInput.getClassFile();
702-
IJavaProject javaProject= file.getJavaProject();
703-
if (!javaProject.exists() || !javaProject.isOnClasspath(file)) {
704-
throw new CoreException(JavaUIStatus.createError(
705-
IJavaModelStatusConstants.INVALID_RESOURCE,
706-
JavaEditorMessages.ClassFileEditor_error_classfile_not_on_classpath,
707-
null));
708-
}
709-
throw e;
710-
}
711-
712724
IDocumentProvider documentProvider= getDocumentProvider();
713725
if (documentProvider instanceof ClassFileDocumentProvider) {
714726
((ClassFileDocumentProvider) documentProvider).removeInputChangeListener(this);
@@ -799,23 +811,6 @@ private void createPartControlCached(Composite parent) {
799811
}
800812
}
801813

802-
private JavaModelException probeInputForSource(IEditorInput input) {
803-
if (input == null) {
804-
return null;
805-
}
806-
807-
IClassFileEditorInput classFileEditorInput= (IClassFileEditorInput) input;
808-
IClassFile file= classFileEditorInput.getClassFile();
809-
810-
try {
811-
file.getSourceRange();
812-
} catch (JavaModelException e) {
813-
return e;
814-
}
815-
816-
return null;
817-
}
818-
819814
/**
820815
* Checks if the class file input has no source attached. If so, a source attachment form is shown.
821816
*
@@ -836,7 +831,7 @@ private void verifyInput(IEditorInput input) throws JavaModelException {
836831
boolean wasUsingSourceCopyAction= fSourceCopyAction == getAction(ITextEditorActionConstants.COPY);
837832

838833
// show source attachment form if no source found
839-
if (file.getSourceRange() == null) {
834+
if (!hasSource(file)) {
840835
// dispose old source attachment form
841836
if (fSourceAttachmentForm != null) {
842837
fSourceAttachmentForm.dispose();
@@ -925,6 +920,15 @@ public void run() {
925920
}
926921
}
927922

923+
private static boolean hasSource(IClassFile file) {
924+
try {
925+
return file.getSourceRange() != null;
926+
} catch (JavaModelException e) {
927+
//assume no source then...
928+
return false;
929+
}
930+
}
931+
928932
/*
929933
* @see ClassFileDocumentProvider.InputChangeListener#inputChanged(IClassFileEditorInput)
930934
*/

0 commit comments

Comments
 (0)