Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Navigate to the super implementation #1165

Merged
merged 4 commits into from
Sep 17, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ InitializeResult initialize(InitializeParams param) {
if (!preferenceManager.getClientPreferences().isTypeDefinitionDynamicRegistered()) {
capabilities.setTypeDefinitionProvider(Boolean.TRUE);
}
if (!preferenceManager.getClientPreferences().isHoverDynamicRegistered()) {
if (!preferenceManager.getClientPreferences().isClientHoverProviderRegistered() && !preferenceManager.getClientPreferences().isHoverDynamicRegistered()) {
capabilities.setHoverProvider(Boolean.TRUE);
}
if (!preferenceManager.getClientPreferences().isReferencesDynamicRegistered()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ public void initialized(InitializedParams params) {
if (preferenceManager.getClientPreferences().isTypeDefinitionDynamicRegistered()) {
registerCapability(Preferences.TYPEDEFINITION_ID, Preferences.TYPEDEFINITION);
}
if (preferenceManager.getClientPreferences().isHoverDynamicRegistered()) {
if (!preferenceManager.getClientPreferences().isClientHoverProviderRegistered() && preferenceManager.getClientPreferences().isHoverDynamicRegistered()) {
registerCapability(Preferences.HOVER_ID, Preferences.HOVER);
}
if (preferenceManager.getClientPreferences().isReferencesDynamicRegistered()) {
Expand Down Expand Up @@ -562,6 +562,11 @@ public CompletableFuture<List<? extends Location>> references(ReferenceParams pa
return computeAsync((monitor) -> handler.findReferences(params, monitor));
}

public CompletableFuture<List<? extends Location>> methodOverride(TextDocumentPositionParams position) {
logInfo(">> methodOverride");
return computeAsync((monitor) -> NavigateToOverrideHandler.methodOverride(position, monitor));
}

/* (non-Javadoc)
* @see org.eclipse.lsp4j.services.TextDocumentService#documentHighlight(org.eclipse.lsp4j.TextDocumentPositionParams)
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*******************************************************************************
* Copyright (c) 2019 Microsoft Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Microsoft Corporation - initial API and implementation
*******************************************************************************/

package org.eclipse.jdt.ls.core.internal.handlers;

import java.util.Collections;
import java.util.List;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IMethod;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.ITypeHierarchy;
import org.eclipse.jdt.core.ITypeRoot;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.internal.corext.util.MethodOverrideTester;
import org.eclipse.jdt.ls.core.internal.JDTUtils;
import org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin;
import org.eclipse.jdt.ls.core.internal.preferences.PreferenceManager;
import org.eclipse.lsp4j.Location;
import org.eclipse.lsp4j.TextDocumentPositionParams;

public class NavigateToOverrideHandler {

public static List<? extends Location> methodOverride(TextDocumentPositionParams position, IProgressMonitor monitor) {
ITypeRoot unit = JDTUtils.resolveTypeRoot(position.getTextDocument().getUri());
Location location = null;
IMethod overrideMethod = null;
if (unit != null && !monitor.isCanceled()) {
PreferenceManager preferenceManager = JavaLanguageServerPlugin.getInstance().getPreferencesManager();
try {
IJavaElement element = JDTUtils.findElementAtSelection(unit, position.getPosition().getLine(), position.getPosition().getCharacter(), preferenceManager, monitor);
overrideMethod = findOverrideMethod(element, monitor);
if (overrideMethod != null) {
location = NavigateToDefinitionHandler.computeDefinitionNavigation(overrideMethod, element.getJavaProject());
}
} catch (JavaModelException e) {
// do nothing
}
}

if (location == null) {
return Collections.emptyList();
}

String declaringTypeName = overrideMethod.getDeclaringType().getFullyQualifiedName();
String methodName = overrideMethod.getElementName();
return Collections.singletonList(new MethodLocation(declaringTypeName, methodName, location));
}

public static IMethod findOverrideMethod(IJavaElement element, IProgressMonitor monitor) throws JavaModelException {
if (!(element instanceof IMethod)) {
return null;
}

IMethod method = (IMethod) element;
IType type = method.getDeclaringType();
if (type == null || type.isInterface() || method.isConstructor()) {
return null;
}

ITypeHierarchy hierarchy = type.newSupertypeHierarchy(monitor);
MethodOverrideTester tester = new MethodOverrideTester(type, hierarchy);
IMethod found = tester.findOverriddenMethod(method, true);
if (found != null && !found.equals(method)) {
return found;
}

return null;
}

public static class MethodLocation extends Location {
public String declaringTypeName;
public String methodName;

public MethodLocation(String declaringTypeName, String methodName, Location location) {
super(location.getUri(), location.getRange());
this.declaringTypeName = declaringTypeName;
this.methodName = methodName;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@
import org.eclipse.jdt.ls.core.internal.handlers.OverrideMethodsHandler.OverridableMethodsResponse;
import org.eclipse.jdt.ls.core.internal.handlers.WorkspaceSymbolHandler.SearchSymbolParams;
import org.eclipse.lsp4j.CodeActionParams;
import org.eclipse.lsp4j.Location;
import org.eclipse.lsp4j.SymbolInformation;
import org.eclipse.lsp4j.TextDocumentIdentifier;
import org.eclipse.lsp4j.TextDocumentPositionParams;
import org.eclipse.lsp4j.WorkspaceEdit;
import org.eclipse.lsp4j.jsonrpc.services.JsonNotification;
import org.eclipse.lsp4j.jsonrpc.services.JsonRequest;
Expand Down Expand Up @@ -111,4 +113,7 @@ public interface JavaProtocolExtensions {

@JsonRequest
CompletableFuture<List<SymbolInformation>> searchSymbols(SearchSymbolParams params);

@JsonRequest
CompletableFuture<List<? extends Location>> methodOverride(TextDocumentPositionParams position);
testforstephen marked this conversation as resolved.
Show resolved Hide resolved
}
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,10 @@ public boolean isMoveRefactoringSupported() {
return Boolean.parseBoolean(extendedClientCapabilities.getOrDefault("moveRefactoringSupport", "false").toString());
}

public boolean isClientHoverProviderRegistered() {
return Boolean.parseBoolean(extendedClientCapabilities.getOrDefault("clientHoverProvider", "false").toString());
}

public boolean isSupportsCompletionDocumentationMarkdown() {
//@formatter:off
return v3supported && capabilities.getTextDocument().getCompletion() != null
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
/*******************************************************************************
* Copyright (c) 2019 Microsoft Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Microsoft Corporation - initial API and implementation
*******************************************************************************/

package org.eclipse.jdt.ls.core.internal.handlers;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.when;

import java.util.List;

import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IPackageFragment;
import org.eclipse.jdt.core.IPackageFragmentRoot;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.ls.core.internal.JDTUtils;
import org.eclipse.jdt.ls.core.internal.handlers.NavigateToOverrideHandler.MethodLocation;
import org.eclipse.jdt.ls.core.internal.managers.AbstractProjectsManagerBasedTest;
import org.eclipse.jdt.ls.core.internal.preferences.ClientPreferences;
import org.eclipse.lsp4j.Location;
import org.eclipse.lsp4j.Position;
import org.eclipse.lsp4j.Range;
import org.eclipse.lsp4j.TextDocumentIdentifier;
import org.eclipse.lsp4j.TextDocumentPositionParams;
import org.junit.Before;
import org.junit.Test;

public class NavigateToOverrideHandlerTest extends AbstractProjectsManagerBasedTest {
private IPackageFragmentRoot sourceFolder;

@Before
public void setup() throws Exception {
IJavaProject javaProject = newEmptyProject();
sourceFolder = javaProject.getPackageFragmentRoot(javaProject.getProject().getFolder("src"));
ClientPreferences clientPreferences = preferenceManager.getClientPreferences();
when(clientPreferences.isResourceOperationSupported()).thenReturn(true);
}

@Test
public void testNavigateToOverrideMethod() throws JavaModelException {
IPackageFragment pack1 = sourceFolder.createPackageFragment("test1", false, null);
//@formatter:off
ICompilationUnit unitA = pack1.createCompilationUnit("A.java", "package test1;\n" +
"\n" +
"public class A {\n" +
" public void run() {\n" +
" }\n" +
"}", true, null);
//@formatter:on

//@formatter:off
ICompilationUnit unitB = pack1.createCompilationUnit("B.java", "package test1;\n" +
"\n" +
"public class B extends A {\n" +
" public void run() {\n" +
" }\n" +
"}", true, null);
//@formatter:on

String uri = JDTUtils.toURI(unitB);
List<? extends Location> response = NavigateToOverrideHandler.methodOverride(new TextDocumentPositionParams(new TextDocumentIdentifier(uri), new Position(3, 14)), new NullProgressMonitor());
assertTrue(response != null && response.size() == 1);
MethodLocation location = (MethodLocation) response.get(0);
assertEquals("test1.A", location.declaringTypeName);
assertEquals("run", location.methodName);
assertEquals(JDTUtils.toURI(unitA), location.getUri());
Range range = location.getRange();
assertEquals(3, range.getStart().getLine());
assertEquals(13, range.getStart().getCharacter());
assertEquals(3, range.getEnd().getLine());
assertEquals(16, range.getEnd().getCharacter());
}

@Test
public void testNearestOverrideMethod() throws JavaModelException {
IPackageFragment pack1 = sourceFolder.createPackageFragment("test1", false, null);
//@formatter:off
ICompilationUnit unitA = pack1.createCompilationUnit("A.java", "package test1;\n" +
"\n" +
"public class A {\n" +
" public void run() {\n" +
" }\n" +
"}", true, null);
//@formatter:on

//@formatter:off
ICompilationUnit unitB = pack1.createCompilationUnit("B.java", "package test1;\n" +
"\n" +
"public class B extends A {\n" +
"}", true, null);
//@formatter:on

//@formatter:off
ICompilationUnit unitC = pack1.createCompilationUnit("C.java", "package test1;\n" +
"\n" +
"public class C extends B {\n" +
" public void run() {\n" +
" }\n" +
"}", true, null);
//@formatter:on

String uri = JDTUtils.toURI(unitC);
List<? extends Location> response = NavigateToOverrideHandler.methodOverride(new TextDocumentPositionParams(new TextDocumentIdentifier(uri), new Position(3, 14)), new NullProgressMonitor());
assertTrue(response != null && response.size() == 1);
MethodLocation location = (MethodLocation) response.get(0);
assertEquals("test1.A", location.declaringTypeName);
assertEquals("run", location.methodName);
assertEquals(JDTUtils.toURI(unitA), location.getUri());
Range range = location.getRange();
assertEquals(3, range.getStart().getLine());
assertEquals(13, range.getStart().getCharacter());
assertEquals(3, range.getEnd().getLine());
assertEquals(16, range.getEnd().getCharacter());
}

@Test
public void testNoOverrideMethod() throws JavaModelException {
IPackageFragment pack1 = sourceFolder.createPackageFragment("test1", false, null);
//@formatter:off
ICompilationUnit unitA = pack1.createCompilationUnit("A.java", "package test1;\n" +
"\n" +
"public class A {\n" +
" public void run() {\n" +
" }\n" +
"}", true, null);
//@formatter:on

String uri = JDTUtils.toURI(unitA);
List<? extends Location> response = NavigateToOverrideHandler.methodOverride(new TextDocumentPositionParams(new TextDocumentIdentifier(uri), new Position(3, 14)), new NullProgressMonitor());
assertTrue(response == null || response.isEmpty());
}
}