how to remove unused class in java project? [duplicate] - java

This question already has answers here:
Find unused classes in a Java Eclipse project
(3 answers)
Closed 7 years ago.
I have big project my manager dedicated to me for remove unused Classes form the project.
i finding good tool for it which give me list of classes which are not used in project..
can apache hama do it?? or suggest any other tool

You can try
Unused Code Detector
An eclipse plugin which might help you. To add to that if the classes are being used via reflection then there will be some problems.
Also read:
The problems found by UCDetector, are only suggestions. Before changing code, you should really know what you are doing! If UCDetector tells you, that there are no references your code still may be used by:
Reflection
Frameworks like Spring, Hibernate or Eclipse which declare dependencies in property files, xml files (extension-points) ...
Third party code, which is using your API
Jars in your workspace
To be continued...
Change visibility may cause problems with inheritance, reflection, instantiation...
It's good idea to do a full text search in workspace for the piece of code, you want to change.

IntelliJ IDEA has an excellent static analyzer that includes dead code detection. For a limited set of languages and frameworks, it is also available in the free community edition.

Related

How can I use the nonfree modules in OpenCV for Java?

I want to use SURF for feature detection found here and use it in a Java applicaion, however the nonfree modules are not included in the library by default, as they are patented.
How do I access this module? I have searched and tried a few things but none have worked; many focusing on Android, which I don't fully understand.
Can I add it when using cmake to build the library? or is there a better mean?
Actually when I was using cmake, nonfree library module was selected as default. So you should have this module built in your lib folder. Nonfree module includes some functionality that may be patented in some countries. So you should be careful if you are building a commercial application adn you will sell it in one of these countries.
To use it, you need to add it to your references and include the headers.
EDIT
I checked the docs here : http://docs.opencv.org/java/ there seems to be no module with name nonfree and there is no class related to sift or surf. I thought it should be same with c++ library but I was mistaken.
On the other hand, people claim that they built it for OpenCV4Android. That means it can be somehow compiled unofficially for java as well but no one seems to overcome this. Like here : http://answers.opencv.org/question/11185/how-can-i-generate-java-bindings-for-non-free/
Also there is a issue here about this : http://code.opencv.org/issues/2825
So at the moment no solution out yet. Implementing a JNI and loading compiled c++ nonfree lib may solve the problem by the way.
One more edit :)
In a tutorial, someone implemented JNI for nonfree module to be used with android. I don't have enough knowledge to try it for java at the moment. But a volunteer would be nice to try this with java :
http://web.guohuiwang.com/technical-notes/opencv_nonfree_android_jni_demo

Removing unnessary classes from third party libraries and JARs [duplicate]

This question already has answers here:
Java - How to reduce the size of third-party jars to reduce the size of your application
(3 answers)
Closed 8 years ago.
I need to remove unused classes from third party JARs. Why tools should I use?
I already tried to use ProGuard. However, it removes only unused classes from the project itself but the library jars - third party - always remain unchanged.
You can create an uber jar and then use ProGuard. Repackaging library classes into jars would be a challenge, but from the spirit of your question you will prefer the uber jar as such.
As other posters have commented, you still need to be careful about classes loaded through the so much abused and misunderstood reflection mechanism.
#Joonas Pulakka is right. But if you still really want to do this and be sure that your application will not fail for ClassNotFoundException run your application with option -verbose:class, perform all usecases that exist, take the log that contains all loaded classes. Then take list of all classes of your third party library and file all classes from your library that have been never loaded. Then create alternative jar file that contains only "needed" classes and pray :)
Good luck.
It is a good thing to know what classes and libraries you are using, and even if there is risk (as pointed out by Peter) to removing unused stuff, there is cost in carrying any kind of excess baggage, and you shouldn't just keep accumulating. If you use reflection, then get a handle on what you are using it for, and systematically get rid of what you don't need. There are benefits to a leaner code-base that you understand better.
Java only loads class as they are used. Removing classes can only cause you problems and won't help you at runtime. 36 MB of code isn't that much given only a portion of it will be loaded. How much memory do you have? Most PC have at least 2000 MB these days If you are downloading your applet or Java WebStart application over a slow link I would imagine you are using pack200 (to make the jars smaller) and have included the minimum of libraries already.

Statically checking a Java app for link errors

