Terminology
Peter Levart
peter.levart at marand.si
Thu Dec 17 07:03:04 PST 2009
On Thursday 17 December 2009 13:24:20 Alex Blewitt wrote:
> For example, Capture F above accesses an instance field of the current
> class. However, consider:
>
> public class F2 {
> public void f2a() {
> ... f2 = #int() { 42 };
> }
> }
>
> in this case, the method (f2a) is still an instance method, but the lambda
> f2 does not capture any instance-specific scope.
> So it could be promoted to
> be a constant or shared lambda; in fact, one could argue that the
> instantiation of that lambda (assuming it's translated to an inner class,
> then, it would be a static inner class) would be a static final. So it might
> transform to:
>
> public class F2 {
> private /* ACC_SYNTH etc */ static class f2 {
> protected int f2() { return 42 };
> }
> private static final f2 = new f2();
> public void f2a() {
> ... f2.f2()
> }
> }
I would even go further in this case and create the following equivalent code:
public class F2 {
private static class $GeneratedAnonymousName$ implements java.lang.function.FI, java.io.Serializable {
private static final java.lang.function.FI INSTANCE = new $GeneratedAnonymousName$();
public int invoke() { return 42; }
Object readResolve() { return INSTANCE; }
}
public void f2a() {
... f2 = F2.$GeneratedAnonymousName$.INSTANCE;
}
}
The holder of the closure singleton should be the generated closure class itself. This way deserialization can return the singleton instance without loading and initializing the defining class (F2 in this case).
Peter
More information about the lambda-dev
mailing list