@Test
public void basicInstance() throws Exception {
binLoader = new TestClassloaderWithRewriting();
String t = "simple.Basic";
TypeRegistry r = getTypeRegistry(t);
ReloadableType rtype = r.addType(t, loadBytesForClass(t));
Object instance = null;
instance = rtype.getClazz().newInstance();
// First method call to 'run' should return "hello"
result = runOnInstance(rtype.getClazz(), instance, "run");
assertEquals("hello", result.returnValue);
// Version 3 makes run() call another local method to get the string
// "abc"
rtype.loadNewVersion("3", retrieveRename(t, t + "3"));
// Field f = rtype.getClazz().getDeclaredField("metaClass");
// f.setAccessible(true);
// Object mc = f.get(instance);
// System.out.println("Metaclass is currently " + mc);
//
// f.set(instance, null);
//
// f =
// rtype.getClazz().getDeclaredField("$class$groovy$lang$MetaClass");
// f.setAccessible(true);
// f.set(instance, null);
//
// Method m = rtype.getClazz().getDeclaredMethod("getMetaClass");
// m.setAccessible(true);
// m.invoke(instance);
// f.setAccessible(true);
// mc = f.get(instance);
// 9: invokevirtual #23; //Method
// $getStaticMetaClass:()Lgroovy/lang/MetaClass;
// 12: dup
// 13: invokestatic #27; //Method
// $get$$class$groovy$lang$MetaClass:()Ljava/lang/Class;
// 16: invokestatic #33; //Method
// org/codehaus/groovy/runtime/ScriptBytecodeAdapter.castToType:(Ljava/lang/Object;Ljav
// a/lang/Class;)Ljava/lang/Object;
// 19: checkcast #35; //class groovy/lang/MetaClass
// 22: aload_0
// 23: swap
// 24: putfield #37; //Field metaClass:Lgroovy/lang/MetaClass;
// 27: pop
// 28: return
// Method m = rtype.getClazz().getDeclaredMethod("$getStaticMetaClass");
// m.setAccessible(true);
// Object o = m.invoke(instance);
// m =
// rtype.getClazz().getDeclaredMethod("$get$$class$groovy$lang$MetaClass");
// m.setAccessible(true);
// Object p = m.invoke(null);
// m =
// rtype.getClazz().getClassLoader().loadClass("org.codehaus.groovy.runtime.ScriptBytecodeAdapter")
// .getDeclaredMethod("castToType", Object.class, Class.class);
// m.setAccessible(true);
//
// Object mc = m.invoke(null, o, p);
// Field f = rtype.getClazz().getDeclaredField("metaClass");
// f.setAccessible(true);
// f.set(instance, null);
// instance = rtype.getClazz().newInstance();
// System.out.println("Metaclass is currently " + mc);
// Let's reinitialize the instance meta class by duplicating
result = runOnInstance(rtype.getClazz(), instance, "run");
// result = runUnguarded(rtype.getClazz(), "run");
assertEquals("abc", result.returnValue);
}