public void fixupGroovyType() {

		StringBuilder s = new StringBuilder();
		if (reflectiveTargets == null) {
			reflectiveTargets = new Object[5];
			try {
				reflectiveTargets[INDEX_SWAPINIT_METHOD] = clazz.getDeclaredMethod("__$swapInit");
			}
			catch (Exception e) {
				s.append("cannot discover __$swapInit " + e.toString() + "  --  ");
			}
			try {
				reflectiveTargets[INDEX_CALLSITEARRAY_FIELD] = clazz.getDeclaredField("$callSiteArray");
			}
			catch (Exception e) {
				s.append("cannot discover $callSiteArray " + e.toString() + "  --  ");
			}
			try {
				reflectiveTargets[INDEX_METACLASS_FIELD] = clazz.getDeclaredField("$class$groovy$lang$MetaClass");
			}
			catch (Exception e) {
				s.append("cannot discover $class$groovy$lang$MetaClass " + e.toString() + "  --  ");
			}
			try {
				reflectiveTargets[3] = clazz.getDeclaredField("$staticClassInfo");
			}
			catch (Exception e) {
				s.append("cannot discover $staticClassInfo " + e.toString() + "  --  ");
			}
		}

		try {
			Method m = null;
			Field f = null;

			if (reflectiveTargets[INDEX_SWAPINIT_METHOD] != null) {
				m = (Method) reflectiveTargets[0];
				m.setAccessible(true);
				m.invoke(null);
			}
			if (reflectiveTargets[INDEX_CALLSITEARRAY_FIELD] != null) {
				f = (Field) reflectiveTargets[1];
				f.setAccessible(true);
				f.set(null, null);
			}

			if (reflectiveTargets[INDEX_METACLASS_FIELD] != null) {
				f = (Field) reflectiveTargets[2];
				f.setAccessible(true);
				f.set(null, null);
			}
			if (reflectiveTargets[3] != null) {
				f = (Field) reflectiveTargets[3];
				f.setAccessible(true);
				f.set(null, null);
			}
		}
		catch (Exception e) {
			s.append("cannot reset state" + e.toString() + "  --  ");
			//			new RuntimeException("Unable to fix up groovy state for " + this.dottedtypename, e);
		}

		try {
			Class<?> clazz = typeRegistry.getClass_GroovySystem();
			Field metaClassRegistryField = clazz.getDeclaredField("META_CLASS_REGISTRY");
			metaClassRegistryField.setAccessible(true);
			Object metaClassRegistry = metaClassRegistryField.get(null);
			Method metaClassRegistryMethod = metaClassRegistry.getClass().getDeclaredMethod("removeMetaClass",
					Class.class);
			metaClassRegistryMethod.setAccessible(true);
			metaClassRegistryMethod.invoke(metaClassRegistry, getClazz());
		}
		catch (Exception e) {
			s.append("Unable to remove meta class for groovy type " + this.dottedtypename + ": " + e.toString()
					+ "  --  ");
			//			new RuntimeException("Unable to remove meta class for groovy type " + this.dottedtypename, e)
			//					.printStackTrace(System.err);
		}

		// Implements: ClassInfo.getClassInfo(Class).cachedClassRef.clear()
		try {
			Method getClassInfoMethod = typeRegistry.getMethod_ClassInfo_getClassInfo();
			Object classInfoObject = getClassInfoMethod.invoke(null, this.clazz);
			Field cachedClassRefField = typeRegistry.getField_ClassInfo_cachedClassRef();
			cachedClassRefField.setAccessible(true);
			Object cachedClassRefObject = cachedClassRefField.get(classInfoObject);
			Class<?> lazyReferenceClass = cachedClassRefObject.getClass();// Class.forName("org.codehaus.groovy.util.LazyReference");
			// java.lang.NoSuchMethodException: org.codehaus.groovy.reflection.ClassInfo$LazyCachedClassRef.clear()
			Method clearMethod = lazyReferenceClass.getMethod("clear");//DeclaredMethod("clear");
			clearMethod.invoke(cachedClassRefObject);
		}
		catch (Exception e) {
			s.append("1 Unable to clear ClassInfo CachedClass data for groovy type " + this.dottedtypename + ": "
					+ e.toString()
					+ "  --  ");
			//			new RuntimeException("Unable to clear ClassInfo CachedClass data for groovy type " + this.dottedtypename, e)
			//					.printStackTrace(System.err);
		}

		try {

			//		    private static final ClassInfoSet globalClassSet = new ClassInfoSet(softBundle);
			Class<?> class_ClassInfo = typeRegistry.getClass_ClassInfo();
			Field field_globalClassSet = class_ClassInfo.getDeclaredField("globalClassSet");
			field_globalClassSet.setAccessible(true);
			Object/*ClassInfoSet*/ instance_classInfoSet = field_globalClassSet.get(null);
			Method method_ClassInfoSetRemove = instance_classInfoSet.getClass().getMethod("remove", Object.class);
			Object retval = method_ClassInfoSetRemove.invoke(instance_classInfoSet, this.clazz);

			//			Method getClassInfoMethod = typeRegistry.getMethod_ClassInfo_getClassInfo();
			//			Object classInfoObject = getClassInfoMethod.invoke(null, this.clazz);
			//			Field cachedClassRefField = typeRegistry.getField_ClassInfo_cachedClassRef();
			//			cachedClassRefField.setAccessible(true);
			//			Object cachedClassRefObject = cachedClassRefField.get(classInfoObject);
			//			Class<?> lazyReferenceClass = cachedClassRefObject.getClass();// Class.forName("org.codehaus.groovy.util.LazyReference");
			//			// java.lang.NoSuchMethodException: org.codehaus.groovy.reflection.ClassInfo$LazyCachedClassRef.clear()
			//			Method clearMethod = lazyReferenceClass.getMethod("clear");//DeclaredMethod("clear");
			//			clearMethod.invoke(cachedClassRefObject);
		}
		catch (Exception e) {
			s.append("2 Unable to clear ClassInfo CachedClass data for groovy type " + this.dottedtypename + ": "
					+ e.toString()
					+ "  --  ");
			//			new RuntimeException("Unable to clear ClassInfo CachedClass data for groovy type " + this.dottedtypename, e)
			//					.printStackTrace(System.err);
		}

		try {
			Set<WeakReference<Object>> deadInstances = null;
			Field f = getClazz().getDeclaredField("metaClass");
			for (WeakReference<Object> instance : liveInstances) {
				Object o = instance.get();
				if (o == null) {
					if (deadInstances == null) {
						deadInstances = new HashSet<WeakReference<Object>>();
					}
					deadInstances.add(instance);
				}
				else {
					f.setAccessible(true);
					f.set(o, null);
				}
			}
			if (deadInstances != null) {
				liveInstances.removeAll(deadInstances);
			}
		}
		catch (Exception e) {
			s.append("2 Unable to clear metaClass for groovy object instance (class=" + this.dottedtypename + ") "
					+ e.toString()
					+ "  --  ");
			//			new RuntimeException("Unable to clear metaClass for groovy object instance (class=" + this.dottedtypename + ")", e)
			//					.printStackTrace(System.err);
		}

	}
