public void addField(String name, Class type) { _classFab.addField(name, type); }
public void addField(String name, Class type) { _classFab.addField(name, type); }
/** * Adds a field, _serviceExtensionPoint, whose type matches this class, and a constructor which * sets the field. */ private static void addConstructor(ClassFab classFab, ServiceModel model) { Class modelClass = model.getClass(); classFab.addField("_serviceModel", modelClass); classFab.addConstructor(new Class[] { modelClass }, null, "{ super(); _serviceModel = $1; }"); }
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; }"); }
/** * Assumes that the factory will keep cranking out instances of the same class. * * @since 1.1 */ private Class constructBridgeProxyClass(Object service) { ProxyBuilder builder = new ProxyBuilder("BridgeProxy", getServicePoint()); ClassFab cf = builder.getClassFab(); Class serviceType = service.getClass(); cf.addField("_service", serviceType); cf.addConstructor(new Class[] { serviceType }, null, "{ super(); _service = $1; }"); builder.addServiceMethods("_service"); return cf.createClass(); }
public String addInjectedField(String fieldName, Class fieldType, Object value) { Defense.notNull(fieldName, "fieldName"); Defense.notNull(fieldType, "fieldType"); Defense.notNull(value, "value"); String existing = (String) _finalFields.get(value); // See if this object has been previously added. if (existing != null) return existing; // TODO: Should be ensure that the name is unique? // Make sure that the field has a unique name (at least, among anything // added // via addFinalField(). String uniqueName = _idAllocator.allocateId(fieldName); // ClassFab doesn't have an option for saying the field should be final, // just private. // Doesn't make a huge difference. _classFab.addField(uniqueName, fieldType); int parameterIndex = addConstructorParameter(fieldType, value); constructorBuilder().addln("{0} = ${1};", uniqueName, Integer.toString(parameterIndex)); // Remember the mapping from the value to the field name. _finalFields.put(value, uniqueName); return uniqueName; }
classFab.addField("_node", Node.class); classFab.addMethod(Modifier.PUBLIC, expressionSetter, "{ _node = $1; }");
public String addInjectedField(String fieldName, Class fieldType, Object value) { Defense.notNull(fieldName, "fieldName"); Defense.notNull(fieldType, "fieldType"); Defense.notNull(value, "value"); String existing = (String) _finalFields.get(value); // See if this object has been previously added. if (existing != null) return existing; // TODO: Should be ensure that the name is unique? // Make sure that the field has a unique name (at least, among anything // added // via addFinalField(). String uniqueName = _idAllocator.allocateId(fieldName); // ClassFab doesn't have an option for saying the field should be final, // just private. // Doesn't make a huge difference. _classFab.addField(uniqueName, fieldType); int parameterIndex = addConstructorParameter(fieldType, value); constructorBuilder().addln("{0} = ${1};", uniqueName, Integer.toString(parameterIndex)); // Remember the mapping from the value to the field name. _finalFields.put(value, uniqueName); return uniqueName; }
classFab.addField("_node", Node.class); classFab.addMethod(Modifier.PUBLIC, expressionSetter, "{ _node = $1; }");
/** * We construct a method that always goes through this service model's * {@link #getServiceImplementationForCurrentThread())} method. */ private static void addServiceAccessor(ClassFab classFab, String serviceModelMethodName, ServicePoint servicePoint) { Class serviceInterface = servicePoint.getServiceInterface(); classFab.addField(SERVICE_ACCESSOR_METHOD_NAME, serviceInterface); BodyBuilder builder = new BodyBuilder(); builder.begin(); builder.add("return ("); builder.add(serviceInterface.getName()); builder.add(") _serviceModel."); builder.add(serviceModelMethodName); builder.add("();"); builder.end(); classFab.addMethod(Modifier.PRIVATE | Modifier.FINAL, new MethodSignature(serviceInterface, SERVICE_ACCESSOR_METHOD_NAME, null, null), builder.toString()); } }
/** @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()); }