IssueRaiser

Feature Request: @PostConstructor

## Proposal Allow a method to be invoked after `@<X>ArgsConstructor` Many times, we want to avoid the tedium of writing constructors, but still do some initialization or computation on non-final / non-injected fields. Right now, the only option is to use Lazy getters, but that is cumbersome and not safe because the computation happens at a different lifetime. A `PostConstructor` would greatly help reduce the pain of boilerplate while still doing something useful (like validation, precomputation, caching), and also catching initialization errors early instead of using a Lazy getter. ## Possible Implementations ### `@PostConstructor` method annotation ``` @<X>ArgsConstructor public class SomeClass { private final int var1; private final int var2; private int computedVar3; @PostConstructor private void someInitMethod() { this.computedVar3 = var1 + var2; } } ``` ### `postConstructor` annotation attribute (like `onConstructor` attribute on XArgsConstructor annotations) ``` @<X>ArgsConstructor(postConstructor="someInitMethod") public class SomeClass { private final int var1; private final int var2; private int computedVar3; private void someInitMethod() { this.computedVar3 = var1 + var2; } } ``` ### Expected output ``` public class SomeClass { private final int var1; private final int var2; private int computedVar3; public SomeClass(int var1, int var2) { this.var1 = var1; this.var2 = var2; this.someLazyInitMethod(); }

Posted by rdsubhas on Oct 7, 2016, 4:08 PM

Tags

Related issues

Raise real-world problems, find people facing the same issue, and discuss solutions.4,500 issues0 comments

github (Other)

Feature Request: @PostConstructor

rdsubhasOct 7, 2016, 4:08 PM

## Proposal Allow a method to be invoked after `@<X>ArgsConstructor` Many times, we want to avoid the tedium of writing constructors, but still do some initialization or computation on non-final / non-injected fields. Right now, the only option is to use Lazy getters, but that is cumbersome and not safe because the computation happens at a different lifetime. A `PostConstructor` would greatly help reduce the pain of boilerplate while still doing something useful (like validation, precomputation, caching), and also catching initialization errors early instead of using a Lazy getter. ## Possible Implementations ### `@PostConstructor` method annotation ``` @<X>ArgsConstructor public class SomeClass { private final int var1; private final int var2; private int computedVar3; @PostConstructor private void someInitMethod() { this.computedVar3 = var1 + var2; } } ``` ### `postConstructor` annotation attribute (like `onConstructor` attribute on XArgsConstructor annotations) ``` @<X>ArgsConstructor(postConstructor="someInitMethod") public class SomeClass { private final int var1; private final int var2; private int computedVar3; private void someInitMethod() { this.computedVar3 = var1 + var2; } } ``` ### Expected output ``` public class SomeClass { private final int var1; private final int var2; private int computedVar3; public SomeClass(int var1, int var2) { this.var1 = var1; this.var2 = var2; this.someLazyInitMethod(); }

Comments (0)

No comments yet. Start the discussion.

Comment on this issue