Search in sources :

Example 11 with Result

use of org.springsource.loaded.test.infra.Result in project spring-loaded by spring-projects.

the class ClassReflectionTests method test_getDeclaredMethodCachedChangedReturnType.

/**
	 * Testing Class.getDeclaredMethod and Method.invoke
	 *
	 * A method with a parameter - existed at first - then it was changed: DIFFERENT RETURN TYPE
	 *
	 * If we held on to the original method object, we should NOT be able to invoke it anymore (different return type
	 * should be treated as different method!
	 */
@Test
public void test_getDeclaredMethodCachedChangedReturnType() throws Exception {
    Result r = getDeclaredMethod("changeReturn", String.class);
    assertMethod("public java.lang.String " + TARGET_CLASS_NAME + ".changeReturn(java.lang.String)", r);
    Method m = (Method) r.returnValue;
    r = invoke(m, "hoho");
    Assert.assertEquals("hohoho!", r.returnValue);
    reloadType("002");
    try {
        r = invoke(m, "hoho");
        Assert.fail("Method return type changed, shouldn't be able to call it anymore");
    } catch (ResultException e) {
        assertNoSuchMethodError(TARGET_CLASS_NAME + ".changeReturn(Ljava/lang/String;)Ljava/lang/String;", e);
    }
}
Also used : Method(java.lang.reflect.Method) ResultException(org.springsource.loaded.test.infra.ResultException) Result(org.springsource.loaded.test.infra.Result) Test(org.junit.Test)

Example 12 with Result

use of org.springsource.loaded.test.infra.Result in project spring-loaded by spring-projects.

the class ClassReflectionTests method test_getDeclaredMethodChanged.

/**
	 * Testing Class.getDeclaredMethod() - the method exists from the start, but its implementation changed (really this
	 * test is not different from test_getDeclaredMethodStays unless we do something special when method isn't
	 * changed...)
	 */
@Test
public void test_getDeclaredMethodChanged() throws Exception {
    Result r = getDeclaredMethod("methodChanged");
    assertMethod("public int " + TARGET_CLASS_NAME + ".methodChanged()", r);
    reloadType("002");
    r = getDeclaredMethod("methodChanged");
    assertMethod("public int " + TARGET_CLASS_NAME + ".methodChanged()", r);
}
Also used : Result(org.springsource.loaded.test.infra.Result) Test(org.junit.Test)

Example 13 with Result

use of org.springsource.loaded.test.infra.Result in project spring-loaded by spring-projects.

the class ClassReflectionTests method test_invokeStaticMethodOverriden.

/**
	 * Test invoking a static... does it truly use static dispatch?
	 */
@Test
public void test_invokeStaticMethodOverriden() throws Exception {
    String methodName = "staticMethodAdded";
    try {
        Result r = getDeclaredMethod(methodName);
        fail("Method shouldn't exist at first!\n" + r.toString());
    } catch (ResultException e) {
        assertNoSuchMethodException(TARGET_CLASS_NAME + "." + methodName + "()", e);
    }
    reloadType("002");
    // Setup subClass and an instance of the subclass
    String subClassName = TARGET_PACKAGE + ".SubClassTarget";
    ReloadableType subTarget = registry.addType(subClassName, loadBytesForClass(subClassName));
    Object subInstance = subTarget.getClazz().newInstance();
    //Double check, the subclass 'overrides' the static method
    assertMethod("public static int " + subClassName + "." + methodName + "()", getDeclaredMethod(subTarget.getClazz(), methodName));
    Result r = getDeclaredMethod(methodName);
    assertMethod("public static int " + TARGET_CLASS_NAME + "." + methodName + "()", r);
    Method m = (Method) r.returnValue;
    // Calling the static method seemingly on a 'subinstance' the instance should be ignored!
    r = invokeOn(subTarget, m);
    assertEquals(2, r.returnValue);
    reloadType("003");
    // Invoke again, using 'cached' copy of the method
    r = invokeOn(subInstance, m);
    assertEquals(3, r.returnValue);
    // Invoke again, using 'fresh' copy of the method
    m = (Method) getDeclaredMethod(methodName).returnValue;
    r = invokeOn(subInstance, m);
    assertEquals(3, r.returnValue);
}
Also used : ReloadableType(org.springsource.loaded.ReloadableType) ResultException(org.springsource.loaded.test.infra.ResultException) Method(java.lang.reflect.Method) Result(org.springsource.loaded.test.infra.Result) Test(org.junit.Test)

