From bad8def27f73d0e350ce4cdb12e76d90b86159c6 Mon Sep 17 00:00:00 2001 From: Fred Bricon Date: Tue, 1 Nov 2016 14:28:34 -0400 Subject: [PATCH] Prevent some IIOB exception when offset < 0 May fix https://github.com/redhat-developer/vscode-java/issues/55 Signed-off-by: Fred Bricon --- .../java/internal/HoverInfoProvider.java | 3 +-- .../tools/vscode/java/internal/JDTUtils.java | 19 +++++++++++++++++++ .../internal/handlers/CodeLensHandler.java | 11 +---------- .../internal/handlers/CompletionHandler.java | 5 ++++- .../handlers/NavigateToDefinitionHandler.java | 10 ++++------ .../internal/handlers/ReferencesHandler.java | 14 +------------- 6 files changed, 30 insertions(+), 32 deletions(-) diff --git a/org.jboss.tools.vscode.java/src/org/jboss/tools/vscode/java/internal/HoverInfoProvider.java b/org.jboss.tools.vscode.java/src/org/jboss/tools/vscode/java/internal/HoverInfoProvider.java index 0422465ec0..509d1632e5 100644 --- a/org.jboss.tools.vscode.java/src/org/jboss/tools/vscode/java/internal/HoverInfoProvider.java +++ b/org.jboss.tools.vscode.java/src/org/jboss/tools/vscode/java/internal/HoverInfoProvider.java @@ -23,7 +23,6 @@ import org.eclipse.jdt.core.ISourceRange; import org.eclipse.jdt.core.ITypeParameter; import org.eclipse.jdt.core.ITypeRoot; -import org.jboss.tools.vscode.java.internal.handlers.JsonRpcHelpers; import org.jboss.tools.vscode.java.internal.javadoc.JavadocContentAccess; public class HoverInfoProvider { @@ -35,7 +34,7 @@ public HoverInfoProvider(ITypeRoot aUnit) { public String computeHover(int line, int column) { try { - IJavaElement[] elements = unit.codeSelect(JsonRpcHelpers.toOffset(unit.getBuffer(),line,column),0); + IJavaElement[] elements = JDTUtils.findElementsAtSelection(unit, line, column); if(elements == null || elements.length == 0) { return null; } diff --git a/org.jboss.tools.vscode.java/src/org/jboss/tools/vscode/java/internal/JDTUtils.java b/org.jboss.tools.vscode.java/src/org/jboss/tools/vscode/java/internal/JDTUtils.java index 6fef56be21..d77b63b7ce 100644 --- a/org.jboss.tools.vscode.java/src/org/jboss/tools/vscode/java/internal/JDTUtils.java +++ b/org.jboss.tools.vscode.java/src/org/jboss/tools/vscode/java/internal/JDTUtils.java @@ -246,4 +246,23 @@ public static String getFileURI(IResource resource) { return uri.replaceFirst("file:/([^/])", "file:///$1"); } + public static IJavaElement findElementAtSelection(ITypeRoot unit, int line, int column) throws JavaModelException { + IJavaElement[] elements = findElementsAtSelection(unit, line, column); + if (elements != null && elements.length == 1) { + return elements[0]; + } + return null; + } + + public static IJavaElement[] findElementsAtSelection(ITypeRoot unit, int line, int column) throws JavaModelException { + if (unit == null) { + return null; + } + int offset = JsonRpcHelpers.toOffset(unit.getBuffer(), line, column); + if (offset > -1) { + return unit.codeSelect(offset, 0); + } + return null; + } + } diff --git a/org.jboss.tools.vscode.java/src/org/jboss/tools/vscode/java/internal/handlers/CodeLensHandler.java b/org.jboss.tools.vscode.java/src/org/jboss/tools/vscode/java/internal/handlers/CodeLensHandler.java index 38c591a6b4..9ed9843144 100644 --- a/org.jboss.tools.vscode.java/src/org/jboss/tools/vscode/java/internal/handlers/CodeLensHandler.java +++ b/org.jboss.tools.vscode.java/src/org/jboss/tools/vscode/java/internal/handlers/CodeLensHandler.java @@ -85,7 +85,7 @@ private CodeLens resolve(CodeLens lens) { return lens; } Map position = (Map) data.get(1); - IJavaElement element = findElementAtSelection(unit, ((Double)position.get("line")).intValue(), ((Double)position.get("character")).intValue()); + IJavaElement element = JDTUtils.findElementAtSelection(unit, ((Double)position.get("line")).intValue(), ((Double)position.get("character")).intValue()); List locations = findReferences(element); int nReferences = locations.size(); lens.setCommand(new Command().withTitle(nReferences == 1 ? "1 reference" : nReferences + " references") @@ -97,15 +97,6 @@ private CodeLens resolve(CodeLens lens) { return lens; } - private IJavaElement findElementAtSelection(ICompilationUnit unit, int line, int column) throws JavaModelException { - IJavaElement[] elements = unit.codeSelect(JsonRpcHelpers.toOffset(unit.getBuffer(), line, column), 0); - - if (elements == null || elements.length != 1) - return null; - return elements[0]; - - } - private List findReferences(IJavaElement element) throws JavaModelException, CoreException { if (element == null) { return Collections.emptyList(); diff --git a/org.jboss.tools.vscode.java/src/org/jboss/tools/vscode/java/internal/handlers/CompletionHandler.java b/org.jboss.tools.vscode.java/src/org/jboss/tools/vscode/java/internal/handlers/CompletionHandler.java index 4c78ecb01d..36d5f19047 100644 --- a/org.jboss.tools.vscode.java/src/org/jboss/tools/vscode/java/internal/handlers/CompletionHandler.java +++ b/org.jboss.tools.vscode.java/src/org/jboss/tools/vscode/java/internal/handlers/CompletionHandler.java @@ -67,7 +67,10 @@ private List computeContentAssist(ICompilationUnit unit, int lin collector.setAllowsRequiredProposals(CompletionProposal.TYPE_REF, CompletionProposal.TYPE_REF, true); - unit.codeComplete(JsonRpcHelpers.toOffset(unit.getBuffer(), line, column), collector, new NullProgressMonitor()); + int offset = JsonRpcHelpers.toOffset(unit.getBuffer(), line, column); + if (offset >-1) { + unit.codeComplete(offset, collector, new NullProgressMonitor()); + } } catch (JavaModelException e) { JavaLanguageServerPlugin.logException("Problem with codeComplete for " + unit.getElementName(), e); } diff --git a/org.jboss.tools.vscode.java/src/org/jboss/tools/vscode/java/internal/handlers/NavigateToDefinitionHandler.java b/org.jboss.tools.vscode.java/src/org/jboss/tools/vscode/java/internal/handlers/NavigateToDefinitionHandler.java index ff3c6390ff..e2d85f29c7 100644 --- a/org.jboss.tools.vscode.java/src/org/jboss/tools/vscode/java/internal/handlers/NavigateToDefinitionHandler.java +++ b/org.jboss.tools.vscode.java/src/org/jboss/tools/vscode/java/internal/handlers/NavigateToDefinitionHandler.java @@ -35,13 +35,11 @@ public boolean canHandle(String request) { private Location computeDefinitonNavigation(ITypeRoot unit, int line, int column) { try { - IJavaElement[] elements = unit.codeSelect(JsonRpcHelpers.toOffset(unit.getBuffer(), line, column), 0); - - if (elements == null || elements.length != 1) + IJavaElement element = JDTUtils.findElementAtSelection(unit, line, column); + if (element == null) { return null; - IJavaElement element = elements[0]; - ICompilationUnit compilationUnit = (ICompilationUnit) element - .getAncestor(IJavaElement.COMPILATION_UNIT); + } + ICompilationUnit compilationUnit = (ICompilationUnit) element.getAncestor(IJavaElement.COMPILATION_UNIT); IClassFile cf = (IClassFile) element.getAncestor(IJavaElement.CLASS_FILE); if (compilationUnit != null || (cf != null && cf.getSourceRange() != null) ) { return JDTUtils.toLocation(element); diff --git a/org.jboss.tools.vscode.java/src/org/jboss/tools/vscode/java/internal/handlers/ReferencesHandler.java b/org.jboss.tools.vscode.java/src/org/jboss/tools/vscode/java/internal/handlers/ReferencesHandler.java index e1a4aad960..0460c9e6ef 100644 --- a/org.jboss.tools.vscode.java/src/org/jboss/tools/vscode/java/internal/handlers/ReferencesHandler.java +++ b/org.jboss.tools.vscode.java/src/org/jboss/tools/vscode/java/internal/handlers/ReferencesHandler.java @@ -21,7 +21,6 @@ import org.eclipse.jdt.core.ICompilationUnit; import org.eclipse.jdt.core.IJavaElement; import org.eclipse.jdt.core.IJavaProject; -import org.eclipse.jdt.core.ITypeRoot; import org.eclipse.jdt.core.JavaCore; import org.eclipse.jdt.core.JavaModelException; import org.eclipse.jdt.core.search.IJavaSearchConstants; @@ -49,17 +48,6 @@ public boolean canHandle(String request) { return LSPMethods.DOCUMENT_REFERENCES.getMethod().equals(request); } - - private IJavaElement findElementAtSelection(ITypeRoot unit, int line, int column) throws JavaModelException { - if(unit == null ) return null; - IJavaElement[] elements = unit.codeSelect(JsonRpcHelpers.toOffset(unit.getBuffer(), line, column), 0); - - if (elements == null || elements.length != 1) - return null; - return elements[0]; - - } - private IJavaSearchScope createSearchScope() throws JavaModelException { IJavaProject[] projects = JavaCore.create(ResourcesPlugin.getWorkspace().getRoot()).getJavaProjects(); return SearchEngine.createJavaSearchScope(projects, IJavaSearchScope.SOURCES | IJavaSearchScope.APPLICATION_LIBRARIES); @@ -70,7 +58,7 @@ public List handle(org.jboss.tools.langs.Referen SearchEngine engine = new SearchEngine(); try { - IJavaElement elementToSearch = findElementAtSelection(JDTUtils.resolveTypeRoot(param.getTextDocument().getUri()), + IJavaElement elementToSearch = JDTUtils.findElementAtSelection(JDTUtils.resolveTypeRoot(param.getTextDocument().getUri()), param.getPosition().getLine().intValue(), param.getPosition().getCharacter().intValue());