Search in sources :

Example 11 with TypeRegistry

use of org.springsource.loaded.TypeRegistry in project spring-loaded by spring-projects.

the class GroovyTests method closure3.

@Ignore
/**
	 * Double nested closure - a method that is invoked on the owners owner.
	 */
@Test
public void closure3() throws Exception {
    String t = "simple.BasicWithClosureC";
    String c = "simple.BasicWithClosureC$_run_closure1";
    String c2 = "simple.BasicWithClosureC$_run_closure1_closure2";
    TypeRegistry r = getTypeRegistry(t + "," + c + "," + c2);
    ReloadableType ctype = r.addType(c, loadBytesForClass(c));
    ReloadableType c2type = r.addType(c2, loadBytesForClass(c2));
    ReloadableType rtype = r.addType(t, loadBytesForClass(t));
    result = runUnguarded(rtype.getClazz(), "run");
    assertEquals("foo() running\nin closure", result.stdout);
    rtype.loadNewVersion("2", retrieveRename(t, t + "2", "simple.BasicWithClosureC2$_run_closure1:simple.BasicWithClosureC$_run_closure1", "simple.BasicWithClosureC2:simple.BasicWithClosureC"));
    c2type.loadNewVersion("2", retrieveRename(c2, "simple.BasicWithClosureC2$_run_closure1_closure2", "simple.BasicWithClosureC2$_run_closure1:simple.BasicWithClosureC$_run_closure1", "simple.BasicWithClosureC2:simple.BasicWithClosureC"));
    ctype.loadNewVersion("2", retrieveRename(c, "simple.BasicWithClosureC2$_run_closure1", "simple.BasicWithClosureC2$_run_closure1_closure2:simple.BasicWithClosureC$_run_closure1_closure2", "simple.BasicWithClosureC2:simple.BasicWithClosureC"));
    result = runUnguarded(rtype.getClazz(), "run");
    assertEquals("in closure\nfoo() running", result.stdout);
//
// // code in closure changes
// ctype.loadNewVersion("3", retrieveRename(c,
// "simple.BasicWithClosureB3$_closure1"));
// result = runUnguarded(rtype.getClazz(), "run");
// assertEquals("Executing:goodbye!", result.stdout);
}
Also used : ReloadableType(org.springsource.loaded.ReloadableType) TypeRegistry(org.springsource.loaded.TypeRegistry) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 12 with TypeRegistry

use of org.springsource.loaded.TypeRegistry in project spring-loaded by spring-projects.

the class GroovyTests method localVariables.

// TODO why doesn't this need swapInit? Is that only required for static
// field constants?
@Test
public void localVariables() throws Exception {
    binLoader = new TestClassloaderWithRewriting();
    String a = "simple.LFront";
    TypeRegistry r = getTypeRegistry(a);
    ReloadableType rtypea = r.addType(a, loadBytesForClass(a));
    result = runUnguarded(rtypea.getClazz(), "run");
    assertEquals("abc", result.returnValue);
    result = runUnguarded(rtypea.getClazz(), "run2");
    assertEquals(99, result.returnValue);
    rtypea.loadNewVersion("2", retrieveRename(a, a + "2"));
    result = runUnguarded(rtypea.getClazz(), "run");
    assertEquals("xxx", result.returnValue);
    result = runUnguarded(rtypea.getClazz(), "run2");
    assertEquals(88, result.returnValue);
}
Also used : ReloadableType(org.springsource.loaded.ReloadableType) TestClassloaderWithRewriting(org.springsource.loaded.test.infra.TestClassloaderWithRewriting) TypeRegistry(org.springsource.loaded.TypeRegistry) Test(org.junit.Test)

Example 13 with TypeRegistry

use of org.springsource.loaded.TypeRegistry in project spring-loaded by spring-projects.

the class FieldReloadingTests method inheritedNonReloadable2.

// Now a pair of types with one field initially.  New field added - original should continue to be accessed
// through the GETFIELD, the new one via the map
@Test
public void inheritedNonReloadable2() throws Exception {
    String top = "fields.X";
    TypeRegistry tr = getTypeRegistry(top);
    ReloadableType rTop = tr.addType(top, loadBytesForClass(top));
    Class<?> cTop = rTop.getClazz();
    Object iTop = cTop.newInstance();
    assertEquals(5, runOnInstance(cTop, iTop, "getI").returnValue);
    runOnInstance(cTop, iTop, "setI", 4);
    assertEquals(4, runOnInstance(cTop, iTop, "getI").returnValue);
    //		// Now reload the ReloadableTop class and introduce that field i which will shadow the one in the supertype
    rTop.loadNewVersion("2", retrieveRename(top, top + "002"));
    assertEquals(0, runOnInstance(cTop, iTop, "getJ").returnValue);
    runOnInstance(cTop, iTop, "setJ", 44);
    assertEquals(44, runOnInstance(cTop, iTop, "getJ").returnValue);
    assertEquals(4, runOnInstance(cTop, iTop, "getI").returnValue);
    ISMgr fa = getFieldAccessor(iTop);
    assertContains("j=44", fa.toString());
    assertDoesNotContain("i=", fa.toString());
}
Also used : ReloadableType(org.springsource.loaded.ReloadableType) ISMgr(org.springsource.loaded.ISMgr) TypeRegistry(org.springsource.loaded.TypeRegistry) Test(org.junit.Test)

