Search in sources :

Example 66 with ReloadableType

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

the class Java8Tests method lambdaMethodReference.

// https://github.com/spring-projects/spring-loaded/issues/87
@Test
public void lambdaMethodReference() throws Exception {
    String t = "basic.LambdaM";
    TypeRegistry typeRegistry = getTypeRegistry("basic..*");
    byte[] sc = loadBytesForClass(t);
    ReloadableType rtype = typeRegistry.addType(t, sc);
    Class<?> simpleClass = rtype.getClazz();
    Result r = runUnguarded(simpleClass, "run");
    r = runUnguarded(simpleClass, "run");
    assertEquals("{5=test3}", r.returnValue);
    //, t + "2$Foo:" + t + "$Foo"));
    rtype.loadNewVersion("2", retrieveRename(t, t + "2"));
    r = runUnguarded(simpleClass, "run");
    assertEquals("{10=test3}", r.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 67 with ReloadableType

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

the class Java8Tests method defaultMethods2.

// A class implements an interface. A default method is added to the interface on
// a reload and invoked from a reloaded version of the class.
@Ignore
@Test
public void defaultMethods2() throws Exception {
    String t = "basic.DefaultMethodsI2A";
    String t2 = "basic.DefaultMethodsC2A";
    TypeRegistry typeRegistry = getTypeRegistry("basic..*");
    byte[] ia = loadBytesForClass(t);
    ReloadableType rtypeI = typeRegistry.addType(t, ia);
    byte[] ca = loadBytesForClass(t2);
    ReloadableType rtypeC = typeRegistry.addType(t2, ca);
    Class<?> simpleClass = rtypeC.getClazz();
    Result r = runUnguarded(simpleClass, "run");
    r = runUnguarded(simpleClass, "run");
    assertEquals(42, r.returnValue);
    //		byte[] renamed = retrieveRename(t, t + "2");
    rtypeI.loadNewVersion("002", ia);
    //		ClassPrinter.print(rtypeI.getBytesLoaded());
    r = runUnguarded(simpleClass, "run");
    assertEquals(42, r.returnValue);
    byte[] renamed = retrieveRename(t, t + "2");
    rtypeI.loadNewVersion(renamed);
    renamed = retrieveRename(t2, t2 + "2", t + "2:" + t);
    rtypeC.loadNewVersion(renamed);
    r = runUnguarded(simpleClass, "run");
    assertEquals(42, r.returnValue);
    assertEquals("FOO", r.stdout);
}
Also used : ReloadableType(org.springsource.loaded.ReloadableType) TypeRegistry(org.springsource.loaded.TypeRegistry) Result(org.springsource.loaded.test.infra.Result) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 68 with ReloadableType

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

the class MethodInvokerRewriterTests method invokevirtual2.

/**
	 * Two classes in a hierarchy, both reloadable. Neither of them defines a toString(), what happens as we reload
	 * versions of them adding then removing toString().
	 */
@Test
public void invokevirtual2() throws Exception {
    String caller = "virtual.CallerTwo";
    String top = "virtual.CalleeTwoTop";
    String bottom = "virtual.CalleeTwoBottom";
    TypeRegistry typeRegistry = getTypeRegistry(top + "," + bottom);
    // The first target does not define toString()
    ReloadableType reloadableTop = typeRegistry.addType(top, loadBytesForClass(top));
    //		ReloadableType reloadableBottom = 
    typeRegistry.addType(bottom, loadBytesForClass(bottom));
    Class<?> callerClazz = loadit(caller, loadBytesForClass(caller));
    Result result = null;
    result = runUnguarded(callerClazz, "runTopToString");
    assertTrue(((String) result.returnValue).startsWith("virtual.CalleeTwoTop@"));
    result = runUnguarded(callerClazz, "runBottomToString");
    assertTrue(((String) result.returnValue).startsWith("virtual.CalleeTwoBottom@"));
    reloadableTop.loadNewVersion("002", retrieveRename(top, top + "002"));
    result = runUnguarded(callerClazz, "runTopToString");
    assertTrue(((String) result.returnValue).startsWith("topToString"));
    result = runUnguarded(callerClazz, "runBottomToString");
    // still no impl in bottom, so hits top
    assertTrue(((String) result.returnValue).startsWith("topToString"));
    // remove it again
    reloadableTop.loadNewVersion("003", retrieveRename(top, top + "003"));
    result = runUnguarded(callerClazz, "runBottomToString");
    assertTrue(((String) result.returnValue).startsWith("virtual.CalleeTwoBottom@"));
}
Also used : ReloadableType(org.springsource.loaded.ReloadableType) TypeRegistry(org.springsource.loaded.TypeRegistry) Result(org.springsource.loaded.test.infra.Result) Test(org.junit.Test)

Example 69 with ReloadableType

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

the class MethodInvokerRewriterTests method invokevirtualNonCatchers.

/**
	 * Testing what happens when reloading introduces a new method in the supertype that is called from the subtype.
	 */
@Test
public void invokevirtualNonCatchers() 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);
    topR.loadNewVersion(retrieveRename(top, top + "2"));
    botR.loadNewVersion(retrieveRename(bot, bot + "2", top + "2:" + top));
    // now 'run()' should be invoking super.bar() where bar() is a new method 
    // introduced into FourTop
    result = runUnguarded(botR.getClazz(), "run");
    assertEquals(77, result.returnValue);
}
Also used : ReloadableType(org.springsource.loaded.ReloadableType) TypeRegistry(org.springsource.loaded.TypeRegistry) Test(org.junit.Test)

Example 70 with ReloadableType

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

the class MethodInvokerRewriterTests method privateMethodCallsAndInvokeSpecial.

/**
	 * Rewriting invokespecial when used for private method access.
	 */
@Test
public void privateMethodCallsAndInvokeSpecial() throws Exception {
    registry = getTypeRegistry("invokespecial..*");
    ReloadableType t = loadType(registry, "invokespecial.ContainsPrivateCalls");
    //			ReloadableType rt = loadType(tr, "invokespecial.Able2");
    //			ReloadableType st = loadType(tr, "invokespecial.Simple2");
    Class<?> clazz = t.getClazz();
    Object o = clazz.newInstance();
    Method m = clazz.getDeclaredMethod("callMyPrivates");
    // Straightforward call, no reloading:
    assertEquals("12123abctruez", m.invoke(o));
    // Reload itself, to cause executor creation:
    reload(t, "001");
    // Now the executor is being used.  The INVOKESPECIALs in the code will be rewritten to invokestatic 
    // calls in the executor that is built
    assertEquals("12123abctruez", 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