Example 14 with Result

use of org.springsource.loaded.test.infra.Result in project spring-loaded by spring-projects.

the class ClassReflectionTests method test_getDeclaredConstructors.

@Test
public void test_getDeclaredConstructors() throws Exception {
    // Let's call the constructor, nothing reloaded
    Result r = getDeclaredConstructors();
    assertNotNull(r.returnValue);
    Constructor<?>[] ctors = (Constructor<?>[]) r.returnValue;
    assertEquals(2, ctors.length);
    for (Constructor<?> ctor : ctors) {
        if (ctor.getParameterTypes().length == 1 && ctor.getParameterTypes()[0] == Integer.TYPE) {
            r = invokeConstructor(ctor, 4);
        } else if (ctor.getParameterTypes().length == 1 && ctor.getParameterTypes()[0] == Float.TYPE) {
            r = invokeConstructor(ctor, 4f);
        } else {
            r = invokeConstructor(ctor);
        }
        assertNotNull(r.returnValue);
    }
    reloadType("002");
    // then retrieving and using a new one
    for (Constructor<?> ctor : ctors) {
        if (ctor.getParameterTypes().length == 1 && ctor.getParameterTypes()[0] == Integer.TYPE) {
            r = invokeConstructor(ctor, 4);
        } else if (ctor.getParameterTypes().length == 1 && ctor.getParameterTypes()[0] == Float.TYPE) {
            r = invokeConstructor(ctor, 4f);
        } else {
            r = invokeConstructor(ctor);
        }
        assertNotNull(r.returnValue);
    }
    r = getDeclaredConstructors();
    assertNotNull(r.returnValue);
    ctors = (Constructor<?>[]) r.returnValue;
    assertEquals(3, ctors.length);
    for (Constructor<?> ctor : ctors) {
        if (ctor.getParameterTypes().length == 1 && ctor.getParameterTypes()[0] == Integer.TYPE) {
            r = invokeConstructor(ctor, 4);
        } else if (ctor.getParameterTypes().length == 1 && ctor.getParameterTypes()[0] == Float.TYPE) {
            r = invokeConstructor(ctor, 4f);
        } else {
            r = invokeConstructor(ctor);
        }
        assertNotNull(r.returnValue);
    }
}
Also used : Constructor(java.lang.reflect.Constructor) Result(org.springsource.loaded.test.infra.Result) Test(org.junit.Test)

Example 15 with Result

use of org.springsource.loaded.test.infra.Result in project spring-loaded by spring-projects.

the class ClassReflectionTests method test_getDeclaredField.

@Test
public void test_getDeclaredField() throws Exception {
    Result r = getDeclaredField("myField");
    assertField("public int " + TARGET_CLASS_NAME + ".myField", r);
    reloadType("002");
    r = getDeclaredField("myField");
    assertField("public int " + TARGET_CLASS_NAME + ".myField", r);
}
Also used : Result(org.springsource.loaded.test.infra.Result) Test(org.junit.Test)

Aggregations

Result (org.springsource.loaded.test.infra.Result)155 Test (org.junit.Test)139 ReloadableType (org.springsource.loaded.ReloadableType)106 TypeRegistry (org.springsource.loaded.TypeRegistry)97 ResultException (org.springsource.loaded.test.infra.ResultException)28 Method (java.lang.reflect.Method)27 InvocationTargetException (java.lang.reflect.InvocationTargetException)7 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 PrintStream (java.io.PrintStream)5 Constructor (java.lang.reflect.Constructor)4 Ignore (org.junit.Ignore)4 Field (java.lang.reflect.Field)2 ReflectiveInterceptor.jlClassGetField (org.springsource.loaded.ri.ReflectiveInterceptor.jlClassGetField)1