Skip to content
This repository has been archived by the owner on Mar 23, 2022. It is now read-only.

Commit

Permalink
Fix NPE in FindImplementersHandler (#71)
Browse files Browse the repository at this point in the history
Signed-off-by: Valeriy Svydenko <[email protected]>
  • Loading branch information
Valeriy Svydenko authored Aug 27, 2018
1 parent 1952443 commit 4c8217e
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,24 @@
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jdt.core.IClassFile;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.ILocalVariable;
import org.eclipse.jdt.core.IMember;
import org.eclipse.jdt.core.IPackageFragment;
import org.eclipse.jdt.core.ISourceRange;
import org.eclipse.jdt.core.ISourceReference;
import org.eclipse.jdt.core.ITypeParameter;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.core.SourceRange;
import org.eclipse.jdt.ls.core.internal.JDTUtils;
import org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin;
import org.eclipse.jdt.ls.core.internal.ResourceUtils;
import org.eclipse.jdt.ls.core.internal.handlers.DocumentSymbolHandler;
import org.eclipse.lsp4j.Location;
import org.eclipse.lsp4j.Position;
import org.eclipse.lsp4j.SymbolKind;

Expand Down Expand Up @@ -85,6 +94,55 @@ static SymbolKind mapKind(IJavaElement element) {
return DocumentSymbolHandler.mapKind(element);
}

/**
* Creates a location for a given java element. Element can be a {@link ICompilationUnit} or
* {@link IClassFile}
*
* @param element java element
* @return location or null
*/
public static Location toLocation(IJavaElement element) throws JavaModelException {
ICompilationUnit unit = (ICompilationUnit) element.getAncestor(IJavaElement.COMPILATION_UNIT);
IClassFile cf = (IClassFile) element.getAncestor(IJavaElement.CLASS_FILE);
if (unit == null && cf == null) {
return null;
}
if (element instanceof ISourceReference) {
ISourceRange nameRange = getNameRange(element);
int offset = 0;
int length = 0;
if (SourceRange.isAvailable(nameRange)) {
offset = nameRange.getOffset();
length = nameRange.getLength();
}
if (cf != null) {
return JDTUtils.toLocation(cf, offset, length);
} else {
return JDTUtils.toLocation(unit, offset, length);
}
}
return null;
}

private static ISourceRange getNameRange(IJavaElement element) throws JavaModelException {
ISourceRange nameRange = null;
if (element instanceof IMember) {
IMember member = (IMember) element;
nameRange = member.getNameRange();
if ((!SourceRange.isAvailable(nameRange))) {
nameRange = member.getSourceRange();
}
} else if (element instanceof ITypeParameter || element instanceof ILocalVariable) {
nameRange = ((ISourceReference) element).getNameRange();
} else if (element instanceof ISourceReference) {
nameRange = ((ISourceReference) element).getSourceRange();
}
if (!SourceRange.isAvailable(nameRange) && element.getParent() != null) {
nameRange = getNameRange(element.getParent());
}
return nameRange;
}

public static IJavaElement getJavaElement(Position position, String uri, IProgressMonitor pm)
throws CoreException {
ICompilationUnit unit = JDTUtils.resolveCompilationUnit(uri);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.List;
import org.eclipse.che.jdt.ls.extension.api.dto.ImplementersResponse;
import org.eclipse.che.jdt.ls.extension.core.internal.GsonUtils;
import org.eclipse.che.jdt.ls.extension.core.internal.JavaModelUtil;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IMethod;
Expand Down Expand Up @@ -95,8 +96,7 @@ private static void findSubTypes(
IType[] implTypes = typeHierarchy.getAllSubtypes(type);

for (IType implType : implTypes) {
SymbolInformation dto = convertToSymbolInformation(implType);
implementers.add(dto);
addImplementer(implType, implementers);
}
}

Expand All @@ -119,19 +119,26 @@ private static void findTypesWithSubMethods(
if (method == null) {
continue;
}
SymbolInformation openDeclaration = convertToSymbolInformation(method);
addImplementer(method, implementers);
}
}

private static void addImplementer(IJavaElement javaElement, List<SymbolInformation> implementers)
throws JavaModelException {
Location location = JavaModelUtil.toLocation(javaElement);
if (location != null) {
SymbolInformation openDeclaration = convertToSymbolInformation(javaElement, location);
implementers.add(openDeclaration);
}
}

@SuppressWarnings("restriction")
private static SymbolInformation convertToSymbolInformation(IJavaElement javaElement)
throws JavaModelException {
private static SymbolInformation convertToSymbolInformation(
IJavaElement javaElement, Location location) throws JavaModelException {
SymbolInformation symbolInformation = new SymbolInformation();
symbolInformation.setKind(DocumentSymbolHandler.mapKind(javaElement));
symbolInformation.setName(
JavaElementLabels.getElementLabel(javaElement, JavaElementLabels.ALL_DEFAULT));
Location location = JDTUtils.toLocation(javaElement);
location.setUri(ResourceUtils.toClientUri(location.getUri()));
symbolInformation.setLocation(location);
return symbolInformation;
Expand Down

0 comments on commit 4c8217e

Please sign in to comment.