|
Re: How to resolve an ITypeBinding from a String(Literal)? [message #1738111 is a reply to message #1737972] |
Fri, 15 July 2016 18:55 |
Stephan Herrmann 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 |
Sven Strohschein 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 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.
Thanks and regards,
Sven
[Updated on: Fri, 15 July 2016 21:39] Report message to a moderator
|
|
|
Powered by
FUDForum. Page generated in 0.03055 seconds