Search in sources :

Example 91 with ReloadableType

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

the class ReflectiveReflectionTests method testJLRCGetAnnotation.

@Test
public void testJLRCGetAnnotation() throws Exception {
    String t = "iri.JLRCGetAnnotation";
    TypeRegistry r = getTypeRegistry(t);
    ReloadableType rtype = r.addType(t, loadBytesForClass(t));
    result = runUnguarded(rtype.getClazz(), "run");
    assertEquals("@reflection.AnnoT() null", result.returnValue);
    rtype.loadNewVersion(retrieveRenameRetarget(t));
    result = runUnguarded(rtype.getClazz(), "run");
    assertEquals("null @java.lang.Deprecated()", result.returnValue);
}
Also used : ReloadableType(org.springsource.loaded.ReloadableType) TypeRegistry(org.springsource.loaded.TypeRegistry) Test(org.junit.Test)

Example 92 with ReloadableType

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

the class MethodInvokerRewriterTests method invokevirtual.

@Test
public void invokevirtual() throws Exception {
    TypeRegistry typeRegistry = getTypeRegistry("virtual.CalleeOne");
    // The first target does not define toString()
    ReloadableType target = typeRegistry.addType("virtual.CalleeOne", loadBytesForClass("virtual.CalleeOne"));
    Class<?> callerClazz = loadit("virtual.CallerOne", loadBytesForClass("virtual.CallerOne"));
    // Run the initial version which does not define toString()
    Result result = runUnguarded(callerClazz, "run");
    // something like virtual.CalleeOne@4cee32
    assertTrue(((String) result.returnValue).startsWith("virtual.CalleeOne@"));
    // Load a version that does define toString()
    target.loadNewVersion("002", retrieveRename("virtual.CalleeOne", "virtual.CalleeOne002"));
    result = runUnguarded(callerClazz, "run");
    assertEquals("abcd", result.returnValue);
    // Load a version that does not define toString()
    target.loadNewVersion("003", retrieveRename("virtual.CalleeOne", "virtual.CalleeOne003"));
    result = runUnguarded(callerClazz, "run");
    // something like virtual.CalleeOne@4cee32
    assertTrue(((String) result.returnValue).startsWith("virtual.CalleeOne@"));
}
Also used : ReloadableType(org.springsource.loaded.ReloadableType) TypeRegistry(org.springsource.loaded.TypeRegistry) Result(org.springsource.loaded.test.infra.Result) Test(org.junit.Test)

Example 93 with ReloadableType

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

the class MethodInvokerRewriterTests method rewriteInvokeInterface1.

/**
	 * Here an interface is changed (reloaded) to include a new method, the implementing class already provides an
	 * implementation.
	 */
@Test
public void rewriteInvokeInterface1() 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);
    // run the original
    Result result = runUnguarded(callerClazz, "run");
    assertEquals(123, result.returnValue);
    intface.loadNewVersion("2", retrieveRename("tgt.SimpleI", "tgt.SimpleI002"));
    // run the original working thing post-reload - check it is still ok
    result = runUnguarded(callerClazz, "run");
    assertEquals(123, result.returnValue);
    callerbytes = loadBytesForClass("tgt.StaticICaller002");
    callerbytes = ClassRenamer.rename("tgt.StaticICaller002", callerbytes, "tgt.SimpleI002:tgt.SimpleI");
    rewrittenBytes = MethodInvokerRewriter.rewrite(typeRegistry, callerbytes);
    Class<?> callerClazz002 = loadit("tgt.StaticICaller002", rewrittenBytes);
    result = runUnguarded(callerClazz002, "run");
    assertEquals("42", 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 94 with ReloadableType

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

the class MethodInvokerRewriterTests method callingMethodIntroducedLaterReturningPrimitiveDouble.

@Test
public void callingMethodIntroducedLaterReturningPrimitiveDouble() 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, "callAppleRetDouble", new Object[] { 5.0d });
    // Load a version of Apple that does define that method
    apple.loadNewVersion("002", retrieveRename("data.Apple", "data.Apple002"));
    Result result = runUnguarded(callerClazz, "callAppleRetDouble", new Object[] { 3.0d });
    assertEquals(6.0d, 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 95 with ReloadableType

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

the class MethodInvokerRewriterTests method privateMethodCallsAndInvokeSpecial2.

/**
	 * Rewriting invokespecial when used for private method access. Similar to the previous test but now we are deleting
	 * some of the methods on reload.
	 */
@Test
public void privateMethodCallsAndInvokeSpecial2() throws Exception {
    registry = getTypeRegistry("invokespecial..*");
    ReloadableType t = loadType(registry, "invokespecial.ContainsPrivateCalls");
    Class<?> clazz = t.getClazz();
    Object o = clazz.newInstance();
    Method m = clazz.getDeclaredMethod("callMyPrivates");
    // Straightforward call, no reloading:
    assertEquals("12123abctruez", m.invoke(o));
    // Reload a version where a private method has been deleted
    t.loadNewVersion("002", retrieveRename("invokespecial.ContainsPrivateCalls", "invokespecial.ContainsPrivateCalls002"));
    // With the removal of the private method the code won't even compile without the call to it being removed,
    // so it just works...
    assertEquals("123abctruez", m.invoke(o));
}
Also used : ReloadableType(org.springsource.loaded.ReloadableType) Method(java.lang.reflect.Method) 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