-
- Type Parameters:
T- type of the target instanceR- return type of the method
public interface Invoker<T,R>Allows indirectly invoking a method that belongs to a managed bean (the target method). To invoke the method, the caller must provide all the arguments that the target method accepts, as well as the instance on which the target method is to be invoked, if it is notstatic.Whenever a direct invocation of a method is a business method invocation, an indirect invocation of that method through an invoker is also a business method invocation.
Invoker implementations must be thread-safe. It is possible to use a single invoker instance to perform multiple independent invocations of the target method, possibly on different instances and with different arguments.
Obtaining an invoker
The CDI container allows building an invoker for non-private methods declared on a managed bean class or inherited from a supertype. Attempting to build an invoker for a private method or a constructor of a managed bean class leads to a deployment problem. Attempting to build an invoker for a method of a class that is not a managed bean class or that is an interceptor or decorator class leads to a deployment problem.Multiple managed beans may inherit a method from a common supertype. In that case, each bean conceptually has its own method and an invoker obtained for one bean may not be used to invoke the method on the other bean.
Using the
InvokerBuilderis the only way to obtain an invoker. AnInvokerBuildercan only be obtained in CDI portable extensions and build compatible extensions.Example
To illustrate how invokers work, let's take a look at an example. Say that the following bean exists and has a method that you want to invoke indirectly:@Dependent class MyService { String hello(String name) { return "Hello " + name + "!"; } }When you obtain anInvokerBuilderfor thehello()method, you can immediately build a direct invoker. In a portable extension, this results in an invoker:InvokerBuilder<Invoker<MyService, String>> builder = ...; Invoker<MyService, String> invoker = builder.build();
In a build compatible extension, this results in an opaque token that later materializes as an invoker:InvokerBuilder<InvokerInfo> builder = ...; InvokerInfo invoker = builder.build();
To call thehello()method through this invoker, callinvoker.invoke(myService, new Object[] {"world"}). The return value is"Hello world!".An implementation of the direct invoker above is equivalent to the following class:
class TheInvoker implements Invoker<MyService, String> { String invoke(MyService instance, Object[] arguments) { return instance.hello((String) arguments[0]); } }- Since:
- 4.1
- See Also:
invoke(Object, Object[])
-
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description Rinvoke(T instance, java.lang.Object[] arguments)Invokes the target method of this invoker on giveninstance, passing givenarguments.
-
-
-
Method Detail
-
invoke
R invoke(T instance, java.lang.Object[] arguments)
Invokes the target method of this invoker on giveninstance, passing givenarguments. If the target method isstatic, theinstanceis ignored; by convention, it should benull. If the target method returns normally, this method returns its return value, unless the target method is declaredvoid, in which case this method returnsnull. If the target method throws an exception, this method rethrows it directly.If some parameter of the target method declares a primitive type, the corresponding element of the
argumentsarray must be of the corresponding wrapper type. No type conversions are performed, so if the parameter is declaredint, the argument must be anIntegerand may not beShortorLong. If the argument isnull, the default value of the primitive type is used. Note that this does not apply to arrays of primitive types; if a parameter is declaredint[], the argument must beint[]and may not beInteger[].If the target method is not
staticandinstanceisnull, aNullPointerExceptionis thrown. If the target method is notstaticand theinstanceis not assignable to the class of the bean to which the method belongs, aClassCastExceptionis thrown.If the target method declares no parameter,
argumentsare ignored. If the target method declares any parameter andargumentsisnull,NullPointerExceptionis thrown. If theargumentsarray has fewer elements than the number of parameters of the target method,ArrayIndexOutOfBoundsExceptionis thrown. If theargumentsarray has more elements than the number of parameters of the target method, the excess elements are ignored. If some of theargumentsis not assignable to the declared type of the corresponding parameter of the target method,ClassCastExceptionis thrown. TODO the previous 2 paragraphs refer to "assignability", which needs to be defined somewhere! TODO when the `InvokerBuilder` applies transformations, some of the requirements above are no longer strictly necessary, should reflect that in this text somehow (it is already mentioned in `InvokerBuilder`, but that likely isn't enough)- Parameters:
instance- the instance on which the target method is to be invoked, may only benullif the method isstaticarguments- arguments to be supplied to the target method, may only benullif the method declares no parameter- Returns:
- return value of the target method, or
nullif the method is declaredvoid
-
-