Search in sources :

Example 41 with ReloadableType

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

the class CrossLoaderTests method reloadTargetInSuperloader.

/**
	 * In a class loaded by the subloader, calling a new method in a class loaded by the superloader. (ivicheck)
	 */
@Test
public void reloadTargetInSuperloader() throws Exception {
    String target = "superpkg.Target";
    String invoker = "subpkg.Invoker";
    ReloadableType targetR = subLoader.loadAsReloadableType(target);
    ReloadableType invokerR = subLoader.loadAsReloadableType(invoker);
    targetR.loadNewVersion("2", retrieveRename(target, target + "002"));
    invokerR.loadNewVersion("2", retrieveRename(invoker, invoker + "002", target + "002:" + target));
    // Check the registry looks right for target
    int targetId = NameRegistry.getIdFor(toSlash(target));
    assertEquals(0, targetId);
    TypeRegistry trtarget = TypeRegistry.getTypeRegistryFor(subLoader.getParent());
    assertEquals(target, trtarget.getReloadableType(targetId).getName());
    assertEquals(target, trtarget.getReloadableType(toSlash(target)).getName());
    int invokerId = NameRegistry.getIdFor(toSlash(invoker));
    TypeRegistry trinvokerR = TypeRegistry.getTypeRegistryFor(subLoader);
    assertEquals(1, invokerId);
    assertEquals(invoker, trinvokerR.getReloadableType(invokerId).getName());
    assertEquals(invoker, trinvokerR.getReloadableType(toSlash(invoker)).getName());
    // Now call the run() in the Invoker type, which calls 'Target.m()' where Target is in a different loader
    // and has been reloaded
    result = runUnguarded(invokerR.getClazz(), "run");
    assertEquals("Target002.m() running", result.stdout);
}
Also used : ReloadableType(org.springsource.loaded.ReloadableType) TypeRegistry(org.springsource.loaded.TypeRegistry) Test(org.junit.Test)

Example 42 with ReloadableType

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

the class CrossLoaderTests method loadTypesAcrossLoaders.

/**
	 * Check the basics - does the SubLoader/SuperLoader mechanism work.
	 */
@Test
public void loadTypesAcrossLoaders() throws Exception {
    ReloadableType rtypeA = subLoader.loadAsReloadableType("superpkg.Top");
    result = runUnguarded(rtypeA.getClazz(), "m");
    assertEquals("Top.m() running", result.stdout);
    ReloadableType rtypeB = subLoader.loadAsReloadableType("subpkg.Bottom");
    result = runUnguarded(rtypeB.getClazz(), "m");
    assertEquals("Bottom.m() running", result.stdout);
    assertNotSame(rtypeA.getTypeRegistry(), rtypeB.getTypeRegistry());
}
Also used : ReloadableType(org.springsource.loaded.ReloadableType) Test(org.junit.Test)

Example 43 with ReloadableType

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

the class DebuggingTests method rewrite.

@Test
public void rewrite() throws Exception {
    TypeRegistry typeRegistry = getTypeRegistry("data.HelloWorld");
    ReloadableType rtype = typeRegistry.addType("data.HelloWorld", loadBytesForClass("data.HelloWorld"));
    runUnguarded(rtype.getClazz(), "greet");
    // Just transform the existing version into a dispatcher/executor
    rtype.loadNewVersion("000", rtype.bytesInitial);
    Assert.assertEquals("Greet from HelloWorld", runUnguarded(rtype.getClazz(), "greet").stdout);
    // Load a real new version
    rtype.loadNewVersion("002", retrieveRename("data.HelloWorld", "data.HelloWorld002"));
    Assert.assertEquals("Greet from HelloWorld 2", runUnguarded(rtype.getClazz(), "greet").stdout);
//		ClassPrinter.print(rtype.getLatestExecutorBytes());
//		ClassPrinter.print(rtype.bytesInitial);
}
Also used : ReloadableType(org.springsource.loaded.ReloadableType) TypeRegistry(org.springsource.loaded.TypeRegistry) Test(org.junit.Test)

