Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » How to resolve an ITypeBinding from a String(Literal)?
How to resolve an ITypeBinding from a String(Literal)? [message #1737972] Thu, 14 July 2016 14:38 Go to next message
Sven Strohschein is currently offline Sven StrohscheinFriend
Messages: 2
Registered: July 2016
Junior Member
Hi,

I have a standalone application which analyzes source code via the ASTParser#createASTs(...). This is working fine and all bindings can get resolved.

Now I want to check if there are fully-qualified Strings which are pointing to a class. So my ASTVisitor#visit(StringLiteral) method takes the String and checks if there is class with that name (for example "java.lang.String") on the classpath via Class#forName(...). But now I need also the ITypeBinding for this type and I find no working solution.

I tried to use org.eclipse.jdt.core.search.SearchEngine and also theAST.newSimpleType(theAST.newSimpleName("java.lang.String")).resolveBinding() but without luck.

Has someone an idea how to load an ITypeBinding with a String (IJavaProject seems not to be available when the ASTParser-API is used).

Thank you in advance.

Regards,

Sven
Re: How to resolve an ITypeBinding from a String(Literal)? [message #1738111 is a reply to message #1737972] Fri, 15 July 2016 18:55 Go to previous messageGo to next message
Stephan Herrmann is currently offline Stephan HerrmannFriend
Messages: 1853
Registered: July 2009
Senior Member
If you want to go the search-route, you would probably need some kind of bridge, mapping the runtime classpath (reflection) into an IJavaSearchScope. I don't think this is viable as classloaders normally have no "list-all" functionality.

When going via AST, first you'd need to assemble the type from its name components, see the javadoc of class QualifiedType. Still that wouldn't cut it in general, because resolveBinding() can only find bindings for types that have previously been seen during compilation (i.e., during createASTs() with resolveBindings=true).

I actually wonder, whether your's is a valid requirement for bindings, because bindings represent cross-references in java programs. A string literal is not a cross reference in that sense. So let me ask: why do you need the type binding corresponding to that string literal?

OTOH, if you have control over the source code being analysed, you might want to write Foo.class.getName() rather than "com.mycomp.Foo", but of course I don't know the context your use case.
Re: How to resolve an ITypeBinding from a String(Literal)? [message #1738128 is a reply to message #1738111] Fri, 15 July 2016 21:31 Go to previous message
Sven Strohschein is currently offline Sven StrohscheinFriend
Messages: 2
Registered: July 2016
Junior Member
Hi,

thank you for your answer.

I have no control over the source code. The tool analyses existing source code, so I can not avoid that the developers use Reflection Wink but the tool should recognize it.

The tool analyzes the code via JDT and uses the information to build a custom model for the analyze phase. I have already a bridge to convert JDT -> Reflection -> my custom model, but the bridge is terrible when constructs like anonymous classes, Lambdas or local inner classes are used. For example there are random numbers generated to the class names and these depend from the used Java compiler. My tool is running with sources which are compiled by the Oracle java compiler and JDT uses another compiler... I'm now trying to simplify that process by removing the bridge (JDT -> my custom model instead of JDT -> Reflection -> my custom model). That is already working fine, but the last thing to get solved is to load the ITypeBinding by a String, so I can convert String -> JDT (ITypeBinding) -> my custom model, without using Reflection or the bridge.

The tool has only to load ITypeBindings by Strings for classes which are also analyzed. Is createASTS(...) compiling all classes before starting the ASTVisitors? Then the ITypeBinding should be available, but I haven't yet found a working solution to load it.

When there isn't a better solution, I think I will have to introduce a pre-stage: Analyze the complete source code to get all types, cache all these types (for example with a map with the class name as the key and the ITypeBinding as the value) and after that analyze the complete source code again... When a StringLiteral is detected, I can load the ITypeBinding via the cache. I hope there is a better solution, because this will be very slow and not comfortable. Smile

Thanks and regards,

Sven

[Updated on: Fri, 15 July 2016 21:39]

Report message to a moderator

Previous Topic:An internal error occurred during: "Building workspace".
Next Topic:Neon errors/warnings from files not in a source tree
Goto Forum:
  


Current Time: Fri Oct 11 15:17:09 GMT 2024

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

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

Back to the top