like:
import com.xxx.utility.*;
class MyClass{
public static void main(String[] args){
MyUtiliy ut = new MyUtiliy();
MyUtility.doAdd(5, 6);
.......
}
}
When put the "." after MyUtiliy, eclipse will tell you all the methods you can use, how does eclipse achieve this?
Does eclipse use the reflection on the fly? (like the answer of this thread? )
The architecture of the eclipse software is describe here, in the section 6.1.2. Java Development Tools (JDT) it briefly describes the incremental build system used. That system would have all the relavent information to populate the autocomplete mechanism.
For the exact mechanism, you would have to look at the eclipse source code.
Yes Eclipse (and any other Java IDEs) uses reflection.
If fact Eclipse uses a ClassLoader for each project's libraries, so it load the classes in jar files, and after that everything is easy, it can get information using reflection.
By the way java IDEs not only use reflection, but also read class debug info, to extract parameter names, and so on.
There is an explanation in this article. Basically the Eclipse Java compiler builds an Abstract Syntax Tree (AST) of your code which lets it find all the information it needs for autocompletion very quickly.
So it is not using reflection for this, rather it is compiling the code in to an internal form for quick access.
When no source code is available (you just have a .class file) it is still possible to construct the part of the AST containing the class methods and types which are needed for completion. This appears to be done by reading the .class files directly rather than using a class loader (org.eclipse.jdt.internal.compiler.classfmt.ClassFileReader)
Related
I'm having an issue with java reflection.
How can I load a .java file or the whole project then analyze them?
input : .java code
output : analyzed class, method, relations between classes, attributes. v/v
Analyzing .java files is a lot more difficult than it sounds, as they are pure text and therefore requires textual analysis inorder to get something. A tool like PMD knows that and performs static code analysis on .java files.
https://pmd.github.io/
Analyzing .class files however is alot easier. For this task one need to create a custom class loader object (URLClassLoader should work) and use it to search and load all of the Class objects. Then one cause those objects' methods to get information on those classes. A tool that performs static code analysis on .class files is FindBugs.
http://findbugs.sourceforge.net
Hopefully this helps you bit
Is there any way to collect the information of a Java Class through a plugin?
I wanted to collect information such as the package it belongs, the imports it has, if it has implements or extends.
After collecting necessary information the intend is to copy it to a text field.
You can collect the information of all the files belonging to a Java project using the Eclipse JDT plugin. You may use the Eclipse's AST parser also in combination.
The JDT Core component is the plug-in that defines the core Java elements and API. This Core component can be included inside our own plugin and can be used to search, compile and manipulate Java code outside an IDE.
You can find a start-up tutorial from here.
You can use ASM library to parse class file and extract any information from it.
In my Java application I have a class Foo
class Foo {
String field1;
String field2;
}
I would like to have some generated code which uses reflection on the fields in this class
(Imaginary template language)
#for each Field $f in Foo.class.getDeclaredFields()
#writeFile $f.java
public #interface $f {
}
The end goal is to have Field1.java and Field2.java with just a simple #interface definition inside each.
Is there a templating language available which could do this generation as part of a Maven build process?
The closest I have been able to find is JET, but this project seems more geared towards generating Java source to be available at runtime, not at compile time. In theory I could probably make this work using AntRun along with several Javac and Java tasks, but it would be cumbersome.
The actual use case which I need this for is generating BindingAnnotations for Google Guice (which will be used in GWT source, so they must exist as .java files at compile time).
I would suggest two options here:
Apache Velocity: it provides a template language looking close to what you describe. Look into it here. You could probably be interested by their engine.
GWTP seems to do something similar to what you are wanting to do. It looks likes they are using annotation processor to perform their code generation. Here is a processor example and their project home is here.
Take a look at Acceleo it is based on XSL Templates to generate source code .
I used it with EMF to generate source code from a Data Model designed by the user.
I would like to write toy IDE for Java, so I ask a question about one particular thing that as I hope can help me get started.
I have editor implemented on top of swing and i have some text in there. There is for example:
import java.util.List;
Now I need a way to send "java.util.List" string to a method that returns me all the information I may need including JavaDoc document.
So is there any tool that can set up classpath with libraries, that would parse every string I send and try to find if there is any Class/Interface with documentation to return?
So is there any tool that can set up classpath with libraries, that would parse every string I send and try to find if there is any Class/Interface with documentation to return?
AFAIK, no. There is no such free-standing tool or library. You will need to implement it yourself. (Don't expect that writing a Java IDE is simple ... even a "toy" one.)
Libraries will have class files, which will not have javadocs.. So it is not clear what you want to do.
There are many byte code engineering tools to analyse and extract information from class files. For example asm or bcel. Javassist allows to process both source and byte code, so may be close to what you need.
You could use html parser to get the javadoc and other info from the web using the full path to the class (including package names to construct the correct URL per class). This will of course depend on the version of java you are using.
You can also use the javadoc tool from within java to generate the desired documentation from java source files (which can be downloaded from the web). The source code of the tool could also help you out. See http://java.sun.com/j2se/javadoc/faq/#developingwithjavadoc
Lastly, if you need information based on runtime types in your program, you might want to check reflection capabilities.
First you need to know How to print imported java libraries?. Then download java API documentation here. Once you find out imported libraries, open an inputStream in order to read appropriate HTML file.
Beware! This technic will only work when importing from jdk.
In Android applications, resources are specified in xml documents, which automatically are built into the R class, readily accessible within the source code as strongly typed.
Is there any way I could use a similar approach for a regular Java desktop application?
What I'd like to accomplish, is both the removal of strings from the code (as a separation of "layers", more or less) and to make it easy to add support for localization, by simply telling the program to choose the xml file corresponding to the desired language.
I've googled around a bit, but the things I'm looking for seem to be drowning in results about parsing or outputting xml, rather than tools utilizing xml to generate code.
Eclipse's message bundle implementation (used by plugins for example) integrates with the Externalize Strings feature and generates both a static class and a resource properties file for your strings:
http://www.eclipse.org/eclipse/platform-core/documents/3.1/message_bundles.html
For this integration to work Eclipse needs to see org.eclipse.osgi.util.NLS on the class path. From memory, the dependencies of the libraries it was available in were a little tricky for the project I used this approach in, so I just got the source and have it as a stand-alone class in my core module (see the comments for more on that).
It provides the type safety you're looking for and the IDE features save a lot of time. I've found no downsides to the approach so far.
Edit: this is actually what ghostbust555 mentioned in the comments, but not clear in that article that this isn't limited to Eclipse plugins and you refer to your resources via static members of a messages class.
I haven't seen any mention of others using this approach with their own applications, but to me it makes complete sense given the IDE integration and type safety.
I'm not sure if this is what you mean but check out internationalization- http://netbeans.org/kb/docs/java/gui-automatic-i18n.html
Are you looking for something that parses XML files and generates Java instances of similar "struct-like" objects, like JAXP, and JAXB?
I came across ResGen which, given resource bundle XML files generates Java files that can be used to access the resources in a type-safe way.
http://eigenbase.sourceforge.net/resgen/