Search in sources :

Example 41 with TypeRegistry

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

the class FieldReloadingTests method ctorReloadWithNewField.

@Test
public void ctorReloadWithNewField() throws Exception {
    String y = "ctors.JR";
    TypeRegistry tr = getTypeRegistry(y);
    ReloadableType rtype = tr.addType(y, loadBytesForClass(y));
    Class<?> clazz = rtype.getClazz();
    Object instance = runStaticUnguarded(clazz, "getInstance").returnValue;
    assertEquals("hello", runOnInstance(clazz, instance, "printMessage").returnValue);
    rtype.loadNewVersion("2", retrieveRename(y, y + "2"));
    assertEquals("goodbye", runOnInstance(clazz, instance, "printMessage").returnValue);
    Object instance2 = runStaticUnguarded(clazz, "getInstance").returnValue;
    Object ret = runOnInstance(clazz, instance2, "getFieldReflectively").returnValue;
    assertEquals(34, ret);
    ret = runOnInstance(clazz, instance2, "setFieldReflectively", 99).returnValue;
    ret = runOnInstance(clazz, instance2, "getFieldReflectively").returnValue;
    assertEquals(99, ret);
}
Also used : ReloadableType(org.springsource.loaded.ReloadableType) TypeRegistry(org.springsource.loaded.TypeRegistry) Test(org.junit.Test)

Example 42 with TypeRegistry

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

the class FieldReloadingTests method removingFieldsShadowingSuperFields.

/**
	 * Load a hierarchy of types. There is a field 'i' in both P and Q. R returns 'i'. Initially it returns Q.i but once
	 * Q has been reloaded it should be returning P.i
	 */
@Test
public void removingFieldsShadowingSuperFields() throws Exception {
    String p = "fields.P";
    String q = "fields.Q";
    String r = "fields.R";
    TypeRegistry tr = getTypeRegistry(p + "," + q + "," + r);
    //		ReloadableType ptype = 
    tr.addType(p, loadBytesForClass(p));
    ReloadableType qtype = tr.addType(q, loadBytesForClass(q));
    ReloadableType rtype = tr.addType(r, loadBytesForClass(r));
    Class<?> rClazz = rtype.getClazz();
    Object rInstance = rClazz.newInstance();
    assertEquals(2, runOnInstance(rClazz, rInstance, "getI").returnValue);
    qtype.loadNewVersion("2", retrieveRename(q, q + "2"));
    assertEquals(1, runOnInstance(rClazz, rInstance, "getI").returnValue);
}
Also used : ReloadableType(org.springsource.loaded.ReloadableType) TypeRegistry(org.springsource.loaded.TypeRegistry) Test(org.junit.Test)

Example 43 with TypeRegistry

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

the class FieldReloadingTests method testingCompatibilityOnFieldTypeChanges.

/**
	 * In this test several fields are setup and queried, then on reload their type is changed - ensure the results are
	 * as expected.
	 */