Example 44 with ReloadableType

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

the class DispatcherBuilderTests method staticMethod.

@Test
public void staticMethod() throws Exception {
    String tclass = "dispatcher.Staticmethod";
    TypeRegistry typeRegistry = getTypeRegistry(tclass);
    ReloadableType rtype = typeRegistry.addType(tclass, loadBytesForClass(tclass));
    // Simply reloads itself to trigger new version handling code paths in both infrastructure and generated code
    rtype.loadNewVersion("2", rtype.bytesInitial);
    //		ClassPrinter.print(rtype.interfaceBytes);
    //		ClassPrinter.print(rtype.getLatestExecutorBytes());
    byte[] callerbytes = loadBytesForClass("dispatcher.StaticmethodCaller");
    byte[] rewrittenBytes = MethodInvokerRewriter.rewrite(typeRegistry, callerbytes);
    Class<?> callerClazz = loadit("dispatcher.StaticmethodCaller", rewrittenBytes);
    result = runUnguarded(callerClazz, "run");
    Assert.assertTrue(result.stdout.indexOf("abc") != -1);
}
Also used : ReloadableType(org.springsource.loaded.ReloadableType) TypeRegistry(org.springsource.loaded.TypeRegistry) Test(org.junit.Test)

Example 45 with ReloadableType

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

the class EnumTests method testEnums1.

/**
	 * Test new values are visible on reload
	 */
@Test
public void testEnums1() throws Exception {
    String t = "enumtests.Colours";
    String runner = "enumtests.RunnerA";
    // Class<?> enumClazz = 
    binLoader.loadClass(t);
    Class<?> runnerClazz = binLoader.loadClass(runner);
    assertNotNull(runnerClazz);
    // Check we loaded it as reloadable
    ReloadableType rtype = TypeRegistry.getTypeRegistryFor(binLoader).getReloadableType(toSlash(t), false);
    assertNotNull(rtype);
    String output = runMethodAndCollectOutput(runnerClazz, "run1");
    assertContains("Red", output);
    assertContains("Green", output);
    assertContains("Blue", output);
    assertContains("[Red Green Blue]", output);
    assertContains("value count = 3", output);
    rtype.loadNewVersion(retrieveRename(t, t + "2"));
    output = runMethodAndCollectOutput(runnerClazz, "run1");
    assertContains("Red", output);
    assertContains("Green", output);
    assertContains("Blue", output);
    assertContains("[Red Green Blue Yellow]", output);
    assertContains("value count = 4", output);
}
Also used : ReloadableType(org.springsource.loaded.ReloadableType) Test(org.junit.Test)

Aggregations

ReloadableType (org.springsource.loaded.ReloadableType)375 Test (org.junit.Test)320 TypeRegistry (org.springsource.loaded.TypeRegistry)287 Result (org.springsource.loaded.test.infra.Result)106 Method (java.lang.reflect.Method)37 InvocationTargetException (java.lang.reflect.InvocationTargetException)26 TestClassloaderWithRewriting (org.springsource.loaded.test.infra.TestClassloaderWithRewriting)22 Ignore (org.junit.Ignore)17 CurrentLiveVersion (org.springsource.loaded.CurrentLiveVersion)10 AccessibleObject (java.lang.reflect.AccessibleObject)9 ResultException (org.springsource.loaded.test.infra.ResultException)9 MethodMember (org.springsource.loaded.MethodMember)8 Field (java.lang.reflect.Field)6 TypeDescriptor (org.springsource.loaded.TypeDescriptor)6 IOException (java.io.IOException)5 Annotation (java.lang.annotation.Annotation)4 ZipEntry (java.util.zip.ZipEntry)4 ZipFile (java.util.zip.ZipFile)4 LoadtimeInstrumentationPlugin (org.springsource.loaded.LoadtimeInstrumentationPlugin)4 File (java.io.File)3