In the project allocated to me I have to understand a big Java project which uses Spring framework and angularjs for the frontend and I don't know where to start with. There is a lot of code running on the server (Jetty) and lots of logs generated. The back-end code is more of an interest for me than the UI side.
My question is where do I start? The Java code that we use for practice in college/academics very simple and had a 'main' method to start with.
In the big project I don't see a main method. How do I find out the 'starting point' of the project and then explore it further?
This question is somewhat similar but not exactly to Understanding a big company project in Java
One possible way to start exploring is find an important entry point to the application and then analysis layer to layer from code to code.
My following explanation is based on the information you have give in the comments of your questions. (application is based on angularjs and spring). It would be a good idea to adjust your question, so that these important informations are a part of the question (comments are temporary and could be removed without notice)
Start the application and find one of it's main functionality (or the functionality you want to extend on the next task).
Open the developer tools of your favourite browser and record all requests and responses. Trigger the action you want to discover. Analysis the records and find the corresponding REST call. Look at the request and response param and think about what this API should do.
Then you search for the corresponding REST-Controller inside the backend code. In most situation you can find it, if you search for some of the latter values of the REST-Call-URL path (which is bind to the controller or a controller method).
Now you have your entry point for that action and you can discover further on more or less similar as you would do it on a main method.
You can repeat this steps to get a understanding of the implementation of some features (one by one). Nevertheless there could be some important part of the application you will not find with this approach. But it would give you a starting point to explore the functions which are exposed by the web-interface to the user.
Depending on the architecture it might be that you find a place with some central core functionality. Then it would be a good idea to explore these core functions independently of the api to the frontend. If the application f.e. would take advance of a domain driven design approach you would find a layer where a functional (not technical) domain is designed. Then you will find the bigger picture here.
If you have anyone who know how the application is designed you should of course start you discovery by interviewing this person. If every one who has knowledge is not reachable, you have to look at some documentation or you have to discover the application on your own like I try to describe.
My real question is about how to look up the expectations on the methods (the 'contract' for a method) in Spring. I keep hitting questions, where unless I find some blogger or a stack-overflow that addresses that specific issue, there seems to be no informative documentation. Am I looking the wrong places? Do I need to buy some book?
In the current specific case: I have working looking up a user/password by making my SQL table map to Spring's defaults, but when a user is absent it's hitting a null pointer exception. I see JdbcUserDetailsManager's "void setUserExistsSql( anSQLString)", and I want to know if that sql-string should return a boolean? a null? and what it should be 'named.' Googling is not turning up any usage examples, nor any documentation. The javadocs I'm finding are uncommented. I can guess-and-test, but it seems there should be a better way to look-it-up?
Ok, I've been working with spring since version 1, and many other open-source projects follow the same pattern. Documentation is hard and expensive to produce, and programmers donating their time for free often don't want to write it. Spring though is one of the better projects as far as documentation is concerned.
However, I've always found it necessary to link spring's source code into my project. If you're using maven you can download the sources along with the jars, and tools like IntelliJ (and probably eclipse) will allow you to drill down into the source and to trace its execution with their debuggers.
With these types of projects it is almost always necessary at some point to drill down and read the source, and that's a good thing because the source is always up to date and always exactly describes the behaviour you're trying to use. Documentation on the other hand is often badly written using an informal language (e.g. English) and it can never accurately describe anything, especially if it's being written or read by someone who isn't a native speaker, which is often the case.
So, to answer your question -- look to the source.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 3 years ago.
Improve this question
What tools do you use to find unused/dead code in large java projects? Our product has been in development for some years, and it is getting very hard to manually detect code that is no longer in use. We do however try to delete as much unused code as possible.
Suggestions for general strategies/techniques (other than specific tools) are also appreciated.
Edit: Note that we already use code coverage tools (Clover, IntelliJ), but these are of little help. Dead code still has unit tests, and shows up as covered. I guess an ideal tool would identify clusters of code which have very little other code depending on it, allowing for docues manual inspection.
An Eclipse plugin that works reasonably well is Unused Code Detector.
It processes an entire project, or a specific file and shows various unused/dead code methods, as well as suggesting visibility changes (i.e. a public method that could be protected or private).
CodePro was recently released by Google with the Eclipse project. It is free and highly effective. The plugin has a 'Find Dead Code' feature with one/many entry point(s). Works pretty well.
I would instrument the running system to keep logs of code usage, and then start inspecting code that is not used for months or years.
For example if you are interested in unused classes, all classes could be instrumented to log when instances are created. And then a small script could compare these logs against the complete list of classes to find unused classes.
Of course, if you go at the method level you should keep performance in mind. For example, the methods could only log their first use. I dont know how this is best done in Java. We have done this in Smalltalk, which is a dynamic language and thus allows for code modification at runtime. We instrument all methods with a logging call and uninstall the logging code after a method has been logged for the first time, thus after some time no more performance penalties occur. Maybe a similar thing can be done in Java with static boolean flags...
I'm suprised ProGuard hasn't been mentioned here. It's one of the most mature products around.
ProGuard is a free Java class file shrinker, optimizer, obfuscator,
and preverifier. It detects and removes unused classes, fields,
methods, and attributes. It optimizes bytecode and removes unused
instructions. It renames the remaining classes, fields, and methods
using short meaningless names. Finally, it preverifies the processed
code for Java 6 or for Java Micro Edition.
Some uses of ProGuard are:
Creating more compact code, for smaller code archives, faster transfer across networks, faster loading, and smaller memory
footprints.
Making programs and libraries harder to reverse-engineer.
Listing dead code, so it can be removed from the source code.
Retargeting and preverifying existing class files for Java 6 or higher, to take full advantage of their faster class loading.
Here example for list dead code: https://www.guardsquare.com/en/products/proguard/manual/examples#deadcode
One thing I've been known to do in Eclipse, on a single class, is change all of its methods to private and then see what complaints I get. For methods that are used, this will provoke errors, and I return them to the lowest access level I can. For methods that are unused, this will provoke warnings about unused methods, and those can then be deleted. And as a bonus, you often find some public methods that can and should be made private.
But it's very manual.
Use a test coverage tool to instrument your codebase, then run the application itself, not the tests.
Emma and Eclemma will give you nice reports of what percentage of what classes are run for any given run of the code.
We've started to use Find Bugs to help identify some of the funk in our codebase's target-rich environment for refactorings. I would also consider Structure 101 to identify spots in your codebase's architecture that are too complicated, so you know where the real swamps are.
In theory, you can't deterministically find unused code. Theres a mathematical proof of this (well, this is a special case of a more general theorem). If you're curious, look up the Halting Problem.
This can manifest itself in Java code in many ways:
Loading classes based on user input, config files, database entries, etc;
Loading external code;
Passing object trees to third party libraries;
etc.
That being said, I use IDEA IntelliJ as my IDE of choice and it has extensive analysis tools for findign dependencies between modules, unused methods, unused members, unused classes, etc. Its quite intelligent too like a private method that isn't called is tagged unused but a public method requires more extensive analysis.
In Eclipse Goto Windows > Preferences > Java > Compiler > Errors/Warnings
and change all of them to errors. Fix all the errors. This is the simplest way. The beauty is that this will allow you to clean up the code as you write.
Screenshot Eclipse Code :
IntelliJ has code analysis tools for detecting code which is unused. You should try making as many fields/methods/classes as non-public as possible and that will show up more unused methods/fields/classes
I would also try to locate duplicate code as a way of reducing code volume.
My last suggestion is try to find open source code which if used would make your code simpler.
The Structure101 slice perspective will give a list (and dependency graph) of any "orphans" or "orphan groups" of classes or packages that have no dependencies to or from the "main" cluster.
DCD is not a plugin for some IDE but can be run from ant or standalone. It looks like a static tool and it can do what PMD and FindBugs can't. I will try it.
P.S. As mentioned in a comment below, the Project lives now in GitHub.
There are tools which profile code and provide code coverage data. This lets you see (as code is run) how much of it is being called. You can get any of these tools to find out how much orphan code you have.
FindBugs is excellent for this sort of thing.
PMD (Project Mess Detector) is another tool that can be used.
However, neither can find public static methods that are unused in a workspace. If anyone knows of such a tool then please let me know.
User coverage tools, such as EMMA. But it's not static tool (i.e. it requires to actually run the application through regression testing, and through all possible error cases, which is, well, impossible :) )
Still, EMMA is very useful.
Code coverage tools, such as Emma, Cobertura, and Clover, will instrument your code and record which parts of it gets invoked by running a suite of tests. This is very useful, and should be an integral part of your development process. It will help you identify how well your test suite covers your code.
However, this is not the same as identifying real dead code. It only identifies code that is covered (or not covered) by tests. This can give you false positives (if your tests do not cover all scenarios) as well as false negatives (if your tests access code that is actually never used in a real world scenario).
I imagine the best way to really identify dead code would be to instrument your code with a coverage tool in a live running environment and to analyse code coverage over an extended period of time.
If you are runnning in a load balanced redundant environment (and if not, why not?) then I suppose it would make sense to only instrument one instance of your application and to configure your load balancer such that a random, but small, portion of your users run on your instrumented instance. If you do this over an extended period of time (to make sure that you have covered all real world usage scenarios - such seasonal variations), you should be able to see exactly which areas of your code are accessed under real world usage and which parts are really never accessed and hence dead code.
I have never personally seen this done, and do not know how the aforementioned tools can be used to instrument and analyse code that is not being invoked through a test suite - but I am sure they can be.
There is a Java project - Dead Code Detector (DCD). For source code it doesn't seem to work well, but for .jar file - it's really good. Plus you can filter by class and by method.
Netbeans here is a plugin for Netbeans dead code detector.
It would be better if it could link to and highlight the unused code. You can vote and comment here: Bug 181458 - Find unused public classes, methods, fields
Eclipse can show/highlight code that can't be reached. JUnit can show you code coverage, but you'd need some tests and have to decide if the relevant test is missing or the code is really unused.
I found Clover coverage tool which instruments code and highlights the code that is used and that is unused. Unlike Google CodePro Analytics, it also works for WebApplications (as per my experience and I may be incorrect about Google CodePro).
The only drawback that I noticed is that it does not takes Java interfaces into account.
I use Doxygen to develop a method call map to locate methods that are never called. On the graph you will find islands of method clusters without callers. This doesn't work for libraries since you need always start from some main entry point.
I'm a new person in this area (plugin developing) and I want to create some kind of plugin for my app:
I've developed an android application and now I need to make a toolkit for the students for future work on this app. The idea is:
1) to make a manual for that app, so that students can read about classes and structure not in separate .doc file but inside Eclipse IDE, probably with some links to the code.
2) to make a supervise of the app's functions (so that students can check if all features (performance-UI design, connection to the external server/API's, etc) of the app are working properly, in case if they will change something). All these data should be in separate frame (looks like a toolkit). (I found information related to this here http://www.ibm.com/developerworks/opensource/tutorials/os-eclipse-jfeature/section5.html, but I'm not sure if it's gonna work for my idea)
I will be glad to get some links of tutorials that are related to my task as well as your suggestions for the set of the toolkit features (but also with links how to make it).
Thank you very much in advance! Hope to get your help :)
The thing for the manual is Javadoc, you can use it in eclipse with java as well as in android.
here are some links, first:
http://en.wikipedia.org/wiki/Javadoc
Then I can quickly explain why javadoc can be useful for you, first of all it allows to create a real manual (java API are created with javadoc) with (in my opinion) the easiest way you can imagine.
You have to use a special comment tag that is:
/**
*
**/
When you put this before any declaration (methods, class, interfaces, fields etc) it will be included in the javadoc.
You have standard things that can be added for example you can specify a description of the method, what it #return what #params it need and many other things, being very careful and precise you can link javadoc with each other, and create very complex and precise documentation.
In eclipse javadoc is useful because eclipse itself allows the user to interact with javadoc by default. For example if you want to know what a method do, just simply hang the mouse over the name of the method and a little yellow dialog will appear. If you also use ctrl-space you can have some tips also in it. Pressing ctrl-space shows for example all avaible public methods, with javadoc for each method you have the yellow description dialog. If you are instantiating a new object you can see how many (and what kind of parameters they have) constructor are definited and so on, I think it is very useful and important.
For the second things if I understand what you want probably something like JUnit (a unit for the testing) can be ok for you.
http://www.junit.org
JUnit is a unit for testing the code, can be fully integrated with eclipse.
In few word, for each part of the code you should write one or more test to check if its behaviour is correct. Once you've written some test you can run them automatically with an user-friendly interface that tell you how many test are failed, how many passed and what kind of error there are.
Why is useful to test each little (stupid?) thing of my code?
Imagine you have a working code (your code).
Imagine you have someone working on it (your students).
How can a user be sure that any change he/she do it's ok with the existing code?
He/she should run the program and check each functionality one by one in order to find an error.
You understand that this is impossible. so JUnit do it for you with just one click (and if you want also in background).
So the student can add the code, and run the tests in order to see if the pre-existing code is still working.
The students can also write his own test to test automatically if all is ok. JUnit in facts allow you to test each part of the code without depending from the other, in this way, you can also test an internet connection without being connected to the net just "mocking" the connection.
I let this part without explanation because It is a long and complicated part. I gave you that "input" to stimulate you to read about testing and XP programming.
Ah, and welcome to stackoverflow! if you like this answer and think that answer correct to your question you can check it with the little check on the left.
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.