Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » How to resolve a referenced type in a compilation unit
How to resolve a referenced type in a compilation unit [message #632056] Mon, 11 October 2010 11:47 Go to next message
Xavier Coulon is currently offline Xavier CoulonFriend
Messages: 58
Registered: July 2009
Member
Hello,

I'm trying to find a clean way to resolve a referenced type in a compilation unit.
For example, given the following code :

package com.example.rs;

import javax.persistence.EntityManager;


@Path(CustomerResource.URI_BASE)
public class CustomerResource {

	private final org.slf4j.Logger logger = LoggerFactory.getLogger(CustomerResource.class);

	@PersistenceContext
	private EntityManager entityManager = null;

        public static final String URI_BASE = "/customers";

   ...
}


I would expect the following results :

  • resolveType(entityManager) -> IType(javax.persistence.EntityManager)
  • resolveType(org.slf4j.Logger logger) -> IType(org.slf4j.Logger logger)
  • resolveType(CustomerResource) -> IType(com.example.rs.CustomerResource)


For now, I managed to write a utility method that basically :

  • creates an AST Parser and parses the given IType (here, CustomerResouce)
  • retrieve the imports
  • iterates on the ImportDeclarations to match the Type without the package name (EntityManager, Logger, CustomerResource)
  • Returns the IType based on the fully qualified name



Is there somthing equivalent or better in the Eclipse AST/JDT APIs for such a need ?

Thank you in advance
Regards,
Xavier


Re: How to resolve a referenced type in a compilation unit [message #632059 is a reply to message #632056] Mon, 11 October 2010 12:14 Go to previous messageGo to next message
Xavier Coulon is currently offline Xavier CoulonFriend
Messages: 58
Registered: July 2009
Member
Never mind, I found how to resolve my initial problem using a custom ASTVisitor and bindings.

The initial issue was to evaluate a given annotation attribute value on specific Types.
Using an ASTVisitor like this seems to do the underlying job:
@Override
	public boolean visit(TypeDeclaration node) {
		for(Object modifier : (List<?>)node.getStructuralProperty(TypeDeclaration.MODIFIERS2_PROPERTY)) {
			if(modifier instanceof Annotation) {
				IAnnotationBinding annotationBinding = ((Annotation)modifier).resolveAnnotationBinding();
				annotationBinding.getAllMemberValuePairs();
...
			}
		}
		
	}


Xavier

Re: How to resolve a referenced type in a compilation unit [message #633023 is a reply to message #632059] Fri, 15 October 2010 06:50 Go to previous messageGo to next message
Walter Harley is currently offline Walter HarleyFriend
Messages: 847
Registered: July 2009
Senior Member
Xavier Coulon wrote:
> Never mind, I found how to resolve my initial problem using a custom
> ASTVisitor and bindings.
>
> The initial issue was to evaluate a given annotation attribute value on
> specific Types. Using an ASTVisitor like this seems to do the underlying
> job:
>
> @Override
> public boolean visit(TypeDeclaration node) {
> for(Object modifier :
> (List<?> )node.getStructuralProperty(TypeDeclaration.MODIFIERS2_PROPE RTY)) {
> if(modifier instanceof Annotation) {
> IAnnotationBinding annotationBinding =
> ((Annotation)modifier).resolveAnnotationBinding();
> annotationBinding.getAllMemberValuePairs();
> ..
> }
> }
>
> }

It sounds like this might also be a problem you can solve with an
annotation processor. Doing it that way
(javax.lang.annotation.processing.Processor) will mean that you have a
standardized solution that can also run at the command line and that
does not depend on Eclipse.
Re: How to resolve a referenced type in a compilation unit [message #633259 is a reply to message #633023] Fri, 15 October 2010 21:30 Go to previous message
Richard is currently offline RichardFriend
Messages: 1
Registered: October 2010
Junior Member
Some really insightful information here. Thanks a lot for sharing. It is well appreciated Smile
Previous Topic:How to cleanly get ICompilationUnit for StorageEditorInput?
Next Topic:Ant build on Run?
Goto Forum:
  


Current Time: Thu Apr 25 22:46:10 GMT 2024

Powered by FUDForum. Page generated in 0.02841 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top