Giter Site home page Giter Site logo

Comments (3)

brychcy avatar brychcy commented on August 17, 2024

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.

klaymore142 avatar klaymore142 commented on August 17, 2024

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:

  1. The IPackageBinding returned is ambiguous - is it intended to represent the specific package within the same source root as the ITypeBinding?
  2. 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.

brychcy avatar brychcy commented on August 17, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.