use of java.lang.reflect.InvocationTargetException in project spring-loaded by spring-projects.
the class MethodInvokerRewriterTests method superCallsRemovingMethods.
/**
* This is similar to the first case except the hierarchy is split such that a middle type exists where the methods
* initially exist but then they are removed in a reload. We should end up at the top level methods.
*/
@Test
public void superCallsRemovingMethods() throws Exception {
TypeRegistry tr = getTypeRegistry("invokespecial..*");
ReloadableType a = loadType(tr, "invokespecial.A");
ReloadableType b = loadType(tr, "invokespecial.B");
ReloadableType c = loadType(tr, "invokespecial.C");
Object object = c.getClazz().newInstance();
Method method = null;
String string = null;
// class B implements it right now
method = c.getClazz().getMethod("run1");
string = (String) method.invoke(object);
assertEquals("66", string);
method = c.getClazz().getMethod("run2");
string = (String) method.invoke(object);
assertEquals("66falseabc", string);
// Load new version of B where the methods are no longer there...
b.loadNewVersion("002", retrieveRename("invokespecial.B", "invokespecial.B002"));
// these calls should drop through to the super class A
method = c.getClazz().getMethod("run1");
string = (String) method.invoke(object);
assertEquals("65", string);
method = c.getClazz().getMethod("run2");
string = (String) method.invoke(object);
assertEquals("65falseabc", string);
// Load new version of A where they aren't there either - how do we fail?
a.loadNewVersion("002", retrieveRename("invokespecial.A", "invokespecial.A002"));
// these calls should drop through to the super class A
method = c.getClazz().getMethod("run1");
try {
string = (String) method.invoke(object);
fail();
} catch (InvocationTargetException ite) {
assertEquals("java.lang.NoSuchMethodError", ite.getCause().getClass().getName());
assertEquals("invokespecial.A.getInt()I", ite.getCause().getMessage());
}
}
use of java.lang.reflect.InvocationTargetException in project spring-loaded by spring-projects.
the class MethodInvokerRewriterTests method superCallsFillingEmptyHierarchy.
/**
* Starting all empty and filling things in on reloads - will the super calls be right?
*/
@Test
public void superCallsFillingEmptyHierarchy() throws Exception {
TypeRegistry tr = getTypeRegistry("invokespecial..*");
ReloadableType x = loadType(tr, "invokespecial.X");
ReloadableType y = loadType(tr, "invokespecial.Y");
ReloadableType z = loadType(tr, "invokespecial.Z");
Object object = z.getClazz().newInstance();
Method method = null;
String string = null;
// does nothing: X and Y are completely empty and all Z.run() does is return ""
method = z.getClazz().getMethod("run");
string = (String) method.invoke(object);
assertEquals("", string);
// load new version of X with a method in it: String foo() { return "X002.foo" }
x.loadNewVersion("002", retrieveRename("invokespecial.X", "invokespecial.X002"));
// no difference, no-one is calling foo()!
assertEquals("", method.invoke(object));
// load new version of Z, this will be calling super.foo() and be accessing the one in X002. Y002 is no different
z.loadNewVersion("002", retrieveRename("invokespecial.Z", "invokespecial.Z002", "invokespecial.X002:invokespecial.X", "invokespecial.Y002:invokespecial.Y"));
// run() now calls 'super.foo()' so should return "X002.foo"
string = (String) method.invoke(object);
assertEquals("X002.foo", string);
// Now reload Y, should make no difference. Y002 is no different
y.loadNewVersion("002", retrieveRename("invokespecial.Y", "invokespecial.Y002", "invokespecial.X002:invokespecial.X"));
assertEquals("X002.foo", method.invoke(object));
// I see it is Ys dispatcher that isn't dispatching to the X.foo() method
// Now reload Y, Y003 does provide an implementation
y.loadNewVersion("003", retrieveRename("invokespecial.Y", "invokespecial.Y003", "invokespecial.X002:invokespecial.X"));
assertEquals("Y003.foo", method.invoke(object));
// Now remove it from Y
y.loadNewVersion("004", retrieveRename("invokespecial.Y", "invokespecial.Y"));
string = (String) method.invoke(object);
assertEquals("X002.foo", string);
// Now remove it from X
x.loadNewVersion("004", retrieveRename("invokespecial.X", "invokespecial.X"));
try {
string = (String) method.invoke(object);
fail();
} catch (InvocationTargetException ite) {
assertEquals("java.lang.NoSuchMethodError", ite.getCause().getClass().getName());
assertEquals("invokespecial.Y.foo()Ljava/lang/String;", ite.getCause().getMessage());
}
}
use of java.lang.reflect.InvocationTargetException 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());
}
use of java.lang.reflect.InvocationTargetException in project spring-loaded by spring-projects.
the class MethodInvokerRewriterTests method rewriteCallArguments.
/**
* Target method here takes (string,integer,string,integer) and return a string
*/
@Test
public void rewriteCallArguments() 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);
try {
runUnguarded(callerClazz, "callApple1", new Object[] { "a", 1, "b", 2 });
Assert.fail("should not work, Apple doesn't have that method in it!");
} catch (InvocationTargetException ite) {
String cause = ite.getCause().toString();
if (!cause.startsWith("java.lang.NoSuchMethodError")) {
ite.printStackTrace();
Assert.fail("Should be a NoSuchMethodError, but got " + ite.getCause());
}
}
// Load a version of Apple that does define that method
apple.loadNewVersion("002", retrieveRename("data.Apple", "data.Apple002"));
Result result = runUnguarded(callerClazz, "callApple1", new Object[] { "a", 1, "b", 2 });
assertEquals("a 1 b 2", result.returnValue);
// Load a version of Apple that doesn't define it
apple.loadNewVersion("003", loadBytesForClass("data.Apple"));
try {
result = runUnguarded(callerClazz, "callApple1", new Object[] { "a", 1, "b", 2 });
Assert.fail("should not work, Apple doesn't have that method in it!");
} catch (InvocationTargetException ite) {
String cause = ite.getCause().toString();
if (!cause.startsWith("java.lang.NoSuchMethodError")) {
ite.printStackTrace();
Assert.fail("Should be a NoSuchMethodError, but got " + ite);
}
}
}
use of java.lang.reflect.InvocationTargetException in project spring-loaded by spring-projects.
the class MethodInvokerRewriterTests method rewriteInvokeStatic6.
/**
* If the static method is made non-static, here is what happens in the java case:
*
* <pre>
* Exception in thread "main" java.lang.IncompatibleClassChangeError: Expected static method B.foo()V
* at A.main(A.java:3)
* </pre>
*/
@Test
public void rewriteInvokeStatic6() throws Exception {
TypeRegistry typeRegistry = getTypeRegistry("tgt.SimpleClass");
ReloadableType callee = typeRegistry.addType("tgt.SimpleClass", loadBytesForClass("tgt.SimpleClass"));
byte[] callerbytes = loadBytesForClass("tgt.StaticCaller");
byte[] rewrittenBytes = MethodInvokerRewriter.rewrite(typeRegistry, callerbytes);
Class<?> callerClazz = loadit("tgt.StaticCaller", rewrittenBytes);
// run the original
Result result = runUnguarded(callerClazz, "run");
assertEquals(123, result.returnValue);
// new version of SimpleClass where target static method has been made non-static
callee.loadNewVersion("6", retrieveRename("tgt.SimpleClass", "tgt.SimpleClass006"));
try {
result = runUnguarded(callerClazz, "run");
Assert.fail();
} catch (InvocationTargetException ite) {
Throwable t = ite.getCause();
IncompatibleClassChangeError icce = (IncompatibleClassChangeError) t;
assertEquals("SpringLoaded: Target of static call is no longer static 'SimpleClass.toInt(Ljava/lang/String;)I'", icce.getMessage());
}
}
Aggregations