Eclipse JDT: How to Get a Data Model for Java Content

When writing Java code in the Eclipse IDE, press Control + Space to open the content support window.
For example, the content support window for System. will display all available fields and methods for the System class.

I need to access the โ€œdata modelโ€ for the content support window using code.
Using the example above, this is: the name of the System class is given, how can I get all the available fields and methods?
I spent some time on the source code of these three classes on grepcode.com:

 org.eclipse.jdt.ui.text.java.JavaContentAssistInvocationContext org.eclipse.jdt.internal.ui.text.java.JavaCompletionProposalComputer org.eclipse.jdt.ui.text.java.CompletionProposalCollector 

It seems like an instance of ICompilationUnit used to provide field names and methods.

Then I do not understand how to generate an instance of ICompilationUnit for a class in the jre system library or a third-party library? Or, if I did not read the code correctly, then how does the program name the names of fields and methods? (I donโ€™t need to worry about things bias and UI, only part of the โ€œdata modelโ€).

+5
source share
2 answers

It seems that the only option is to create a (temporary) compilation, which, in turn, requires a properly configured Java project. The JDT needs an infrastructure to know which JRE is used, what compiler options are used, etc.

See here how to set up a Java project, and here how to get a compilation unit.

The compilation block will look something like this:

 class Foo { void bar() { java.lang.System. } } 

and codeComplete() must be called with an offset that indicates the position immediately after System. .

+1
source

You can try using the java mapping API to get all the members of this particular class ( YourClass.getMethods() or YourClass.getDeclaredMethods() ),

To make it dynamic according to your input, you can use Class.forName(<fullClassName>) to get the appropriate class (see this post for more information on this).

The problem you are facing is that you have to provide the full name of the class, so you may need to check the import to find out which package you should look for the appropriate class, but this is the only way to go with this method.
Simple names are not always unique and therefore not suitable for such a search (explained here .

0
source

Source: https://habr.com/ru/post/1238417/


All Articles