private void createInfrastructure(InterceptorStack stack, ClassFab classFab) { Class topClass = ClassFabUtils.getInstanceClass(stack.peek(), stack.getServiceInterface()); classFab.addField("_log", Log.class); // This is very important: since we know the instance of the top object (the next // object in the pipeline for this service), we can build the instance variable // and constructor to use the exact class rather than the service interface. // That's more efficient at runtime, lowering the cost of using interceptors. // One of the reasons I prefer Javassist over JDK Proxies. classFab.addField("_delegate", topClass); classFab.addConstructor( new Class[] { Log.class, topClass }, null, "{ _log = $1; _delegate = $2; }"); }
/** @since 1.1 */ private static void addDelegateAccessor(ClassFab classFab, ServicePoint servicePoint, Object delegate) { classFab.addField("_shutdown", boolean.class); Class delegateClass = ClassFabUtils.getInstanceClass(delegate, servicePoint .getServiceInterface()); classFab.addField("_delegate", delegateClass); classFab.addConstructor(new Class[] { delegateClass }, null, "{ super(); _delegate = $1; }"); classFab.addInterface(RegistryShutdownListener.class); if( RegistryShutdownListener.class.isAssignableFrom( delegateClass ) ) { classFab.addMethod(Modifier.PUBLIC | Modifier.FINAL, new MethodSignature(void.class, "registryDidShutdown", null, null), "{ _delegate.registryDidShutdown(); _delegate = null; _shutdown = true; }"); } else { classFab.addMethod(Modifier.PUBLIC | Modifier.FINAL, new MethodSignature(void.class, "registryDidShutdown", null, null), "{ _delegate = null; _shutdown = true; }"); } BodyBuilder builder = new BodyBuilder(); builder.begin(); builder.addln("if (_shutdown)"); builder.addln(" throw org.apache.hivemind.HiveMind#createRegistryShutdownException();"); builder.add("return _delegate;"); builder.end(); classFab.addMethod(Modifier.FINAL | Modifier.PRIVATE, new MethodSignature(delegateClass, DELEGATE_ACCESSOR_METHOD_NAME, null, null), builder.toString()); }