Example 14 with TypeRegistry

use of org.springsource.loaded.TypeRegistry in project spring-loaded by spring-projects.

the class FieldReloadingTests method defaultFields.

// The problem with default visibility fields is that the executors cannot see them.  So we create accessors for
// these fields in the target types.  If two types in a hierarchy declare the same field, then these accessor methods will be in an 
// override relationship.  The overriding accessors will be subject to virtual dispatch - but the runtime type should be
// correct so that the right one is called.
@Test
public void defaultFields() throws Exception {
    TypeRegistry tr = getTypeRegistry("accessors..*");
    ReloadableType top = tr.addType("accessors.DefaultFields", loadBytesForClass("accessors.DefaultFields"));
    ReloadableType bottom = tr.addType("accessors.DefaultFieldsSub", loadBytesForClass("accessors.DefaultFieldsSub"));
    ClassPrinter.print(top.bytesLoaded);
    Object topInstance = top.getClazz().newInstance();
    Result result = runOnInstance(top.getClazz(), topInstance, "a");
    assertEquals(1, result.returnValue);
    Object botInstance = bottom.getClazz().newInstance();
    result = runOnInstance(bottom.getClazz(), botInstance, "a");
    assertEquals(1, result.returnValue);
    result = runOnInstance(bottom.getClazz(), botInstance, "b");
    assertEquals(2, result.returnValue);
}
Also used : ReloadableType(org.springsource.loaded.ReloadableType) TypeRegistry(org.springsource.loaded.TypeRegistry) Result(org.springsource.loaded.test.infra.Result) Test(org.junit.Test)

Example 15 with TypeRegistry

use of org.springsource.loaded.TypeRegistry in project spring-loaded by spring-projects.

the class FieldReloadingTests method accessingFieldsThroughReloadableType.

/**
	 * Both 'normal' reloadable field access and reflective field access will use the same set methods on
	 * ReloadableType. In the reflection case the right FieldAccessor must be discovered, in the normal case it is
	 * passed in. This test checks that using either route behaves - exercising the method directly and not through
	 * reflection.
	 */
@Test
public void accessingFieldsThroughReloadableType() throws Exception {
    String p = "fields.P";
    String q = "fields.Q";
    String r = "fields.R";
    TypeRegistry tr = getTypeRegistry(p + "," + q + "," + r);
    ReloadableType ptype = tr.addType(p, loadBytesForClass(p));
    ReloadableType qtype = tr.addType(q, loadBytesForClass(q));
    ReloadableType rtype = tr.addType(r, loadBytesForClass(r));
    Class<?> rClazz = rtype.getClazz();
    Object rInstance = rClazz.newInstance();
    // Before reloading, check both routes get to the same field:
    assertEquals(2, runOnInstance(rClazz, rInstance, "getI").returnValue);
    assertEquals(2, rtype.getField(rInstance, "i", false));
    // Now mess with it via one route and check result via the other:
    rtype.setField(rInstance, "i", false, 44);
    assertEquals(44, runOnInstance(rClazz, rInstance, "getI").returnValue);
    qtype.loadNewVersion("2", retrieveRename(q, q + "2"));
    // After reloading, check both routes get to the same field:
    assertEquals(1, runOnInstance(rClazz, rInstance, "getI").returnValue);
    assertEquals(1, ptype.getField(rInstance, "i", false));
    // Now mess with it via one route and check result via the other:
    rtype.setField(rInstance, "i", false, 357);
    assertEquals(357, runOnInstance(rClazz, rInstance, "getI").returnValue);
}
Also used : ReloadableType(org.springsource.loaded.ReloadableType) TypeRegistry(org.springsource.loaded.TypeRegistry) Test(org.junit.Test)

Aggregations

TypeRegistry (org.springsource.loaded.TypeRegistry)322 Test (org.junit.Test)305 ReloadableType (org.springsource.loaded.ReloadableType)287 Result (org.springsource.loaded.test.infra.Result)97 TestClassloaderWithRewriting (org.springsource.loaded.test.infra.TestClassloaderWithRewriting)24 InvocationTargetException (java.lang.reflect.InvocationTargetException)20 TypeDescriptor (org.springsource.loaded.TypeDescriptor)20 Ignore (org.junit.Ignore)17 Method (java.lang.reflect.Method)13 TypeDescriptorExtractor (org.springsource.loaded.TypeDescriptorExtractor)13 MethodMember (org.springsource.loaded.MethodMember)10 IOException (java.io.IOException)5 Properties (java.util.Properties)5 ResultException (org.springsource.loaded.test.infra.ResultException)5 IncrementalTypeDescriptor (org.springsource.loaded.IncrementalTypeDescriptor)4 LoadtimeInstrumentationPlugin (org.springsource.loaded.LoadtimeInstrumentationPlugin)4 File (java.io.File)3 FileInputStream (java.io.FileInputStream)3 ZipEntry (java.util.zip.ZipEntry)3 ZipFile (java.util.zip.ZipFile)3