Details
-
New Feature
-
Status: Closed
-
Minor
-
Resolution: Fixed
-
None
-
None
Description
The whole idea is similar to existing great @Lazy annotation, but it differs in concept: instead of being applied to fields it is applied to methods, thus providing a wider field of use. When applied to getters it serves as an alternative to @Lazy, but applied to other methods it provides what @Lazy can't. Thus it eliminates the need for heavy refactoring in certain situations, by simply letting the user add the annotation to the method.
Here is a suggestion of how it could work:
@Cached
T createX() {
new T(1, 2, 3)
}
gets transformed into:
private T $createX$result T $createX() { new T(1, 2, 3) } T createX() { T $result_local = $createX$result if ($result_local != null) return $result_local else { synchronized (this) { if ($createX$result == null) { $createX$result = $createX() } return $createX$result } } }
This whole thing could be extended to cache different results of a method depending on its arguments, but it's a topic for a discussion.