Search in sources :

Example 51 with ReloadableType

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

the class MethodInvokerRewriterTests method invokevirtualNonCatchersErrorScenario.

@Test
public void invokevirtualNonCatchersErrorScenario() throws Exception {
    String top = "virtual.FourTop";
    String bot = "virtual.FourBot";
    TypeRegistry typeRegistry = getTypeRegistry(top + "," + bot);
    // The first top does not define foo()
    //		ReloadableType topR = 
    typeRegistry.addType(top, loadBytesForClass(top));
    ReloadableType botR = typeRegistry.addType(bot, loadBytesForClass(bot));
    result = runUnguarded(botR.getClazz(), "run");
    assertEquals(42, result.returnValue);
    // Dont load new Top, which means the new method that the subtype will call
    // will not exist
    // topR.loadNewVersion(retrieveRename(top, top + "2"));
    botR.loadNewVersion(retrieveRename(bot, bot + "2", top + "2:" + top));
    // introduced into FourTop
    try {
        result = runUnguarded(botR.getClazz(), "run");
        fail("Should have failed!");
    } catch (InvocationTargetException ite) {
        assertTrue(ite.getCause() instanceof NoSuchMethodError);
        assertEquals("FourBot.bar()I", ite.getCause().getMessage());
    }
}
Also used : ReloadableType(org.springsource.loaded.ReloadableType) TypeRegistry(org.springsource.loaded.TypeRegistry) InvocationTargetException(java.lang.reflect.InvocationTargetException) Test(org.junit.Test)

Example 52 with ReloadableType

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

the class MethodInvokerRewriterTests method callingMethodIntroducedLaterReturningReferenceArray.

@Test
public void callingMethodIntroducedLaterReturningReferenceArray() throws Exception {
    TypeRegistry typeRegistry = TypeRegistry.getTypeRegistryFor(binLoader);
    // Configure it directly such that data.Apple is considered reloadable
    configureForTesting(typeRegistry, "data.Apple");
    ReloadableType apple = typeRegistry.addType("data.Apple", loadBytesForClass("data.Apple"));
    byte[] callerbytes = loadBytesForClass("data.Orange002");
    callerbytes = ClassRenamer.rename("data.Orange", callerbytes, "data.Apple002:data.Apple");
    byte[] rewrittenBytes = MethodInvokerRewriter.rewrite(typeRegistry, callerbytes);
    Class<?> callerClazz = loadit("data.Orange", rewrittenBytes);
    runExpectNoSuchMethodException(callerClazz, "callAppleRetArrayString", new Object[] { new String[] { "abc" } });
    // Load a version of Apple that does define that method
    apple.loadNewVersion("002", retrieveRename("data.Apple", "data.Apple002"));
    Result result = runUnguarded(callerClazz, "callAppleRetArrayString", new Object[] { new String[] { "abc", "def" } });
    assertEquals("abc", ((String[]) result.returnValue)[0]);
}
Also used : ReloadableType(org.springsource.loaded.ReloadableType) TypeRegistry(org.springsource.loaded.TypeRegistry) Result(org.springsource.loaded.test.infra.Result) Test(org.junit.Test)

Example 53 with ReloadableType

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

the class MethodInvokerRewriterTests method rewriteInvokeInterface3.

/**
	 * Here an interface and the implementation are changed (to add a new method to both).
	 */
