Java: Print every method called to console? - java

I need to understand a quite large Java project. I browse through it with eclipse and use the call hierarchy and all, but that doesnt get me quite the idea on what is happening when the project runs (it's a webservice).
Is there a possibility to print out every method call with parameters to console?
Basically something that puts
System.out.println("methodName, params: " + param1.toString());
in every method for me.
Some kind of framework that provides that for example?

You should try to use Aspect-oriented programming (AOP).
Here is an example that does more or less what you want: How to use AOP with AspectJ for logging?

Related

Disadvantage of direct object manipulation with BlueJ?

So I used BlueJ at uni for my first year and noticed how the workbench can be used to create an object and then directly call their methods , this seemed to be a very quick and easy way to test methods on the object and was a quick way of getting inputs for the methods parameters without having to print the outputs or something to ensure bject is functional. Having switched to eclipse there doesn't seem to be this option except for downloading a plugin. Is this kind of direct object creation a bad thing? Would it be bad practice to download this plugin and use it for testing?

Intercepting method of an tomcat application with aop

I have a problem which is hard to explain so lets get started:
Context: I have a application running on a tomcat server Lets call it "admin". The admin have an import/export function. Our own application is an extension to that and we need to gather some information when the "admin apps" use the import/export function.
Problem: The third party jar that contain the class ImportController is located there: ~/someFolder/admin/WEB-INF/lib/admin.jar. The goal is to gather the Old project ID and the new Project ID so that our extension can link our class to the right project. Since i know the method signature i though i could use AOP to do so.
Idea: The idea i came with is to put something like idHiJacker.jar that would contain a single pointcut and advice into the ~/someFolder/admin/WEB-INF/lib/ and enable load-time weaving. That advice would simply put the information into an xml file so our extension would be able to read it when we want to put the link back after a project import.
Also i must say I'm a pure newbie with AOP and web stuff. But i do not wish to import a monster for just doing this small operation with AOP. At the moment im reading on aspectJ and AspectWerkz
Question:
1) Am I in the right direction? Do you see anything that would make this idea not work at all?
2) If this is possible what would be the good practice to do it in a very clean manner?
3) Should i do it with AspectJ? AspectWerkz? Or Something else?
4) Am i doing this for nothing? Is there an easier way to do that operation?
Edit: Also if you have good tutorial to link with answer, it would be awesome
Thanks for your time and answer
Question:
1) Am I in the right direction? Do you see anything that would make this idea not work at all?
I can't see any reason why this will not work. Aspect Oriented Programming and cross-cutting concerns apart, the notion of advice is to execute some before or after some other pointcut and often to influence the behavior of that advised function. You are doing exactly that here.
2) If this is possible what would be the good practice to do it in a very clean manner?
There is some inherent chaos with aspects/advices -- since the control flow is hijacked more then a simple sequential reading of code is needed to understand whats going on.
3) Should i do it with AspectJ? AspectWerkz? Or Something else?
I have never used AspectWerkz but I have very good experience with AspectJ; especially in terms of the support here on stackoverflow and perhaps even more on its mailing list.
4) Am i doing this for nothing? Is there an easier way to do that operation?
Unless you can change the code of import controller or change the clients to make extra calls to do the linking thing this interception based approach seems best IMHO.
I have suggestion for a simpler solution - use a decorator pattern to wrap around the third party ImportController, put your functionality before the third party library is called. You should be able to do this since you seem to have access to the admin application.
This is essentially doing what AOP is doing, but using code. Your approach using load time weaver should also work, but is in my opinion complicated - if you absolutely plan to go this way, do use AspectJ.

Is passing code to a service class possible in Android/Java?

I'm trying to let a user "make his own service". most direct method I could think of is creating an empty service and letting him "fill it". he will write his own code and I'll compile it along with my project.
(If it's not understood, the code should be injected to the service programmaticaly and not manually).
There are limitations that require it's done this way.
Here's a post about programmatically compiling and running Java code,
How do I programmatically compile and instantiate a Java class?
This is pretty ugly though. You might want to look at having the use supply a script in a higher level language, and executing it via the android scripting environment,
http://google-opensource.blogspot.com/2009/06/introducing-android-scripting.html
There's also the SL4a project,
http://code.google.com/p/android-scripting/

Dynamic class creation in Java

I'm wondering if anybody knows a way to dynamically create a Java class, or more specifically a method in a Java class. I'm trying to do some unit testing, so I have code that I've already written, and I'm always modifying the code by adding extra System.out.println statements and then deleting them when I'm finished (otherwise the code gets too cluttered). What I'm trying to do is write a framework that can take a method, copy its code, add the System.out.println statements automatically, and then run the test on the copied method. This might also save time recompiling an entire Java application when I've only made a minor change to get some extra info while debugging.
Any ideas would be greatly appreciated.
You could just use a logging framework.
Sounds like you need aspects, i.e. AspectJ.
One option is to use cglib (Code Generation Library).
I don't have a specific example, but Aspect Oriented Programming (AOP) is what you want. Take a look at AspectJ.

Getting a call hierarchy in java

I am having real trouble tracking down a bug and it would help be a lot to know which method called a certain method. Is there an easy way to get a call hierarchy from java? Java is a small part of the app so I cannot compile and run the whole app in eclipse/net beans so I don't have access to an IDE debugger's call hierarchy.
Thread.currentThread().getStackTrace();
or
Exception ex = new Exception();
ex.printStackTrace();
It's fairly slow, but fine for debugging purposes. API docs here.
Java is a small part of the app so I cannot compile and run the whole app in eclipse/net beans so I don't have access to an IDE debugger's call hierarchy.
You dont need to run the app at all. If you make a project in Eclipse, you can use its built-in Call Hierarchy tool to find all of the possible places that call a method.
There is a trade off: The Call Hierarchy tool will give you ALL of the places from where a method is called. This is a good if you want/need to know all the possibilities. Neslson's suggestion of Thread.currentThread().getStackTrace() will give you the places from where a method is invoked during the process of you program for this invocation. The nuance is that you might not run through every code path during this invocation. If you are seeing specific behavior and want to know how you got there, use the getStackTrace() option.
Have you tried using Java's remote debugging capability?
Google search to get you started if you haven't
The best thing to do is throw an exception, immediately catch it and analyze the stack trace.
I believe that is how Log4J is capable of logging the calling method.

Categories