I'm trying to listen to when a method on a class its called.
I have an ArrayList and I want to listen on each item when a method is called (not each method, just the one that I want).
In c# there is something called Action, where I can register methods that will be called when this Action is Invoked, inside a method for example, and also returns parameters to know which item was called etc.
There is something like that in Java?
Take a look at AspectJ (https://www.baeldung.com/aspectj). It allows you to define pointcuts that match your method so that a given code (the advice) is run when your method is called.
Related
What would be the correct way to verify that one behaviour is triggered when there is an argument being passed, and another behaviour is triggered when there are no arguments being passed when running a java app from cmd?
Since the main method is static it's a little tricky to verify, but I also feel that introducing PowerMock is a bit over the top just for that.
Basically I want to create an object with a constructor with no arguments if there are no cmd arguments, and create an object with a String argument constructor if there are passed arguments to the app.
I do not see your code, so I can only imagine how it looks like.
I can imagine that within the main method some logic is triggered, which results in one or another event.
I suggest thinking about moving the processing of the arguments to another class (ArgumentProcessor) which can be fed with a builder object or factory object in the constructor and it could have a process(String [] args) method that returns a runnable or whatever you want to achieve.
If you then feed the ArgumentProcessor with a stubbed builder/factory than I think it should be possible to check if the logic has been processed in the right way.
I have prepare interceptor in use. In one action class, I have an action method named testSomething(), and I also have an action method named prepareTestSomething().
The problem I'm facing here is that the prepare interceptor would invoke the prepareTestSomething() action method as if it was a preparing method for testSomething(), in which case it is not.
Is there a way to make the prepare interceptor to skip the invocation for certain action methods? Like for validation interceptor, we can use "excludeMethods" parameter.
Prepending the prepare word to an Xxx method is the Struts2 convention to tell the framework that it is the prepare() method for the Xxx action method. From the docs:
if the action class have prepare{MethodName}(), it will be invoked
Instead of doing other voodoos (like excluding methods) in order to make this voodoo work, simply change the method name, it is the only right way to go.
Call it initTestSomething(), initializeTestSomething(), preparzTestSomething()... whatever; but please, don't use a convention trying to make it work for something else. It is just... wrong.
I understand why addXXXListener() take an argument. But why is it in the removeXXXListener()?
Because you're passing the method a specific listener to remove. When you add a listener it gets added to a set. If you want to remove it, you have to tell the object which listener to remove from the set.
From the JavaBeans specification:
Invoking the add<ListenerType> method adds the given listener to the set of event listeners registered for events associated with the <ListenerType>. Similarly invoking the remove<ListenerType> method removes the given listener from the set of event listeners registered for events associated with the <ListenerType>.
We have Interceptor, we have custom interceptor where we can do all that we want to do before or after our action executes.
Then what is the need to use Preparable interface and implement prepare method for it?
Is this another option or there is some specific aim to do like that?
Well Preparable interface act in conjunction with Prepare Interceptor.This interface has one method defined prepare() and as its name suggest this method is responsible to allow the action to prepare itself.
Prepare interceptor calls prepare() on actions which implement Preparable. This interceptor is very useful for any situation where you need to ensure some logic runs before the actual execute method runs.So if you see this is some kind if init for your action class and it makes sure that before the Action's execute or any other method get called, this method prepare your execute method to work fine.
If you see the default-stack define in core, you will come to know that this interceptor is being called before params or workflow interceptor which indicates its purpose.
A typical use of this is to run some logic to load an object from the database so that when parameters are set they can be set on this object. For details read the doc of Prepare interceptor for details how it work closely with Preparable interface.In short Prepare interceptor will come in to act only when action implements Preparable.
Prepare-Interceptor
Prepare interface assures that if object in use is already on value stack then no need to query database it populates form properties using existing object on value stack.
I am (supposed to be) creating a simple menu display. My Menu class creates a List of MenuOption objects and these can be displayed and selected, etc. A programmer can add options to the List in the Menu class, optionList, using the addOption method.
What I want to be able to do is make it so the programmer can associate any arbitrary method from one of his or her other classes with a specific option.
For example, I want it so if the programmer typed something like:
menu.addOption("Print a roster", roster.print());
then the addOption method would do something like this:
optionList.add(new MenuOption("Print a roster", roster.print()));
and then, henceforth, the method roster.print() would be associated with the menu option text "Print a roster" so if a user chose "Print a roster," roster.print() would be called.
===============================================
By the way, I have started looking into the new Lambda Expressions from Java 8, but I'm not quite sure how they work or if they provide the necessary approach I would need to achieve my desired effect.
Any help is appreciated, thank you!
The second parameter to the MenuOption constructor would have to be a Runnable, and you would be able to supply
roster::print
as a value. This is called a Method Reference, and is just syntactic sugar for creating an object that implements a Functional Interface (in this case Runnable). It could also be written as
() -> roster.print()
On button click you would need to call the run() method.