I have a scenario where I have code written against version 1 of a library but I want to ship version 2 of the library instead. The code has shipped and is therefore not changeable. I'm concerned that it might try to access classes or members of the library that existed in v1 but have been removed in v2.
I figured it would be possible to write a tool to do a simple check to see if the code will link against the newer version of the library. I appreciate that the code may still be very broken even if the code links. I am thinking about this from the other side - if the code won't link then I can be sure there is a problem.
As far as I can see, I need to run through the bytecode checking for references, method calls and field accesses to library classes then use reflection to check whether the class/member exists.
I have three-fold question:
(1) Does such a tool exist already?
(2) I have a niggling feeling it is much more complicated that I imagine and that I have missed something major - is that the case?
(3) Do you know of a handy library that would allow me to inspect the bytecode such that I can find the method calls, references etc.?
Thanks!
I think that Clirr - a binary compatibility checker - can help here:
Clirr is a tool that checks Java libraries for binary and source compatibility with older releases. Basically you give it two sets of jar files and Clirr dumps out a list of changes in the public api. The Clirr Ant task can be configured to break the build if it detects incompatible api changes. In a continuous integration process Clirr can automatically prevent accidental introduction of binary or source compatibility problems.
Changing the library in your IDE will result in all possible compile-time errors.
You don't need anything else, unless your code uses another library, which in turn uses the updated library.
Be especially wary of Spring configuration files. Class names are configured as text and don't show up as missing until runtime.
If you have access to the source code, you could just compile source against the new library. If it doesn't compile, you have definitely a problem. If it compiles you may still have a problem if the program uses reflection, some kind of IoC stuff like Spring etc.
If you have unit tests, then you may have a better change catch any linking errors.
If you have only have a .class file of the program, then I don't know any tools that would help besides decomplining class file to source and compiling source again against the new library, but that doesn't sound too healthy.
The checks you mentioned are done by the JVM/Java class loader, see e.g. Linking of Classes and Interfaces.
So "attempting to link" can be simply achieved by trying to run the application. Of course you could hoist the checks to run them yourself on your collection of .class/.jar files. I guess a bunch of 3rd party byte code manipulators like BCEL will also do similar checks for you.
I notice that you mention reflection in the tags. If you load classes/invoke methods through reflection, there's no way to analyse this in general.
Good luck!

java tool for debugging

Currently we are studying the Java based tool which is primarily Reporting tool.It was developed in 2000/2001 period and uses many open source libraries like Apache Avalon/Mx4J.Adaptor/edu.Oswego(java concurrent package) etc. Tool uses jdk 1.3.1 and goal is to upgrade to jdk 1.5.We have also been asked to remove these 'outdated' packages and replace by standard Java packages if possible.
Unfortunately we have the code available for study but lacks any documentation and really difficult to track the flow(Total number of classes written might be more than 1000) during debugging.
Whats the best way to understand this kind of tool? Any graphical tool to see the relationship between the classes?
Thanks,
SR
You could try some of the Source Code Analyzer plugins to eclipse. Tools like DIVER or X-Ray might be useful.
That's a common problem (unfortunately), and again unfortunately there is no easy solution.
There are many tools to help you (see below), but these are only helpers, they will not solve the problem for you.
I have found that a systematic approach is best. There is a good article on this:
Swallowing an elephant in 10 easy steps , about understanding a large, undocumented system. It's about Perl, but the ideas are independent of language.
Some tools that might help:
Step through interesting parts in a debugger (e.g. Eclipses debugger)
Use Eclipse's "Call hierarchy" and "find references" to understand which part of the code uses what
Run tests with simple input data, understand what they produce
Write javadocs into the code documenting what you found, possibly correcting existing docs
Use tools to visualize class dependencies. I have unsed JDepend with some success; there are many others.
Eclipse (and newer version of NetBeans and perhaps IntelliJ) have wonderful tools for analyzing large codebases:
Call hierarchy (CTRL + ALT + H) - you see the hierarchies of calls to/from a given method
Type hierarcy (F4) - you see the whole inheritance structure
Data hierarchy
Right click on item > References
many different search options
Any graphical tool to see the relationship between the classes?
If you want to see the relationship between classes you could try Green UML . It creates a nice UML class diagram out of your repository. It works on Eclipse.
I hope that helps.
You can do it easily in NetBeans.
Select the method signature and press ALT+F7 (or alternately right click and then click "Find Usages") this would show you from where a particular method is being called.
Second option is little hectic but may give some results. Configure log4j for your project and try to give the proper logging code in each method.

Call hierarchy and/or data flow tool for Scala

Is there an IDE/Tool/script/something that can show call hierarchy and/or data flow in Scala+Java programs (preferably from source code).
Or (as a backup plan) is there a tool that can show it using Java bytecode? (And preferably give the option to go to source code, if provided by user).
All that, preferably integrated into an IDE and/or Maven :-)
The requirement to support Scala is crucial in this question. I Already know of and use such tools for Java, in 3 IDEs. They do not work very well (actually: at all) when Scala is involved.
TIA
Poor man's call hierarchy: Comment the method out and see where your red squigglies show up. [/me ducks]
Did you tried Eclipse?
SBT can do that. You'll have to check it out to get more information, because I haven't done it.
EDIT
Sorry, I confused things. SBT can generate component dependencies, not call hierarchy.

Categories