@Test
public void rewriteInvokeInterface3() throws Exception {
    TypeRegistry typeRegistry = getTypeRegistry("tgt.SimpleIClass,tgt.SimpleI");
    ReloadableType intface = typeRegistry.addType("tgt.SimpleI", loadBytesForClass("tgt.SimpleI"));
    ReloadableType impl = typeRegistry.addType("tgt.SimpleIClass", loadBytesForClass("tgt.SimpleIClass"));
    byte[] callerbytes = loadBytesForClass("tgt.StaticICaller");
    byte[] rewrittenBytes = MethodInvokerRewriter.rewrite(typeRegistry, callerbytes);
    Class<?> callerClazz = loadit("tgt.StaticICaller", rewrittenBytes);
    Result result = runUnguarded(callerClazz, "run");
    assertEquals(123, result.returnValue);
    // new interface on method and new implementation in the implementing class
    intface.loadNewVersion("2", retrieveRename("tgt.SimpleI", "tgt.SimpleI003"));
    impl.loadNewVersion("2", retrieveRename("tgt.SimpleIClass", "tgt.SimpleIClass003", "tgt.SimpleI003:tgt.SimpleI"));
    // run the original working thing post-reload - check it is still ok
    result = runUnguarded(callerClazz, "run");
    assertEquals(123, result.returnValue);
    callerbytes = loadBytesForClass("tgt.StaticICaller003");
    callerbytes = ClassRenamer.rename("tgt.StaticICaller003", callerbytes, "tgt.SimpleI003:tgt.SimpleI", "tgt.SimpleIClass003:tgt.SimpleIClass");
    rewrittenBytes = MethodInvokerRewriter.rewrite(typeRegistry, callerbytes);
    Class<?> callerClazz003 = loadit("tgt.StaticICaller003", rewrittenBytes);
    result = runUnguarded(callerClazz003, "run");
    assertEquals("2.01232768false", 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 54 with ReloadableType

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

the class MethodInvokerRewriterTests method rewriteInvokeInterface4_methodDeletion.

/**
	 * A method is removed from an interface.
	 */
@Test
public void rewriteInvokeInterface4_methodDeletion() throws Exception {
    TypeRegistry typeRegistry = getTypeRegistry("tgt.SimpleIClass,tgt.SimpleI");
    ReloadableType intface = typeRegistry.addType("tgt.SimpleI", loadBytesForClass("tgt.SimpleI"));
    typeRegistry.addType("tgt.SimpleIClass", loadBytesForClass("tgt.SimpleIClass"));
    byte[] callerbytes = loadBytesForClass("tgt.StaticICaller");
    byte[] rewrittenBytes = MethodInvokerRewriter.rewrite(typeRegistry, callerbytes);
    Class<?> callerClazz = loadit("tgt.StaticICaller", rewrittenBytes);
    Result result = runUnguarded(callerClazz, "run");
    assertEquals(123, result.returnValue);
    // new interface version has method removed
    intface.loadNewVersion("2", retrieveRename("tgt.SimpleI", "tgt.SimpleI004"));
    try {
        // run the original working thing post-reload - check it is still ok
        result = runUnguarded(callerClazz, "run");
        fail("Method no longer exists, should not have been callable");
    } catch (InvocationTargetException ite) {
        assertTrue(ite.getCause() instanceof NoSuchMethodError);
        assertEquals("SimpleI.toInt(Ljava/lang/String;)I", ite.getCause().getMessage());
    }
    // new interface version has method re-added
    intface.loadNewVersion("3", retrieveRename("tgt.SimpleI", "tgt.SimpleI"));
    result = runUnguarded(callerClazz, "run");
    assertEquals(123, result.returnValue);
}
Also used : ReloadableType(org.springsource.loaded.ReloadableType) TypeRegistry(org.springsource.loaded.TypeRegistry) InvocationTargetException(java.lang.reflect.InvocationTargetException) Result(org.springsource.loaded.test.infra.Result) Test(org.junit.Test)

Example 55 with ReloadableType

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

the class FieldReloadingTests method newFieldAdded.

// Field 'i' is added to Add on a reload and interacted with
@Test
public void newFieldAdded() throws Exception {
    TypeRegistry r = getTypeRegistry("fields.Add");
    ReloadableType add = loadType(r, "fields.Add");
    Class<?> addClazz = add.getClazz();
    Object addInstance = addClazz.newInstance();
    assertEquals(0, runOnInstance(addClazz, addInstance, "getValue").returnValue);
    //		ClassPrinter.print(add.bytesLoaded, true);
    add.loadNewVersion("2", retrieveRename("fields.Add", "fields.Add002"));
    assertEquals(0, runOnInstance(addClazz, addInstance, "getValue").returnValue);
    runOnInstance(addClazz, addInstance, "setValue", 45);
    assertEquals(45, runOnInstance(addClazz, addInstance, "getValue").returnValue);
    assertEquals(45, add.getField(addInstance, "i", false));
}
Also used : ReloadableType(org.springsource.loaded.ReloadableType) TypeRegistry(org.springsource.loaded.TypeRegistry) 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