Generating Method Call Diagram in Java Project [closed] - java

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 2 years ago.
Improve this question
I have a source code of a java Project and i want to trace for every method, what method called inside that method.
example:
foo(){}
bar(){}
bar2(){
bar();
}
foo2(){
bar();
bar2();
}
main(){
foo2();
}
the result will be like:
main
|_foo2
|_bar
|_bar2
|_bar
any add-ons or plug-ins is okay and since it is an internal project of a company, i hope the tools is offline. For the result, it doesn't have to be like that, graph or anything is okay.
Adding some information:
I cannot edit the code
I cannot run the code
but (maybe) i can add them to eclipse IDE
The project is either in WAR, EAR, or JAR type

Firstly, If you are use Eclipse as your IDE, then you can right click on a method and choose "open call hierarchy"
then eclipse shows all the methods that call the selected method and all the methods that are called from the selected method .
Secondly We can find it by programming.
ie, by using the eclipse JDT internal classes, these are the same classes used by eclipse to show whose calling the method.
initially we need to importing below two classes,
import org.eclipse.jdt.internal.corext.callhierarchy.CallHierarchy;
import org.eclipse.jdt.internal.corext.callhierarchy.MethodWrapper;
And then Create a method to get callers, and that takes the selected method as an input.(please note it will be your API type method) you should call this method from your program for find which methods call what..

If it’s a pure Java project with no other framework, one way to do this would be to use AspectJ to intercept every method, and put an entry in Thread local map for method name to counter + 1. If main is 0, foo2 would be 1, bar and bar2 would be 2, and so on.
See this section of AspectJ docs.

All you need is to use ajc compiler instead of javac. It makes you able to use AspectJ. Using aspects you can define Pointcut and log what you need.
Another option I think, could be analyze a stacktrase after all calls.

You can use a Plugin like Code Iris from IntelliJ

Related

