Comments (3)
You haven't really described your use case, but I suspect the following is what you want:
IJavaElement element = binding.getJavaElement();
while (element != null && !(element instanceof IPackageFragment)) {
element = element.getParent();
}
IResource resource = element == null ? null : element.getResource();
from eclipse.jdt.core.
Thanks - but this doesn't really resolve the issue.
I need 2 things - I need the correct IPackageBinding, and I need to get the correct IResource from it.
When we call binding.getPackage() to get the IPackageBinding, it's not clear whether this is giving us the correct binding since the package exists in 2 source roots. Our plugin uses this IPackageBinding in multiple places for a variety of things - one of those places calls getResource() on it, which gives us the wrong resource. The call to getResource() does not have access to the original ITypeBinding, which your example code requires.
I see 2 issues here:
- The IPackageBinding returned is ambiguous - is it intended to represent the specific package within the same source root as the ITypeBinding?
- When calling getResource() on the ITypeBinding, we cannot get the one we expect - which should be in a particular source root.
from eclipse.jdt.core.
Regarding 1.:
I think the IPackageBinding is not ambigous: it represents the whole package, corresponding to all source folders.
The contribution to a package by a single source folder is called a "package fragment".
(My code tried to show how to find the IJavaElement for one matching a given type.)
So there really is no "correct" resource corresponding for an IPackageBinding. Maybe pkg.getJavaElement().getResource() should throw an UnsupportedOperationException. But here the implemenation is slightly sloppy: pkg.getJavaElement() actually returns the JavaElement corresponding to its first contributing package fragment:
org.eclipse.jdt.core.dom.PackageBinding.getJavaElement():
IJavaElement[] pkgs = nameLookup.findPackageFragments(getName(), false/*exact match*/);
[...]
return pkgs[0];
Regarding 2., I'm not sure, what you mean. for "ITypeBinding binding" corresponding to a source file, binding.getJavaElement().getResource() should return the File which is located in a source folder.
from eclipse.jdt.core.
Related Issues (20)
- [23] JSR 398
- Building with xml and errors with UTF-8
- Types solely referenced from Javadoc reported as unused imports
- [feat] Add support for polymorphic external null annotations
- Bug 575822 - FR: Add support for polymorphic external null annotations HOT 1
- BindingKeyResolver.consumeAnnotation() should expect null array elements
- [23] markdown javadoc should detect code regions HOT 5
- [Bug - Go To/Navigation Java Records] - Can not go to Class HOT 2
- [Bug - Go To/Navigation/References] - Does not work for Private Methods/Constructor HOT 1
- [Bug - Autocompletion] - Does not work with Record implements Interface
- win: 24 failing compiler.regression.TestAll since I20240805-1800 "being used by another process" HOT 1
- JEP 413 Javadoc External Code Snippets do not work for JARs HOT 1
- ephemeral memory allocation in JRTUtil.getJrtSystem() HOT 3
- ClassCastException with array as wildcard generic parameter
- NPE in TypeSystem.getArrayType
- [23] Code completion for module imports HOT 2
- [23] Parsing details for markdown comments
- [Bug] - No Autocompletion/Import for annotation elements
- NPE in AST: Cannot read the array length because "this.arguments" is null HOT 3
- [feat] Add `@LateNonNull` annotation to declare late initializaiton of non-null fields
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from eclipse.jdt.core.