private void addAssociationToTarget(String name, Object target, Object obj) {
    if (obj == null) {
      return;
    }

    MetaClassRegistry reg = GroovySystem.getMetaClassRegistry();
    MetaClass mc = reg.getMetaClass(target.getClass());
    final String addMethodName = "addTo" + GrailsNameUtils.getClassNameRepresentation(name);
    mc.invokeMethod(target, addMethodName, obj);
  }
  public static MetaClassRegistryCleaner createAndRegister() {

    MetaClassRegistry metaClassRegistry = GroovySystem.getMetaClassRegistry();
    MetaClassRegistryChangeEventListener[] listeners =
        metaClassRegistry.getMetaClassRegistryChangeEventListeners();
    boolean registered = false;
    for (MetaClassRegistryChangeEventListener listener : listeners) {
      if (listener == INSTANCE) {
        registered = true;
        break;
      }
    }
    if (!registered) {
      GroovySystem.getMetaClassRegistry().addMetaClassRegistryChangeEventListener(INSTANCE);
    }
    return INSTANCE;
  }
  @SuppressWarnings("rawtypes")
  public static MetaClass getExpandoMetaClass(Class clazz) {
    MetaClassRegistry registry = GroovySystem.getMetaClassRegistry();
    Assert.isTrue(
        registry.getMetaClassCreationHandler() instanceof ExpandoMetaClassCreationHandle,
        "Grails requires an instance of [ExpandoMetaClassCreationHandle] to be set in Groovy's MetaClassRegistry! (current is : "
            + registry.getMetaClassCreationHandler()
            + ")");

    MetaClass mc = registry.getMetaClass(clazz);
    AdaptingMetaClass adapter = null;
    if (mc instanceof AdaptingMetaClass) {
      adapter = (AdaptingMetaClass) mc;
      mc = ((AdaptingMetaClass) mc).getAdaptee();
    }

    if (!(mc instanceof ExpandoMetaClass)) {
      // removes cached version
      registry.removeMetaClass(clazz);
      mc = registry.getMetaClass(clazz);
      if (adapter != null) {
        adapter.setAdaptee(mc);
      }
    }
    Assert.isTrue(
        mc instanceof ExpandoMetaClass,
        "BUG! Method must return an instance of [ExpandoMetaClass]!");
    return mc;
  }