public void fixupGroovyType ( ) { StringBuilder s = new StringBuilder ( ) ; if ( reflectiveTargets == null ) { reflectiveTargets = new Object [ 5 ] ; try { reflectiveTargets [ INDEX_SWAPINIT_METHOD ] = clazz . getDeclaredMethod ( "__$swapInit" ) ; } catch ( Exception e ) { s . append ( "cannot discover __$swapInit " + e . toString ( ) + "  --  " ) ; } try { reflectiveTargets [ INDEX_CALLSITEARRAY_FIELD ] = clazz . getDeclaredField ( "$callSiteArray" ) ; } catch ( Exception e ) { s . append ( "cannot discover $callSiteArray " + e . toString ( ) + "  --  " ) ; } try { reflectiveTargets [ INDEX_METACLASS_FIELD ] = clazz . getDeclaredField ( "$class$groovy$lang$MetaClass" ) ; } catch ( Exception e ) { s . append ( "cannot discover $class$groovy$lang$MetaClass " + e . toString ( ) + "  --  " ) ; } try { reflectiveTargets [ 3 ] = clazz . getDeclaredField ( "$staticClassInfo" ) ; } catch ( Exception e ) { s . append ( "cannot discover $staticClassInfo " + e . toString ( ) + "  --  " ) ; } } try { Method m = null ; Field f = null ; if ( reflectiveTargets [ INDEX_SWAPINIT_METHOD ] != null ) { m = ( Method ) reflectiveTargets [ 0 ] ; m . setAccessible ( true ) ; m . invoke ( null ) ; } if ( reflectiveTargets [ INDEX_CALLSITEARRAY_FIELD ] != null ) { f = ( Field ) reflectiveTargets [ 1 ] ; f . setAccessible ( true ) ; f . set ( null , null ) ; } if ( reflectiveTargets [ INDEX_METACLASS_FIELD ] != null ) { f = ( Field ) reflectiveTargets [ 2 ] ; f . setAccessible ( true ) ; f . set ( null , null ) ; } if ( reflectiveTargets [ 3 ] != null ) { f = ( Field ) reflectiveTargets [ 3 ] ; f . setAccessible ( true ) ; f . set ( null , null ) ; } } catch ( Exception e ) { s . append ( "cannot reset state" + e . toString ( ) + "  --  " ) ; } try { Class < ? > clazz = typeRegistry . getClass_GroovySystem ( ) ; Field metaClassRegistryField = clazz . getDeclaredField ( "META_CLASS_REGISTRY" ) ; metaClassRegistryField . setAccessible ( true ) ; Object metaClassRegistry = metaClassRegistryField . get ( null ) ; Method metaClassRegistryMethod = metaClassRegistry . getClass ( ) . getDeclaredMethod ( "removeMetaClass" , Class . class ) ; metaClassRegistryMethod . setAccessible ( true ) ; metaClassRegistryMethod . invoke ( metaClassRegistry , getClazz ( ) ) ; } catch ( Exception e ) { s . append ( "Unable to remove meta class for groovy type " + this . dottedtypename + ": " + e . toString ( ) + "  --  " ) ; } try { Method getClassInfoMethod = typeRegistry . getMethod_ClassInfo_getClassInfo ( ) ; Object classInfoObject = getClassInfoMethod . invoke ( null , this . clazz ) ; Field cachedClassRefField = typeRegistry . getField_ClassInfo_cachedClassRef ( ) ; cachedClassRefField . setAccessible ( true ) ; Object cachedClassRefObject = cachedClassRefField . get ( classInfoObject ) ; Class < ? > lazyReferenceClass = cachedClassRefObject . getClass ( ) ; Method clearMethod = lazyReferenceClass . getMethod ( "clear" ) ; clearMethod . invoke ( cachedClassRefObject ) ; } catch ( Exception e ) { s . append ( "1 Unable to clear ClassInfo CachedClass data for groovy type " + this . dottedtypename + ": " + e . toString ( ) + "  --  " ) ; } try { Class < ? > class_ClassInfo = typeRegistry . getClass_ClassInfo ( ) ; Field field_globalClassSet = class_ClassInfo . getDeclaredField ( "globalClassSet" ) ; field_globalClassSet . setAccessible ( true ) ; Object instance_classInfoSet = field_globalClassSet . get ( null ) ; Method method_ClassInfoSetRemove = instance_classInfoSet . getClass ( ) . getMethod ( "remove" , Object . class ) ; Object retval = method_ClassInfoSetRemove . invoke ( instance_classInfoSet , this . clazz ) ; } catch ( Exception e ) { s . append ( "2 Unable to clear ClassInfo CachedClass data for groovy type " + this . dottedtypename + ": " + e . toString ( ) + "  --  " ) ; } try { Set < WeakReference < Object > > deadInstances = null ; Field f = getClazz ( ) . getDeclaredField ( "metaClass" ) ; for ( WeakReference < Object > instance : liveInstances ) { Object o = instance . get ( ) ; if ( o == null ) { if ( deadInstances == null ) { deadInstances = new HashSet < WeakReference < Object > > ( ) ; } deadInstances . add ( instance ) ; } else { f . setAccessible ( true ) ; f . set ( o , null ) ; } } if ( deadInstances != null ) { liveInstances . removeAll ( deadInstances ) ; } } catch ( Exception e ) { s . append ( "2 Unable to clear metaClass for groovy object instance (class=" + this . dottedtypename + ") " + e . toString ( ) + "  --  " ) ; } }
Groovy types need some extra fixup:
<ul>
<li>they contain a callsite array that caches destinations for calls. It needs clearing (it will be reinitialized
when required)
<li>not quite sure about the two: $staticClassInfo and GroovySystem removeMetaClass
<li>ClassScope.getClassInfo(Foo.class).cachedClassRef.clear()
</ul>