Collecting Java class information - java

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.

Related

How eclipse deduces the methods of a class in a jar file

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)

Accessing information about Java libraries programmatically

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.

Automatically build resource class based of XML in Java

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/

How to use core Java classes in Papyrus UML (Eclipse)?

I'm new to Papyrus UML in Eclipse. I'm trying to make a class diagram of classes that also use standard Java classes (e.g. java.awt.Point). So for example: Class Tracker has a property Point location. However, if I click the property location, I cannot set its type to any standard Java class.
I tried Import from registered library but there are no options that include the standard Java packages.
How can I use these standard classes in my class diagram?
Some Papyrus/Java lover should write the core java profile and donate it to the papyrus project.
You can not use Papyrus with Java existing java code. Usually you model then generate a java code from the model.
You can try the reverse engineering feature but it would create model which is now not related to your existing java class.
The only solution I see is to use live code and model synchronization UML tool.
After much searching, i found a way..
Simply download the Java Core (JCL) API UML file I made here:
https://mega.nz/#!qVdAAAIL!gWstAOCyQFWVHTqxVrgnYssiOJZSAEN-rbxMIaZzMJo
And use the model->import->import package from user model functionality in papyrus in eclipse
Then you can use standard java library types outside of just primitives in your papyrus UML diagram.
I've tested this with the code generator and it works with one caveat, you end up with an extra local copy of the standard library packages in the generated project. Simply delete these and you can move on with your day. I'm not sure, but eclipse payprus codegen doesn't currently seem to generate the imports necessary, so you'll have to do that manually or let the IDE do it for you, but it sure beats having placeholder types.
If you're wondering how this was done, I used the following plugin:
https://marketplace.eclipse.org/content/jar2uml
To generate a UML file for the standard library jar file version 1.8, this is usually under the jre installation directory inside the lib folder.
This gave me a model with all the standard library packages within it, but the model was named rt.java.* instead of java.* which messed with the code generation process. So i simply removed the other packages other than java.* and made java the model itself instead of a package.
There is a java profile and library/package in Papyrus Software Designer extention.
You may install it via the market place.
See also: https://wiki.eclipse.org/Java_Code_Generation

How do I extract a single class plus all its dependencies from a library?

I got a big Java library but only need a small portion of it. How would I extract the main class and all of its dependencies? An automated solution would be preferred, but I can also live with something that generates a list of files I need to extract (I don't want to write that something myself ;-)).
This is similar to Tool or plugin to extract class and all of its dependencies from VS project - I just need it for Java (IDE in use is Eclipse).
There is a tool called autojar.
It works.

Categories