From dd982b69c93d5b7ca671ec6f2e0915c7955a8a83 Mon Sep 17 00:00:00 2001 From: Roland Grunberg Date: Fri, 26 Jul 2024 12:14:42 -0400 Subject: [PATCH] Fix NPE occuring when a call hierarchy item may not support class files. - When classFileContentsSupport set to false, call hierarchy item's URI will be null, so we should handle this for clients Signed-off-by: Roland Grunberg --- .../handlers/CallHierarchyHandler.java | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/CallHierarchyHandler.java b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/CallHierarchyHandler.java index 9584a485ea..162d6f1e3b 100644 --- a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/CallHierarchyHandler.java +++ b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/CallHierarchyHandler.java @@ -20,6 +20,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; +import java.util.Collections; import java.util.List; import java.util.Map; import java.util.Optional; @@ -80,7 +81,8 @@ public List prepareCallHierarchy(CallHierarchyPrepareParams p return null; } checkMonitor(monitor); - return Arrays.asList(toCallHierarchyItem(candidate)); + CallHierarchyItem symbol = toCallHierarchyItem(candidate); + return symbol != null ? Arrays.asList(symbol) : Collections.emptyList(); } catch (OperationCanceledException e) { // do nothing } catch (JavaModelException e) { @@ -213,9 +215,11 @@ private List getIncomingCallItemsAt(String uri, int l IOpenable openable = getOpenable(location); Range callRange = getRange(openable, location); CallHierarchyItem symbol = toCallHierarchyItem(call.getMember()); - symbol.setSelectionRange(callRange); - List ranges = toCallRanges(callLocations); - result.add(new CallHierarchyIncomingCall(symbol, ranges)); + if (symbol != null) { + symbol.setSelectionRange(callRange); + List ranges = toCallRanges(callLocations); + result.add(new CallHierarchyIncomingCall(symbol, ranges)); + } } } IMember member = call.getMember(); @@ -268,7 +272,9 @@ private List getOutgoingCallItemsAt(String uri, int l List ranges = toCallRanges(callLocations); for (int i = 0; i < callLocations.size(); i++) { CallHierarchyItem symbol = toCallHierarchyItem(call.getMember()); - result.add(new CallHierarchyOutgoingCall(symbol, ranges)); + if (symbol != null) { + result.add(new CallHierarchyOutgoingCall(symbol, ranges)); + } } } IMember member = call.getMember(); @@ -306,6 +312,9 @@ private MethodWrapper getCallRoot(IMember member, boolean isIncomingCall) { private CallHierarchyItem toCallHierarchyItem(IMember member) throws JavaModelException { Location fullLocation = getLocation(member, LocationType.FULL_RANGE); + if (fullLocation == null) { + return null; + } Range range = fullLocation.getRange(); String uri = fullLocation.getUri(); CallHierarchyItem item = new CallHierarchyItem();