List of GUI functions built in NetBeans? [closed]

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 4 years ago.
Improve this question
I am using the drag-and-drop style of GUI ( I can't write GUI code yet ).
I only know a few function like: setVisible, getText, setText, etc.
But I want to learn all the functions for the buttons or textfields that I can use
If you mean the Swing GUI:
The actual methods you can call and fields you can access in your code are exactly what you'll find in javax.swing in the Java documentation corresponding to the class you're looking at.
For example, if you have a JFrame, you can find all the methods in javax.swing.JFrame.
Properties are derived from the above Swing methods (mostly in a "remove the 'set' and 'get' manner").
For example, if you have a JFrame, you might see a background property in Netbeans, and you can find setBackground and getBackground in the documentation.
Bindings seem to be NetBeans specific, but these are also based on the properties.
For example, there's a background binding and also a background property.
Events are a bit more complicated - for example, all the mouseX events correspond roughly to addMouseListener and addMouseMotionListener, where the corresponding MouseListener and MouseMotionListener parameters has the mouseClicked, mouseEntered, etc. methods.
If you mean AWT, I imagine something similar would apply for that (but just taken from the java.awt package instead).
That's not to say every method appears in some form in the NetBeans UI - it wouldn't make sense for something like update to appear there, since that's something you need to decide when to call yourself during runtime.
Whenever you intend to study all the methods of a class, the first thing to check should be the documentation entry of that class (if exists). In general, it should contain some of the methods you are interested about and looking at the documentation entry of the parent class and the parent class of the parent class and so on should reveal all the knowledge you are interested about, therefore, this could be the learning algorithm:
define a set of classes you intend to study
open the documentation of the class
add the parent class/interface to the list described in step 1
create a list of methods you are interested about (probably you are not interested in all of them in all the cases)
go through the methods one by one
reread your lists and check whether you think there are items you might not be remembering some important details and if there are so, reread their entry. Repeat this step until there are no such items
If you already have satisfying theoretical knowledge, but you are not sure about the practice, you might want to create some small sandbox projects to try the things you just learned.

List of functions that were executed during a program in java (using eclipse)

I am currently working on an open source project. I am new in this field. My work is adding a new feature to the currently existing codebase.
However, I do not know where to add my code in the project. The projects contains hundreds of files, classes, and thousands of methods. It is not literally possible to go through each and every line of code.
Eg: Suppose I am working on a project called Calculator. I have the whole source code. Now I type 2 + 3 and hit enter. I get the result 5 which is displayed. But, I need to find where this exact calculation takes place in the source code. For instance, if a method is defined as :
public int add(int a, int b) {
return (a+b);
}
So I need to find this method, whose name is not known to me.
I am currently using the Eclipse IDE.
Can you help me? Or am I approaching this completely the wrong way? If so, how should I go about contributing to open source projects?
I'm not sure if this is exactly what you are looking for but you can access a thread's stackTrace that contains the information about the currently called methods. Therefore you could watch this for getting to know which methods are called.
See this question for how to access the stackTrace.
To access the running code to implement some sort of log mechanism you could use instrumentation to get your code into the running program if you don't have direct access to the code.
This logging method might also be worth a look

Implementing UML diagram to Java [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
How would I go about implementing/converting this UML diagram to Java code? Do I have to use constructors?
Here is the diagram:
Answer to your Question: No you dont have to use constructors, none of the classes shows one so youre fine with just the default constructors beeing generated while compiling.
Here is what you can do based on the assumption that this pictures information is stored in a understandable format (like for example extracted from IBMs Enterprise Architect).
1.) Learn Java so you can transform for example the box "CEO" into this:
public class CEO extends Employee{
#Override
public void printDetails(){}
}
2.) Use a IDE/ Tool that can generate such class skeletons based on your diagramms information. ArgoUml, Enterrpise Architekt, BlueJ, IntelliJ and stuff, just google for "java uml to code"
3.) Write a Tool that parses UML pictures metadata and generates the appropriate code.
In your case you probably want to go for 2.) and then hire a application developper to fill the generated skeletons with functionality.
Edit: As other comments suggested there is a problem with your diagramm. Before programms like in 2.) above can parse the information to generate class skeletons it must be understandable what the relations (arrows) in your diagramm express. Look at the relation between for example Office and Departement. I as a human can interpret this as "yeah that arrows probably do tell that a office extends from a Departement rather than telling me that a Departement has a reference to a Office and vice a verse". But that a tool can make this decission it must be clear what of both is meant. Usualy this arrows would suggest both classes holding a references to the other one and you probably rather want a single arrow pointing from Office to departement that has a Label "extends" or "implements" attached.
First of all, you will have to understand what each notation in UML means based on which you can convert those to java classes.
Below is a nice article that attempts to explain.
http://java.dzone.com/articles/uml2-class-diagram-java
Use UML tool ArgoUML. It is based on java. ArgoUML is the leading open source UML modeling tool and includes support for all standard UML 1.4 diagrams. Default constructors is a good practice as in some case like singleton pattern you have a private constructor.

