use of org.springsource.loaded.test.infra.Result in project spring-loaded by spring-projects.
the class ClassReflectionTests method test_getDeclaredMethod_param_ChangedReturnType.
/**
* Testing Class.getDeclaredMethod and Method.invoke
*
* A method with a parameter - existed at first - then it was changed: DIFFERENT RETURN TYPE
*/
@Test
public void test_getDeclaredMethod_param_ChangedReturnType() throws Exception {
Result r = getDeclaredMethod("changeReturn", String.class);
assertMethod("public java.lang.String " + TARGET_CLASS_NAME + ".changeReturn(java.lang.String)", r);
r = invoke((Method) r.returnValue, "hoho");
Assert.assertEquals("hohoho!", r.returnValue);
reloadType("002");
Result m = getDeclaredMethod("changeReturn", String.class);
assertMethod("public int " + TARGET_CLASS_NAME + ".changeReturn(java.lang.String)", m);
r = invoke((Method) m.returnValue, "hoho");
Assert.assertEquals(4, r.returnValue);
}
use of org.springsource.loaded.test.infra.Result in project spring-loaded by spring-projects.
the class ClassReflectionTests method test_invokePublicMethodInDefaultClassAllowed.
/**
* Invoking a public method inside a default class by means of "invoke" call, from within a class in the same
* package as the target class should be allowed.
*/
@Test
public void test_invokePublicMethodInDefaultClassAllowed() throws Exception {
ReloadableType defaultClass = reloadableClass(TARGET_PACKAGE + "." + "DefaultClass");
Result r = getDeclaredMethodAndInvoke("callPublicMethodOnDefaultClass");
Assert.assertEquals(82, r.returnValue);
reloadType(defaultClass, "002");
r = getDeclaredMethodAndInvoke("callPublicMethodOnDefaultClass");
Assert.assertEquals(999, r.returnValue);
}
use of org.springsource.loaded.test.infra.Result in project spring-loaded by spring-projects.
the class ClassReflectionTests method test_getDeclaredConstructor.
@Test
public void test_getDeclaredConstructor() throws Exception {
// Let's call the constructor, nothing reloaded
Result r = getDeclaredConstructor();
assertNotNull(r.returnValue);
Constructor<?> ctor = (Constructor<?>) r.returnValue;
r = invokeConstructor(ctor);
assertNotNull(r.returnValue);
Constructor<?> ctorFloat = (Constructor<?>) getDeclaredConstructor(Float.TYPE).returnValue;
r = invokeConstructor(ctorFloat, 44f);
assertNotNull(r.returnValue);
reloadType("002");
// now call it again, first using the one we still have,
// then retrieving and using a new one
r = invokeConstructor(ctor);
assertNotNull(r.returnValue);
ctorFloat = (Constructor<?>) getDeclaredConstructor(Float.TYPE).returnValue;
r = invokeConstructor(ctorFloat, 22f);
assertNotNull(r.returnValue);
r = getDeclaredConstructor();
assertNotNull(r.returnValue);
ctor = (Constructor<?>) r.returnValue;
r = invokeConstructor(ctor);
assertNotNull(r.returnValue);
// Access a constructor that isn't there
try {
r = getDeclaredConstructor(String.class);
} catch (ResultException re) {
assertTrue(re.getCause() instanceof InvocationTargetException);
assertTrue(re.getCause().getCause().toString(), re.getCause().getCause() instanceof NoSuchMethodException);
}
// Access a new constructor (added in reload)
r = getDeclaredConstructor(Integer.TYPE);
assertNotNull(r.returnValue);
ctor = (Constructor<?>) r.returnValue;
r = invokeConstructor(ctor, 4);
assertNotNull(r.returnValue);
// Load new version with modified ctor
reloadType("003");
r = invokeConstructor(ctor, 4);
assertContains("modified!", r.stdout);
}
use of org.springsource.loaded.test.infra.Result in project spring-loaded by spring-projects.
the class MethodInvokerRewriterTests method callingMethodIntroducedLaterReturningPrimitiveLong.
@Test
public void callingMethodIntroducedLaterReturningPrimitiveLong() 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, "callAppleRetLong", new Object[] { 5L });
// Load a version of Apple that does define that method
apple.loadNewVersion("002", retrieveRename("data.Apple", "data.Apple002"));
Result result = runUnguarded(callerClazz, "callAppleRetLong", new Object[] { 5L });
assertEquals(10L, result.returnValue);
}
use of org.springsource.loaded.test.infra.Result in project spring-loaded by spring-projects.
the class MethodInvokerRewriterTests method callingMethodIntroducedLaterReturningPrimitiveFloat.
@Test
public void callingMethodIntroducedLaterReturningPrimitiveFloat() 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, "callAppleRetFloat", new Object[] { 3.0f });
// Load a version of Apple that does define that method
apple.loadNewVersion("002", retrieveRename("data.Apple", "data.Apple002"));
Result result = runUnguarded(callerClazz, "callAppleRetFloat", new Object[] { 4.0f });
assertEquals(8.0f, result.returnValue);
}
Aggregations