3939import org .eclipse .core .runtime .IPath ;
4040import org .eclipse .core .runtime .IProgressMonitor ;
4141import org .eclipse .core .runtime .IStatus ;
42+ import org .eclipse .core .runtime .OperationCanceledException ;
4243import org .eclipse .core .runtime .Status ;
4344import org .eclipse .core .runtime .jobs .Job ;
4445
7374import org .eclipse .ui .texteditor .ITextEditorActionConstants ;
7475
7576import org .eclipse .jdt .core .ClasspathContainerInitializer ;
77+ import org .eclipse .jdt .core .ElementChangedEvent ;
7678import org .eclipse .jdt .core .IClassFile ;
7779import org .eclipse .jdt .core .IClasspathAttribute ;
7880import org .eclipse .jdt .core .IClasspathContainer ;
7981import org .eclipse .jdt .core .IClasspathEntry ;
82+ import org .eclipse .jdt .core .IElementChangedListener ;
8083import org .eclipse .jdt .core .IJavaElement ;
8184import org .eclipse .jdt .core .IJavaModelStatusConstants ;
8285import 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