Is every cyclic references between Java packages bad? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
Improve this question
I've been using Sonar code quality management platform for some time, and for the most cases I find it very helpful in revealing hidden design flaws of my code base.
However, there's one rule that gives me more annoyance than help and that is its check for 'cyclic package reference' violations.
I guess I fully understand where such a dependency between packages is a bad thing. For example, in a typical 3-tier presentation/service/persistence layered design, it's almost always a bad idea to let the database handling code have a reference back to UI related classes. I have no problem with calling it a 'violation'.
But let's consider other cases, i.e. like designing an IDE-like application. Say, we have a main package which contains an Application interface, which defines List<View> Application.getViews() method to reference application's views.
However, when the View interface has an Application getApplication() method to refer back to its parent application, which I believe is a quite common design, it will introduce a cyclic reference, provided each of the interfaces are separated in com.myapp.ui, and com.myapp.ui.view respectively.
Of course, you can just put the View interface into the com.myapp.ui to break the cycle. But when you have various other view related APIs in com.myapp.ui.view, many of them another abstract APIs like AbstractView, ContentView, AbstractContentView, etc. I wonder if it's more advisable to keep them in separate packages for a management purpose.
And consider the said application has many other similar cases like com.myapp.ui.action, com.myapp.ui.perspective, etc. which would really make com.myapp.ui package crowded if we are to put them all in there.
So, what approach do you suggest to handle such a situation? Are really every cyclic package references a bad thing? Or if I have to live with them, how do you configure Sonar to check only real, problematic cycles?
Every absolute -- except this one ;) -- is going to be wrong some of the time. So, is every cyclic reference bad? No. You have to use your judgement.
But if you do introduce a cyclic dependency, it's worth asking if you really need it, and why. The tl;dr is that more often than not, you may find that breaking the cycle can improve your modularity, and in particular your ability to test components separately.
To use your example, does a view really need a getApplication(), which presumably returns a relatively "heavy" object (ie, one that itself needs a database, network, etc etc)? Maybe... but maybe not. If what you really need from that getApplication is something with a few callbacks (such as when a user initiates some action), then it could be useful to create an interface in some common package for that callback. So, rather than:
com.foo.app.Application
com.foo.view.View
Application getApplication()
You'd have:
com.foo.common.Callback // maybe just a Callable, Runnable, etc?
com.foo.app.Application
provides a Callback for some action foo
com.foo.view.View
Callback getFooCallback()
The question you should be asking is: what does that give me? It could be that you have to stub out so much that it doesn't give you much -- though that may suggest you can break apart your classes some. But it could be that it actually makes it easier to test your view, because now your unit test can (1) test the view without spinning up a whole application, and (b) provide a "dummy" callback that does something like saving a string that describes the action, and then your unit test asserts that it saved the right string.
And indeed there is an open JIRA ticket to prevent considering a cycle between father/child packages as a quality flaw : http://jira.codehaus.org/browse/SONAR-3452

What is AspectJ good for? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
First let me note, that I use AspectJ and I like it, but what else can I do with it.
I know AspectJ can be/is used for Logging. In some cases it is used for Transaction controlling – mostly implemented in conjunction with annotations.
AspectJ can also be used to enhance classes with (code-generated) methods, like Spring Roo does.
But I believe AspectJ and AOP in general, can be used for more than: logging, transaction controlling, and simulation partial classes.
So what are other useful use cases for AspectJ and AOP?
permission check
interrupt action that takes too long
run action in separate thread or even in context of different process or event on other machine
monitoring
preparing any data / environment before call and processing results after call
opening / closing resources
EDIT
Although many years passed since I gave this answer I decided to add the following to make the answer more complete.
security check.
fixes of incorrect or behavior of API that you cannot change. For example boolean method that returns false in some conditions but should return true. You can fix this using AspectJ.
The Wikipedia entry gives you a few more examples (but not that many). Typically, Aspect Oriented Programing should be use only to implement simple behaviours that are not part of core concern of a class and are common to different classes. As soon as you begin to put too much logic in your aspects, the code becomes really unreadable.
The aspect you suggest (logging, transaction, ...) are the most commonly used. I would add security as well.
One can use AspectJ for enforcing some (design) rules.
like every controller method need some special annotations
every service/frontend/dto class must be located in a service/fronten/dto pacakge
more mature thinks like: checking that setters do not have any logic.
Inject Mocks in classes that otherwise would create new instances by using new.
Assume you have this code:
public void sendInvitationEmail(String address) {
InvitationEmail email = new InvitationEmail();
email.sendTo(address).send();
}
And need to replace email by an mock. Then you could use an Aspect (#Pointcut("call(InvitationEmail.new(..))")
) to "inject" a mock. -- #See Blog JMock and AspectJ by Daniel Roop, as well as Spring Roo`s #MockStaticEntityMethods (Mock Static Methods using Spring Aspect)

Categories