classFab.addField("_deferredProxy", deferredProxyClass);
classFab.addField("_service", serviceInterface);
classFab.addField("_serviceModel", getClass());
BodyBuilder body = new BodyBuilder();
// The constructor remembers the outer proxy and registers itself
// with the outer proxy.
body.begin();
body.addln("super();");
body.addln("_deferredProxy = $1;");
body.addln("_serviceModel = $2;");
body.addln("_deferredProxy._setInner(this);");
body.end();
classFab.addConstructor(new Class[]
{ deferredProxyClass, getClass() }, null, body.toString());
// Method _service() will look up the service implementation,
// then update the deferred proxy to go directly to the
// service implementation, bypassing itself!
body.clear();
body.begin();
body.add("if (_service == null)");
body.begin();
body.add("_service = (");
body.add(serviceInterface.getName());
body.addln(") _serviceModel.getActualServiceImplementation();");
body.add("_deferredProxy._setInner(_service);");
body.end();
body.add("return _service;");
body.end();
classFab.addMethod(
Modifier.PRIVATE | Modifier.FINAL | Modifier.SYNCHRONIZED,
new MethodSignature(serviceInterface, "_service", null, null),
body.toString());
builder.addServiceMethods("_service()");
// Build the implementation of interface SingletonInnerProxy
body.clear();
body.begin();
body.add("_service();");
body.end();
classFab.addMethod(Modifier.PUBLIC | Modifier.FINAL, new MethodSignature(void.class,
"_instantiateServiceImplementation", null, null), body.toString());
classFab.addInterface(SingletonInnerProxy.class);
return classFab.createClass();
}