From 838b7c873c17f8baeafaf61d621bda8a0c13c850 Mon Sep 17 00:00:00 2001 From: Stephan Herrmann Date: Tue, 2 Jul 2024 20:46:22 +0200 Subject: [PATCH] AnnotateAssistTest1d8 test failed (#1486) explicitly request a ClassfileEditor 2nd attempt at https://github.com/eclipse-jdt/eclipse.jdt.ui/issues/736 --- .../quickfix/AbstractAnnotateAssistTests.java | 15 +++++++++ .../tests/quickfix/AnnotateAssistTest1d8.java | 32 +++++++++---------- .../internal/ui/javaeditor/EditorUtility.java | 11 +++++-- 3 files changed, 40 insertions(+), 18 deletions(-) diff --git a/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/AbstractAnnotateAssistTests.java b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/AbstractAnnotateAssistTests.java index f0bbaefa768..87c003c3209 100644 --- a/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/AbstractAnnotateAssistTests.java +++ b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/AbstractAnnotateAssistTests.java @@ -41,6 +41,8 @@ import org.eclipse.jface.text.contentassist.ICompletionProposal; import org.eclipse.jface.text.quickassist.IQuickAssistInvocationContext; +import org.eclipse.ui.IEditorPart; +import org.eclipse.ui.PartInitException; import org.eclipse.ui.part.FileEditorInput; import org.eclipse.jdt.core.IClassFile; @@ -49,6 +51,7 @@ import org.eclipse.jdt.core.ICompilationUnit; import org.eclipse.jdt.core.IJavaElement; import org.eclipse.jdt.core.IJavaProject; +import org.eclipse.jdt.core.ISourceReference; import org.eclipse.jdt.core.JavaCore; import org.eclipse.jdt.core.WorkingCopyOwner; @@ -59,6 +62,7 @@ import org.eclipse.jdt.ui.tests.quickfix.JarUtil.ClassFileFilter; import org.eclipse.jdt.internal.ui.javaeditor.ClassFileEditor; +import org.eclipse.jdt.internal.ui.javaeditor.EditorUtility; import org.eclipse.jdt.internal.ui.javaeditor.IClassFileEditorInput; import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor; import org.eclipse.jdt.internal.ui.javaeditor.JavaSourceViewer; @@ -89,6 +93,17 @@ protected void ensureExists(IContainer parent) throws CoreException { ((IFolder)parent).create(true, true, null); } + static JavaEditor openInClassfileEditor(IJavaElement element, boolean activate, boolean reveal) throws PartInitException { + if (!(element instanceof ISourceReference)) { + return null; + } + IEditorPart part= EditorUtility.openInSpecificEditor(element, JavaUI.ID_CF_EDITOR, activate); + if (reveal && part != null) { + EditorUtility.revealInEditor(part, element); + } + return (JavaEditor) part; + } + public List collectAnnotateProposals(JavaEditor javaEditor, int offset) throws CoreException { if (javaEditor instanceof ClassFileEditor) { JavaSourceViewer viewer= (JavaSourceViewer)javaEditor.getViewer(); diff --git a/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/AnnotateAssistTest1d8.java b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/AnnotateAssistTest1d8.java index c97b561e697..ca5e6ef2a20 100644 --- a/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/AnnotateAssistTest1d8.java +++ b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/AnnotateAssistTest1d8.java @@ -196,7 +196,7 @@ class pack/age/X annotationFile.create(new ByteArrayInputStream(initialContent.getBytes("UTF-8")), 0, null); IType type= fJProject1.findType(X_PATH.replace('/', '.')); - JavaEditor javaEditor= (JavaEditor) JavaUI.openInEditor(type); + JavaEditor javaEditor= openInClassfileEditor(type, true, true); try { int offset= pathAndContents[1].indexOf("String> list"); @@ -261,7 +261,7 @@ public interface X { }; addLibrary(fJProject1, "lib.jar", "lib.zip", pathAndContents, ANNOTATION_PATH, JavaCore.VERSION_1_8, null); IType type= fJProject1.findType(X_PATH.replace('/', '.')); - JavaEditor javaEditor= (JavaEditor) JavaUI.openInEditor(type); + JavaEditor javaEditor= openInClassfileEditor(type, true, true); try { int offset= pathAndContents[1].indexOf("int[]"); @@ -309,7 +309,7 @@ class pack/age/X annotationFile.create(new ByteArrayInputStream(initialContent.getBytes("UTF-8")), 0, null); IType type= fJProject1.findType(X_PATH.replace('/', '.')); - JavaEditor javaEditor= (JavaEditor) JavaUI.openInEditor(type); + JavaEditor javaEditor= openInClassfileEditor(type, true, true); try { int offset= pathAndContents[1].indexOf("Number> list"); @@ -386,7 +386,7 @@ public interface X { annotationFile.create(new ByteArrayInputStream(initialContent.getBytes("UTF-8")), 0, null); IType type= fJProject1.findType(X_PATH.replace('/', '.')); - JavaEditor javaEditor= (JavaEditor) JavaUI.openInEditor(type); + JavaEditor javaEditor= openInClassfileEditor(type, true, true); try { int offset= pathAndContents[1].indexOf("[] ints"); @@ -467,7 +467,7 @@ class pack/age/X annotationFile.create(new ByteArrayInputStream(initialContent.getBytes("UTF-8")), 0, null); IType type= fJProject1.findType(X_PATH.replace('/', '.')); - JavaEditor javaEditor= (JavaEditor) JavaUI.openInEditor(type); + JavaEditor javaEditor= openInClassfileEditor(type, true, true); try { int offset= pathAndContents[1].indexOf("[] ints"); @@ -548,7 +548,7 @@ class pack/age/X annotationFile.create(new ByteArrayInputStream(initialContent.getBytes("UTF-8")), 0, null); IType type= fJProject1.findType(X_PATH.replace('/', '.')); - JavaEditor javaEditor= (JavaEditor) JavaUI.openInEditor(type); + JavaEditor javaEditor= openInClassfileEditor(type, true, true); try { int offset= pathAndContents[1].indexOf("[],"); @@ -629,7 +629,7 @@ class pack/age/X annotationFile.create(new ByteArrayInputStream(initialContent.getBytes("UTF-8")), 0, null); IType type= fJProject1.findType(X_PATH.replace('/', '.')); - JavaEditor javaEditor= (JavaEditor) JavaUI.openInEditor(type); + JavaEditor javaEditor= openInClassfileEditor(type, true, true); try { int offset= pathAndContents[1].indexOf("[][][],"); @@ -708,7 +708,7 @@ class pack/age/X annotationFile.create(new ByteArrayInputStream(initialContent.getBytes("UTF-8")), 0, null); IType type= fJProject1.findType(X_PATH.replace('/', '.')); - JavaEditor javaEditor= (JavaEditor) JavaUI.openInEditor(type); + JavaEditor javaEditor= openInClassfileEditor(type, true, true); try { int offset= pathAndContents[1].indexOf("[] test"); @@ -787,7 +787,7 @@ class pack/age/X annotationFile.create(new ByteArrayInputStream(initialContent.getBytes("UTF-8")), 0, null); IType type= fJProject1.findType(X_PATH.replace('/', '.')); - JavaEditor javaEditor= (JavaEditor) JavaUI.openInEditor(type); + JavaEditor javaEditor= openInClassfileEditor(type, true, true); try { int offset= pathAndContents[1].indexOf("[];"); @@ -866,7 +866,7 @@ class pack/age/X annotationFile.create(new ByteArrayInputStream(initialContent.getBytes("UTF-8")), 0, null); IType type= fJProject1.findType(X_PATH.replace('/', '.')); - JavaEditor javaEditor= (JavaEditor) JavaUI.openInEditor(type); + JavaEditor javaEditor= openInClassfileEditor(type, true, true); try { int offset= pathAndContents[1].indexOf("..."); @@ -947,7 +947,7 @@ class pack/age/X annotationFile.create(new ByteArrayInputStream(initialContent.getBytes("UTF-8")), 0, null); IType type= fJProject1.findType(X_PATH.replace('/', '.')); - JavaEditor javaEditor= (JavaEditor) JavaUI.openInEditor(type); + JavaEditor javaEditor= openInClassfileEditor(type, true, true); try { int offset= pathAndContents[1].indexOf("..."); @@ -1026,7 +1026,7 @@ class pack/age/X annotationFile.create(new ByteArrayInputStream(initialContent.getBytes("UTF-8")), 0, null); IType type= fJProject1.findType(X_PATH.replace('/', '.')); - JavaEditor javaEditor= (JavaEditor) JavaUI.openInEditor(type); + JavaEditor javaEditor= openInClassfileEditor(type, true, true); try { int offset= pathAndContents[1].indexOf("String ..."); @@ -1094,7 +1094,7 @@ public interface X { addLibrary(fJProject1, "lib.jar", "lib.zip", pathAndContents, ANNOTATION_PATH, JavaCore.VERSION_1_8, null); IType type= fJProject1.findType(X_PATH.replace('/', '.')); - JavaEditor javaEditor= (JavaEditor) JavaUI.openInEditor(type); + JavaEditor javaEditor= openInClassfileEditor(type, true, true); try { int offset= pathAndContents[1].indexOf("T extends"); @@ -1201,7 +1201,7 @@ public interface X > { addLibrary(fJProject1, "lib.jar", "lib.zip", pathAndContents, ANNOTATION_PATH, JavaCore.VERSION_1_8, null); IType type= fJProject1.findType(X_PATH.replace('/', '.')); - JavaEditor javaEditor= (JavaEditor) JavaUI.openInEditor(type); + JavaEditor javaEditor= openInClassfileEditor(type, true, true); try { int offset= pathAndContents[1].indexOf("T extends"); @@ -1304,7 +1304,7 @@ public class Node { }; addLibrary(fJProject1, "lib.jar", "lib.zip", pathAndContents, ANNOTATION_PATH, JavaCore.VERSION_1_5, null); IType type= fJProject1.findType(NODE_PATH.replace('/', '.')); - JavaEditor javaEditor= (JavaEditor) JavaUI.openInEditor(type); + JavaEditor javaEditor= openInClassfileEditor(type, true, true); try { int offset= pathAndContents[1].indexOf("[]> value"); @@ -1477,7 +1477,7 @@ public interface MyMap { // START of actual assist test: IType type= fJProject1.findType(MY_MAP_PATH.replace('/', '.')); - JavaEditor javaEditor= (JavaEditor) JavaUI.openInEditor(type); + JavaEditor javaEditor= openInClassfileEditor(type, true, true); try { int offset= pathAndContents[1].indexOf("V get"); diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/javaeditor/EditorUtility.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/javaeditor/EditorUtility.java index be495cf2cfa..4edeebb83ea 100644 --- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/javaeditor/EditorUtility.java +++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/javaeditor/EditorUtility.java @@ -166,6 +166,10 @@ public static IEditorPart openInEditor(Object inputElement) throws PartInitExcep */ public static IEditorPart openInEditor(Object inputElement, boolean activate) throws PartInitException { + return openInSpecificEditor(inputElement, null, activate); + } + + public static IEditorPart openInSpecificEditor(Object inputElement, String editorID, boolean activate) throws PartInitException { if (inputElement instanceof IFile) { IFile file= (IFile) inputElement; if (!isClassFile(file)) @@ -174,14 +178,17 @@ public static IEditorPart openInEditor(Object inputElement, boolean activate) th } IEditorPart editor= findEditor(inputElement, activate); - if (editor != null) + if (editor != null && (editorID == null || editorID.equals(editor.getEditorSite().getId()))) return editor; IEditorInput input= getEditorInput(inputElement); if (input == null) throw new PartInitException(new Status(IStatus.ERROR, JavaUI.ID_PLUGIN, IJavaStatusConstants.EDITOR_NO_EDITOR_INPUT, JavaEditorMessages.EditorUtility_no_editorInput, null)); - return openInEditor(input, getEditorID(input), activate); + if (editorID == null) { + editorID = getEditorID(input); + } + return openInEditor(input, editorID, activate); } /**