Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Method invokers #460

Closed
arjantijms opened this issue Nov 23, 2020 · 5 comments
Closed

Method invokers #460

arjantijms opened this issue Nov 23, 2020 · 5 comments
Labels
spec-feature An issue requesting an addition specification
Milestone

Comments

@arjantijms
Copy link

(copied from CDI.Next doc, as there was no issue yet to track this)

Currently CDI does not have a concept of a bean method that can be executed from another bean with parameter binding.
Such a concept is used in other specs, such as JAX-RS, where resource methods are executed at runtime with the need to bind their parameters, and provide the support for interceptors and other CDI features.
The CDI spec should add API for such a feature, so other specs do not need to define a custom solution.

The following should be possible:

  • Obtain a reference to executable method with all metadata, including annotations on method and on fields
  • Execute an executable method on a bean instance
  • Obtain executable method metadata in interceptors (to analyze annotations using reflection-less approach)
  • Create a parameter binder, that would support a specific annotation on a parameter (basically a parameter qualifier)

Pseudocode:

Let’s consider the following bean:

@RequestScoped
@Path("/greet")
public class Resource {
   @GET
   String helloWorld(@HeaderParam("HEADER") String header) {
   }
}
@Qualifier
@Target(ElementType.PARAMETER)
public @interface HeaderParam {
   String value();
}

Now we could create a parameter binder:

@Singleton
@Binder(HeaderParam.class)
public class HeaderParamBinder implements ParamBinder {
   // HttpHeaders is a request scoped bean
   private final HttpHeaders headers;
   private final Converter converter;

   @Inject
   HeaderParamBinder(HttpHeaders headers, Converter converter) {
       this.headers = headers;
       this.converter = converter;
   }

   public <T> T bind(HeaderParam annotation, GenericType<T> type) {
       return converter.convert(headers.getRequestHeader(annotation.value()), type);
   }
}

And the execution of this method would be something like:

@MethodHandler(Path.class)
public class JaxRsExecutor implements ExecMethodHandler<Object, Object> {
   private final ParameterBinder binder;
   private final Container container;
  
   @Inject
   JaxRsExecutor(ArgumentBinder binder) {
       this.binder = binder;
   }

   @Override
   public void processMethod(ExecutableMethod<Object, Object> method) {
       // process registration with web server
   }
  
   private void executeMethod(ExecutableMethod<Object, Object> method) {
       // start request scope
       Object beanInstance = container.select(method.getDeclaringType()).get();
       method.invoke(beanInstance, binder.bind(method));
   }
}
@Ladicek
Copy link
Contributor

Ladicek commented Jul 29, 2022

When we get to this, I believe it would be great if this API supported providing an initial context data map for eventual interceptors (InvocationContext.getContextData()).

@Ladicek
Copy link
Contributor

Ladicek commented Dec 2, 2022

I just submitted an initial proposal for this: #639.

@Emily-Jiang Emily-Jiang added this to the CDI 4.1/5.0 milestone Jan 31, 2023
@Ladicek Ladicek removed the CDI.next label Jan 31, 2023
@manovotn
Copy link
Contributor

manovotn commented Oct 13, 2023

The initial proposal was merged and will be part of the first Alpha for 4.1.
However, there are still TODOs in the code that need addressing and until then, this issue should stay open.

TCK tracking issue - jakartaee/cdi-tck#491

@manovotn manovotn changed the title Executable methods Method invokers Oct 13, 2023
@starksm64 starksm64 added the spec-feature An issue requesting an addition specification label Feb 15, 2024
@Ladicek
Copy link
Contributor

Ladicek commented Feb 27, 2024

The specification and TCK for this are done.

We ended up removing transformers and wrappers from CDI 4.1, those should get specified in a future version, but the main functionality is there.

@Ladicek Ladicek closed this as completed Feb 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
spec-feature An issue requesting an addition specification
Projects
None yet
Development

No branches or pull requests

6 participants