Search in sources :

Example 71 with TypeRegistry

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

the class PluginTests method testSimplePlugin.

// Test a simple plugin that processes reload events
@Test
public void testSimplePlugin() throws Exception {
    binLoader = new TestClassloaderWithRewriting("meta1", true);
    String t = "simple.Basic";
    captureOn();
    TypeRegistry r = getTypeRegistry(t);
    String output = captureOff();
    assertContains("Instantiated ReloadEventProcessorPlugin1", output);
    ReloadableType rtype = r.addType(t, loadBytesForClass(t));
    result = runUnguarded(rtype.getClazz(), "run");
    assertEquals("hello", result.returnValue);
    captureOn();
    rtype.loadNewVersion("2", retrieveRename(t, t + "2"));
    output = captureOff();
    assertContains("Reloading: Loading new version of simple.Basic [2]", output);
    assertContains("ReloadEventProcessorPlugin1: reloadEvent(simple.Basic,simple.Basic,2)", output);
    result = runUnguarded(rtype.getClazz(), "run");
    assertEquals("goodbye", 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 72 with TypeRegistry

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

the class PluginTests method testPluginRerunStaticInitializerRequest2.

@Test
public void testPluginRerunStaticInitializerRequest2() throws Exception {
    binLoader = new TestClassloaderWithRewriting("metaNotExist", true);
    String t = "clinit.One";
    ReloadEventProcessorPlugin repp = new ReloadEventProcessorPlugin() {

        public void reloadEvent(String typename, Class<?> clazz, String encodedTimestamp) {
            System.out.println("Plugin: reloadEvent(" + typename + "," + clazz.getName() + "," + encodedTimestamp + ")");
        }

        public boolean shouldRerunStaticInitializer(String typename, Class<?> clazz, String encodedTimestamp) {
            System.out.println("Plugin: rerun request for " + typename);
            // if this were false, the result below would be 5!
            return true;
        }
    };
    try {
        Plugins.registerGlobalPlugin(repp);
        TypeRegistry r = getTypeRegistry(t);
        ReloadableType rtype = r.addType(t, loadBytesForClass(t));
        captureOn();
        rtype.loadNewVersion("2", retrieveRename(t, t + "2"));
        String output = captureOff();
        System.out.println(output);
        assertContains("Reloading: Loading new version of clinit.One [2]", output);
        assertUniqueContains("Plugin: reloadEvent(clinit.One,clinit.One,2)", output);
        assertContains("Reloading: Loading new version of clinit.One [2]", output);
        assertUniqueContains("Plugin: rerun request for clinit.One", output);
        result = runUnguarded(rtype.getClazz(), "run");
        assertEquals("7", result.returnValue);
    } finally {
        Plugins.unregisterGlobalPlugin(repp);
    }
}
Also used : ReloadableType(org.springsource.loaded.ReloadableType) ReloadEventProcessorPlugin(org.springsource.loaded.ReloadEventProcessorPlugin) TestClassloaderWithRewriting(org.springsource.loaded.test.infra.TestClassloaderWithRewriting) TypeRegistry(org.springsource.loaded.TypeRegistry) Test(org.junit.Test)

Example 73 with TypeRegistry

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

the class ProxyTests method xmultipleInterfaces.

/**
	 * Proxying with multiple interfaces, changed independently.
	 */
// To run these tests you need to have -javaagent specified
@Ignore
@Test
public void xmultipleInterfaces() throws Exception {
    // Set so that the Proxy generator can see the interface class
    Thread.currentThread().setContextClassLoader(binLoader);
    Class<?> clazz = Class.forName("proxy.three.TestA1", false, binLoader);
    runUnguarded(clazz, "createProxy");
    Class<?> clazzForInterface = Class.forName("proxy.three.TestIntfaceA1", false, binLoader);
    Class<?> clazzForInterfaceB1 = Class.forName("proxy.three.TestIntfaceB1", false, binLoader);
    // Call a method through the proxy
    assertContains("TestInvocationHandler1.invoke() for ma", runUnguarded(clazz, "runMA").stdout);
    TypeRegistry tr = TypeRegistry.getTypeRegistryFor(binLoader);
    assertNotNull(tr);
    ReloadableType rt = tr.getReloadableType(clazzForInterface);
    assertNotNull(rt);
    ReloadableType rt2 = tr.getReloadableType(clazzForInterfaceB1);
    assertNotNull(rt2);
    // new version adds a method called na
    byte[] newVersionOfTestInterfaceA1 = retrieveRename("proxy.three.TestIntfaceA1", "proxy.three.TestIntfaceA2");
    rt.loadNewVersion(newVersionOfTestInterfaceA1);
    // running m() should still work
    assertContains("TestInvocationHandler1.invoke() for ma", runUnguarded(clazz, "runMA").stdout);
    // Now load new version of proxy.TestA1 that will enable us to call n on the new interface
    byte[] newVersionOfTestA2 = retrieveRename("proxy.three.TestA1", "proxy.three.TestA2", "proxy.three.TestIntfaceA2:proxy.three.TestIntfaceA1", "proxy.three.TestIntfaceB2:proxy.three.TestIntfaceB1");
    tr.getReloadableType(clazz).loadNewVersion(newVersionOfTestA2);
    // running ma() should still work
    assertContains("TestInvocationHandler1.invoke() for ma", runUnguarded(clazz, "runMA").stdout);
    // running na() should now work! (if the proxy was auto regen/reloaded)
    assertContains("TestInvocationHandler1.invoke() for na", runUnguarded(clazz, "runNA").stdout);
    // should be OK - mb() was in from the start
    assertContains("TestInvocationHandler1.invoke() for mb", runUnguarded(clazz, "runMB").stdout);
    // TestIntfaceB1 hasn't been reloaded yet, nb() isnt on the interface (nor proxy)
    try {
        runUnguarded(clazz, "runNB");
        fail();
    } catch (InvocationTargetException re) {
        assertTrue(re.getCause() instanceof NoSuchMethodError);
        assertEquals("proxy.three.TestIntfaceB1.nb()V", re.getCause().getMessage());
    }
    // new version adds a method called nb
    byte[] newVersionOfTestInterfaceB1 = retrieveRename("proxy.three.TestIntfaceB1", "proxy.three.TestIntfaceB2");
    rt2.loadNewVersion("3", newVersionOfTestInterfaceB1);
    // running nb() should now work! (if the proxy was auto regen/reloaded)
    assertContains("TestInvocationHandler1.invoke() for nb", runUnguarded(clazz, "runNB").stdout);
    Set<ReloadableType> proxies = tr.getJDKProxiesFor("proxy/three/TestIntfaceA1");
    assertFalse(proxies.isEmpty());
    ReloadableType proxyRT = proxies.iterator().next();
    assertStartsWith("proxy.three.", proxyRT.getName());
}
Also used : ReloadableType(org.springsource.loaded.ReloadableType) TypeRegistry(org.springsource.loaded.TypeRegistry) InvocationTargetException(java.lang.reflect.InvocationTargetException) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 74 with TypeRegistry

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

the class ProxyTests method xnonPublicInterface.

/**
	 * For non public interfaces the proxies are generated in the same package as the interface.
	 */
// To run these tests you need to have -javaagent specified
@Ignore
@Test
public void xnonPublicInterface() throws Exception {
    // Set so that the Proxy generator can see the interface class
    Thread.currentThread().setContextClassLoader(binLoader);
    Class<?> clazz = Class.forName("proxy.two.TestA1", false, binLoader);
    Result r = runUnguarded(clazz, "createProxy");
    Class<?> clazzForInterface = Class.forName("proxy.two.TestIntfaceA1", false, binLoader);
    // Call a method through the proxy
    r = runUnguarded(clazz, "runM");
    assertContains("TestInvocationHandler1.invoke() for m", r.stdout);
    TypeRegistry tr = TypeRegistry.getTypeRegistryFor(binLoader);
    assertNotNull(tr);
    ReloadableType rt = tr.getReloadableType(clazzForInterface);
    assertNotNull(rt);
    // new version adds a method called n
    byte[] newVersionOfTestInterfaceA1 = retrieveRename("proxy.two.TestIntfaceA1", "proxy.two.TestIntfaceA2");
    rt.loadNewVersion(newVersionOfTestInterfaceA1);
    // running m() should still work
    r = runUnguarded(clazz, "runM");
    assertContains("TestInvocationHandler1.invoke() for m", r.stdout);
    // Now load new version of proxy.TestA1 that will enable us to call n on the new interface
    byte[] newVersionOfTestA2 = retrieveRename("proxy.two.TestA1", "proxy.two.TestA2", "proxy.two.TestIntfaceA2:proxy.two.TestIntfaceA1");
    tr.getReloadableType(clazz).loadNewVersion(newVersionOfTestA2);
    // running m() should still work
    r = runUnguarded(clazz, "runM");
    assertContains("TestInvocationHandler1.invoke() for m", r.stdout);
    // running n() should now work! (if the proxy was auto regen/reloaded)
    r = runUnguarded(clazz, "runN");
    assertContains("TestInvocationHandler1.invoke() for n", r.stdout);
    Set<ReloadableType> proxies = tr.getJDKProxiesFor("proxy/two/TestIntfaceA1");
    assertFalse(proxies.isEmpty());
    ReloadableType proxyRT = proxies.iterator().next();
    assertStartsWith("proxy.two.", proxyRT.getName());
}
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 75 with TypeRegistry

use of org.springsource.loaded.TypeRegistry 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)

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