diff options
| author | Jeff Johnston | 2018-12-05 18:45:26 +0000 |
|---|---|---|
| committer | Roland Grunberg | 2019-04-30 18:07:42 +0000 |
| commit | 8512b0ea667a6c5cc185f31c018fdc0cefdbeab2 (patch) | |
| tree | e0b7255c6359cfdc5abfb813bf6d36e24343b6ef | |
| parent | 22e1a5c6ee7dd76d9bc36d0d6f288e045f0407a5 (diff) | |
| download | eclipse.jdt.ui-8512b0ea667a6c5cc185f31c018fdc0cefdbeab2.tar.gz eclipse.jdt.ui-8512b0ea667a6c5cc185f31c018fdc0cefdbeab2.tar.xz eclipse.jdt.ui-8512b0ea667a6c5cc185f31c018fdc0cefdbeab2.zip | |
Bug 541834 - [code mining] implementations not shown on methods
- add support for IMethod in JavaElementCodeMining when
showImplentations is true
- add new FindDeclarationsInSubHierarchyAction which is similar
to FindDeclarationsInHierarchyAction but restricts results
to sub-types
- add support in JavaImplementationCodeMining for IMethods
and use the new FindDeclarationsInSubHierarchyAction which
will show results in Search View
Change-Id: I7ab708e09c43ad9e515833f391219952ca90f5d3
Signed-off-by: Jeff Johnston <jjohnstn@redhat.com>
3 files changed, 82 insertions, 18 deletions
diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/javaeditor/codemining/JavaElementCodeMiningProvider.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/javaeditor/codemining/JavaElementCodeMiningProvider.java index 3f0f76e833..15c092d127 100644 --- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/javaeditor/codemining/JavaElementCodeMiningProvider.java +++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/javaeditor/codemining/JavaElementCodeMiningProvider.java @@ -31,6 +31,7 @@ import org.eclipse.jface.text.source.ISourceViewerExtension5; import org.eclipse.ui.texteditor.ITextEditor; import org.eclipse.jdt.core.IJavaElement; +import org.eclipse.jdt.core.IMethod; import org.eclipse.jdt.core.IType; import org.eclipse.jdt.core.ITypeRoot; import org.eclipse.jdt.core.JavaModelException; @@ -163,15 +164,22 @@ public class JavaElementCodeMiningProvider extends AbstractCodeMiningProvider { } } if (showImplementations) { + // support methods, classes, and interfaces + boolean addMining= false; if (element instanceof IType) { IType type= (IType) element; if (type.isInterface() || type.isClass()) { - try { - minings.add(new JavaImplementationCodeMining(type, (JavaEditor) textEditor, viewer.getDocument(), this, - showAtLeastOne)); - } catch (BadLocationException e) { - // Should never occur - } + addMining= true; + } + } else if (element instanceof IMethod) { + addMining= true; + } + if (addMining) { + try { + minings.add(new JavaImplementationCodeMining(element, (JavaEditor) textEditor, viewer.getDocument(), this, + showAtLeastOne)); + } catch (BadLocationException e) { + // Should never occur } } } diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/javaeditor/codemining/JavaImplementationCodeMining.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/javaeditor/codemining/JavaImplementationCodeMining.java index 654524daeb..46fae0c530 100644 --- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/javaeditor/codemining/JavaImplementationCodeMining.java +++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/javaeditor/codemining/JavaImplementationCodeMining.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2018 Angelo Zerr and others. + * Copyright (c) 2019 Angelo Zerr and others. * * This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 @@ -10,16 +10,20 @@ * * Contributors: * - Angelo Zerr: initial API and implementation + * - Red Hat Inc. - add Method implementation support *******************************************************************************/ package org.eclipse.jdt.internal.ui.javaeditor.codemining; import java.text.MessageFormat; +import java.util.List; import java.util.concurrent.CompletableFuture; import java.util.function.Consumer; +import java.util.stream.Collectors; import java.util.stream.Stream; import org.eclipse.swt.events.MouseEvent; +import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.jface.viewers.StructuredSelection; @@ -30,9 +34,11 @@ import org.eclipse.jface.text.ITextViewer; import org.eclipse.jface.text.codemining.ICodeMiningProvider; import org.eclipse.jdt.core.IJavaElement; +import org.eclipse.jdt.core.IMethod; import org.eclipse.jdt.core.IType; import org.eclipse.jdt.core.JavaModelException; +import org.eclipse.jdt.ui.actions.FindDeclarationsInHierarchyAction; import org.eclipse.jdt.ui.actions.OpenTypeHierarchyAction; import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor; @@ -50,7 +56,7 @@ public class JavaImplementationCodeMining extends AbstractJavaElementLineHeaderC private Consumer<MouseEvent> action; - public JavaImplementationCodeMining(IType element, JavaEditor editor, IDocument document, ICodeMiningProvider provider, + public JavaImplementationCodeMining(IJavaElement element, JavaEditor editor, IDocument document, ICodeMiningProvider provider, boolean showImplementationsAtLeastOne) throws JavaModelException, BadLocationException { super(element, document, provider, null); this.editor= editor; @@ -63,14 +69,27 @@ public class JavaImplementationCodeMining extends AbstractJavaElementLineHeaderC return CompletableFuture.runAsync(() -> { try { IJavaElement element= super.getElement(); - long implCount= countImplementations((IType) element, monitor); - action= implCount > 0 ? e -> new OpenTypeHierarchyAction(editor).run(new StructuredSelection(element)) : null; - if (implCount == 0 && showImplementationsAtLeastOne) { - super.setLabel(""); //$NON-NLS-1$ - } else { - super.setLabel(MessageFormat.format(JavaCodeMiningMessages.JavaImplementationCodeMining_label, implCount)); + long implCount = 0; + if (element instanceof IType) { + // for a type, count types implementing this type and show type hierarchy + implCount= countTypeImplementations((IType) element, monitor); + action= implCount > 0 ? e -> new OpenTypeHierarchyAction(editor).run(new StructuredSelection(element)) : null; + if (implCount == 0 && showImplementationsAtLeastOne) { + super.setLabel(""); //$NON-NLS-1$ + } else { + super.setLabel(MessageFormat.format(JavaCodeMiningMessages.JavaImplementationCodeMining_label, implCount)); + } + } else if (element instanceof IMethod) { + // for a method, count declarations in hierarchy and show search->declarations->hierarchy + implCount= countMethodImplementations((IMethod) element, monitor); + action= implCount > 0 ? e -> new FindDeclarationsInHierarchyAction(editor, true).run(element) : null; + if (implCount == 0 && showImplementationsAtLeastOne) { + super.setLabel(""); //$NON-NLS-1$ + } else { + super.setLabel(MessageFormat.format(JavaCodeMiningMessages.JavaImplementationCodeMining_label, implCount)); + } } - } catch (JavaModelException e) { + } catch (CoreException e1) { // Should never occur } }); @@ -89,9 +108,28 @@ public class JavaImplementationCodeMining extends AbstractJavaElementLineHeaderC * @return the count of implementation for the given java element type. * @throws JavaModelException throws when Java error */ - private static long countImplementations(IType type, IProgressMonitor monitor) throws JavaModelException { + private static long countTypeImplementations(IType type, IProgressMonitor monitor) throws JavaModelException { IType[] results= type.newTypeHierarchy(monitor).getAllSubtypes(type); return Stream.of(results).filter(t -> t.getAncestor(IJavaElement.COMPILATION_UNIT) != null).count(); } + /** + * Return the count of implementation for the java element method. + * + * @param method the java element method. + * @param monitor the monitor + * @return the count of implementation for the given java element type. + * @throws CoreException throws when java error + */ + private static long countMethodImplementations(IMethod method, IProgressMonitor monitor) throws CoreException { + if (method == null) { + return 0; + } + IType type= method.getDeclaringType(); + IType[] results= type.newTypeHierarchy(monitor).getAllSubtypes(type); + List<IType> list= Stream.of(results).filter(t -> t.getAncestor(IJavaElement.COMPILATION_UNIT) != null).collect(Collectors.toList()); + long count= list.stream().filter(t -> t.getMethod(method.getElementName(), method.getParameterTypes()).exists()).count(); + return count; + } + } diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/ui/actions/FindDeclarationsInHierarchyAction.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/ui/actions/FindDeclarationsInHierarchyAction.java index c8b17bd29e..80e5f564c2 100644 --- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/ui/actions/FindDeclarationsInHierarchyAction.java +++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/ui/actions/FindDeclarationsInHierarchyAction.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2016 IBM Corporation and others. + * Copyright (c) 2000, 2019 IBM Corporation and others. * * This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 @@ -10,6 +10,7 @@ * * Contributors: * IBM Corporation - initial API and implementation + * Red Hat Inc. - add support for restricting to sub-types *******************************************************************************/ package org.eclipse.jdt.ui.actions; @@ -51,6 +52,8 @@ import org.eclipse.jdt.internal.ui.search.SearchMessages; */ public class FindDeclarationsInHierarchyAction extends FindDeclarationsAction { + private boolean onlySubtypes; + /** * Creates a new <code>FindDeclarationsInHierarchyAction</code>. The action * requires that the selection provided by the site's selection provider is of type @@ -72,6 +75,19 @@ public class FindDeclarationsInHierarchyAction extends FindDeclarationsAction { super(editor); } + /** + * Note: This constructor is for internal use only. Clients should not call this constructor. + * @param editor the Java editor + * @param onlySubtypes true if only sub-types should be considered in search + * + * @noreference This constructor is not intended to be referenced by clients. + * @since 3.18 + */ + public FindDeclarationsInHierarchyAction(JavaEditor editor, boolean onlySubtypes) { + super(editor); + this.onlySubtypes= onlySubtypes; + } + @Override Class<?>[] getValidTypes() { return new Class[] { IField.class, IMethod.class, ILocalVariable.class, ITypeParameter.class }; @@ -93,7 +109,9 @@ public class FindDeclarationsInHierarchyAction extends FindDeclarationsAction { if (type == null) { return super.createQuery(element); } - IJavaSearchScope scope= SearchEngine.createHierarchyScope(type); + IJavaSearchScope scope= onlySubtypes + ? SearchEngine.createStrictHierarchyScope(null, type, true, false, null) + : SearchEngine.createHierarchyScope(type); String description= factory.getHierarchyScopeDescription(type); return new ElementQuerySpecification(element, getLimitTo(), scope, description); } |