@Test
public void testingCompatibilityOnFieldTypeChanges() throws Exception {
    String a = "fields.TypeChanging";
    String b = "fields.Middle";
    TypeRegistry tr = getTypeRegistry(a + "," + b);
    ReloadableType type = tr.addType(a, loadBytesForClass(a));
    tr.addType(b, loadBytesForClass(b));
    Class<?> clazz = type.getClazz();
    Object instance = clazz.newInstance();
    // Should be fine
    assertEquals("SubInstance", toString(runOnInstance(clazz, instance, "getSuper").returnValue));
    assertEquals(1, runOnInstance(clazz, instance, "getI").returnValue);
    assertEquals(34L, runOnInstance(clazz, instance, "getLong").returnValue);
    assertEquals(true, runOnInstance(clazz, instance, "getBoolean").returnValue);
    assertEquals((short) 32, runOnInstance(clazz, instance, "getShort").returnValue);
    assertEquals('a', runOnInstance(clazz, instance, "getChar").returnValue);
    assertEquals(2.0f, runOnInstance(clazz, instance, "getFloat").returnValue);
    assertEquals(3.141d, runOnInstance(clazz, instance, "getDouble").returnValue);
    assertEquals((byte) 0xff, runOnInstance(clazz, instance, "getByte").returnValue);
    assertEquals("{1,2,3}", toString(runOnInstance(clazz, instance, "getWasArray").returnValue));
    assertEquals("abc", toString(runOnInstance(clazz, instance, "getWasNotArray").returnValue));
    // This changes all the field types
    type.loadNewVersion("2", retrieveRename(a, a + "2"));
    assertEquals("SubInstance", toString(runOnInstance(clazz, instance, "getSuper").returnValue));
    assertEquals(1, runOnInstance(clazz, instance, "getI").returnValue);
    assertEquals(34L, runOnInstance(clazz, instance, "getLong").returnValue);
    assertEquals(true, runOnInstance(clazz, instance, "getBoolean").returnValue);
    assertEquals((short) 32, runOnInstance(clazz, instance, "getShort").returnValue);
    assertEquals('a', runOnInstance(clazz, instance, "getChar").returnValue);
    assertEquals(2.0f, runOnInstance(clazz, instance, "getFloat").returnValue);
    assertEquals(3.141d, runOnInstance(clazz, instance, "getDouble").returnValue);
    assertEquals((byte) 0xff, runOnInstance(clazz, instance, "getByte").returnValue);
    assertEquals("0", toString(runOnInstance(clazz, instance, "getWasArray").returnValue));
    assertEquals(null, toString(runOnInstance(clazz, instance, "getWasNotArray").returnValue));
}
Also used : ReloadableType(org.springsource.loaded.ReloadableType) TypeRegistry(org.springsource.loaded.TypeRegistry) Test(org.junit.Test)

Example 44 with TypeRegistry

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

the class FieldReloadingTests method removingFieldsShadowingInterfaceFields1.

/**
	 * Here the field in Qc is shadowing (same name) a field in the interface. Once the new version of Qc is loaded that
	 * doesn't define the field, this exposes the field in the interface but it is static and as we are running a
	 * GETFIELD operation in Rc, it is illegal (IncompatibleClassChangeError)
	 */
@Test
public void removingFieldsShadowingInterfaceFields1() throws Exception {
    //		String i = "fields.Ic";
    String q = "fields.Qc";
    String r = "fields.Rc";
    TypeRegistry tr = getTypeRegistry(q + "," + r);
    ReloadableType qtype = tr.addType(q, loadBytesForClass(q));
    ReloadableType rtype = tr.addType(r, loadBytesForClass(r));
    Class<?> rClazz = rtype.getClazz();
    Object rInstance = rClazz.newInstance();
    assertEquals(2, runOnInstance(rClazz, rInstance, "getNumber").returnValue);
    qtype.loadNewVersion("2", retrieveRename(q, q + "2"));
    try {
        runOnInstance(rClazz, rInstance, "getNumber");
        fail();
    } catch (ResultException re) {
        Throwable e = (re).getCause();
        assertTrue(e.getCause() instanceof IncompatibleClassChangeError);
        assertEquals("Expected non-static field fields.Rc.number", e.getCause().getMessage());
    }
}
Also used : ReloadableType(org.springsource.loaded.ReloadableType) ResultException(org.springsource.loaded.test.infra.ResultException) TypeRegistry(org.springsource.loaded.TypeRegistry) Test(org.junit.Test)

Example 45 with TypeRegistry

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

the class Java8Tests method lambdaInvokeVirtual.

@Test
public void lambdaInvokeVirtual() throws Exception {
    String t = "basic.LambdaJ";
    TypeRegistry typeRegistry = getTypeRegistry("basic..*");
    // Since Foo needs promoting to public, have to ensure it is directly loaded:
    ReloadableType itype = typeRegistry.addType(t + "$Foo", loadBytesForClass(t + "$Foo"));
    byte[] sc = loadBytesForClass(t);
    ReloadableType rtype = typeRegistry.addType(t, sc);
    Class<?> simpleClass = rtype.getClazz();
    Result r = runUnguarded(simpleClass, "run");
    r = runUnguarded(simpleClass, "run");
    assertEquals("fooa", r.returnValue);
    itype.loadNewVersion("002", retrieveRename(t + "$Foo", t + "2$Foo"));
    rtype.loadNewVersion("002", retrieveRename(t, t + "2", t + "2$Foo:" + t + "$Foo"));
    r = runUnguarded(simpleClass, "run");
    assertEquals("fooab", 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)

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