diff options
author | Stephan Herrmann | 2018-03-10 22:21:32 +0000 |
---|---|---|
committer | Stephan Herrmann | 2018-03-10 22:22:21 +0000 |
commit | 5063a3b3b0f4591017b42136112f685ccc4edd02 (patch) | |
tree | 2140772bc64f426a674055f9156a6b78efdb2145 | |
parent | 8147c873affe7df599eae78bdd0ef5b9e8b28f9c (diff) | |
download | eclipse.jdt.core-5063a3b3b0f4591017b42136112f685ccc4edd02.tar.gz eclipse.jdt.core-5063a3b3b0f4591017b42136112f685ccc4edd02.tar.xz eclipse.jdt.core-5063a3b3b0f4591017b42136112f685ccc4edd02.zip |
Bug 485080 - Binding based hover throws NPE on source file that's not on
the classpath
Change-Id: Ic5c441e93d9c0406c4be740ffca5aaee8fcb6935
-rw-r--r-- | org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/model/JavaElement8Tests.java | 47 | ||||
-rw-r--r-- | org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/util/Util.java | 24 |
2 files changed, 63 insertions, 8 deletions
diff --git a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/model/JavaElement8Tests.java b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/model/JavaElement8Tests.java index 4ddd4bad68..a6340b2d9f 100644 --- a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/model/JavaElement8Tests.java +++ b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/model/JavaElement8Tests.java @@ -10,11 +10,14 @@ *******************************************************************************/ package org.eclipse.jdt.core.tests.model; +import java.io.ByteArrayInputStream; import java.io.IOException; import java.util.HashMap; import junit.framework.Test; +import org.eclipse.core.resources.IFile; +import org.eclipse.core.resources.IFolder; import org.eclipse.core.runtime.CoreException; import org.eclipse.jdt.core.IClassFile; import org.eclipse.jdt.core.ICompilationUnit; @@ -26,6 +29,10 @@ import org.eclipse.jdt.core.IPackageFragmentRoot; import org.eclipse.jdt.core.IType; import org.eclipse.jdt.core.ITypeHierarchy; import org.eclipse.jdt.core.JavaCore; +import org.eclipse.jdt.core.dom.ASTParser; +import org.eclipse.jdt.core.dom.IBinding; +import org.eclipse.jdt.core.dom.IMethodBinding; +import org.eclipse.jdt.core.dom.ITypeBinding; import org.eclipse.jdt.core.tests.util.AbstractCompilerTest; import org.eclipse.jdt.internal.compiler.impl.CompilerOptions; import org.eclipse.jdt.internal.core.LambdaExpression; @@ -614,4 +621,44 @@ public class JavaElement8Tests extends AbstractJavaModelTests { deleteProject(projectName); } } + + public void testBug485080() throws Exception { + String projectName = "Bug485080"; + try { + IJavaProject project = createJavaProject(projectName, new String[] {"src"}, new String[] {"JCL18_LIB"}, "bin", "1.8"); + project.open(null); + + // create a .java file in a folder that's not on the build path: + IFolder folder= project.getProject().getFolder("nosrc"); + folder.create(0, true, null); + IFile file= folder.getFile("X.java"); + StringBuilder buf= new StringBuilder(); + buf.append("public class X {\n"); + buf.append(" public <T> void meth(T s) {\n"); + buf.append(" }\n"); + buf.append("}\n"); + String content= buf.toString(); + file.create(new ByteArrayInputStream(content.getBytes("UTF-8")), 0, null); + + // create a CU from that file: + ICompilationUnit cu = JavaCore.createCompilationUnitFrom(file); + cu.becomeWorkingCopy(null); + + // create the binding for the CU's main type, and drill down to details: + ASTParser parser= ASTParser.newParser(AST_INTERNAL_JLS9); + parser.setProject(project); + IBinding[] bindings = parser.createBindings(new IJavaElement[] { cu.findPrimaryType() }, null); + IMethodBinding methodBinding= ((ITypeBinding) bindings[0]).getDeclaredMethods()[1]; + assertEquals("method name", "meth", methodBinding.getName()); + ITypeBinding typeParameter = methodBinding.getTypeParameters()[0]; + + // fetch and inspect the corresponding java element: + IJavaElement javaElement = typeParameter.getJavaElement(); + assertNotNull("java element", javaElement); + assertEquals("element kind", IJavaElement.TYPE_PARAMETER, javaElement.getElementType()); + assertEquals("element name", "T", javaElement.getElementName()); + } finally { + deleteProject(projectName); + } + } } diff --git a/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/util/Util.java b/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/util/Util.java index 1d37cba603..894ebb3892 100644 --- a/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/util/Util.java +++ b/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/util/Util.java @@ -772,15 +772,23 @@ public class Util { if (pkgEnd == -1) return null; IPackageFragment pkg = getPackageFragment(slashSeparatedFileName, pkgEnd, -1/*no jar separator for .java files*/); - if (pkg == null) return null; - int start; - ICompilationUnit cu = pkg.getCompilationUnit(new String(slashSeparatedFileName, start = pkgEnd+1, slashSeparatedFileName.length - start)); - if (workingCopyOwner != null) { - ICompilationUnit workingCopy = cu.findWorkingCopy(workingCopyOwner); - if (workingCopy != null) - return workingCopy; + if (pkg != null) { + int start; + ICompilationUnit cu = pkg.getCompilationUnit(new String(slashSeparatedFileName, start = pkgEnd+1, slashSeparatedFileName.length - start)); + if (workingCopyOwner != null) { + ICompilationUnit workingCopy = cu.findWorkingCopy(workingCopyOwner); + if (workingCopy != null) + return workingCopy; + } + return cu; + } + IWorkspaceRoot wsRoot = ResourcesPlugin.getWorkspace().getRoot(); + IFile file = wsRoot.getFile(new Path(String.valueOf(fileName))); + if (file.exists()) { + // this approach works if file exists but is not on the project's build path: + return JavaCore.createCompilationUnitFrom(file); } - return cu; + return null; } /** |