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

Support bean-initialisation through static factory methods. #183

Open
marcus-talbot42 opened this issue Dec 29, 2022 · 0 comments
Open

Support bean-initialisation through static factory methods. #183

marcus-talbot42 opened this issue Dec 29, 2022 · 0 comments

Comments

@marcus-talbot42
Copy link
Contributor

Complex classes are often provided with a static factory method, used to instantiate an object of the complex class. Furthermore, it would be beneficial for the use of Singletons to support such methods.

A fairly simple method of supporting static factory methods could be achieved through a new annotation BeanFactoryMethod.

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface BeanFactoryMethod {
    String[] value();
}

This annotation would accept a String-array, containing the names of the fields that would need to be provided to the method as arguments, in the correct order.

In order to process the BeanFactoryMethod, a new implementation of the BeanInitializer-interface could be provided, to properly invoke the factory method.

It is important to note that, as this process runs before a target-object has been initialized, there is no object of the target type available for use in reflection. As such, the factory method MUST be static, as the call to it will use Method#invoke(Object, Object...). As no object of the target type is available, we would be forced to pass null as the first parameter. This is only possible for static methods.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant