use of org.springsource.loaded.ReloadableType in project spring-loaded by spring-projects.
the class MethodInvokerRewriterTests method rewriteInvokeInterface2.
/**
* Variant of the above where there is a parameter.
*/
@Test
public void rewriteInvokeInterface2() throws Exception {
TypeRegistry typeRegistry = getTypeRegistry("tgt.SimpleIClassTwo,tgt.SimpleITwo");
ReloadableType intface = typeRegistry.addType("tgt.SimpleITwo", loadBytesForClass("tgt.SimpleITwo"));
// ReloadableType impl =
typeRegistry.addType("tgt.SimpleIClassTwo", loadBytesForClass("tgt.SimpleIClassTwo"));
byte[] callerbytes = loadBytesForClass("tgt.StaticICallerTwo");
byte[] rewrittenBytes = MethodInvokerRewriter.rewrite(typeRegistry, callerbytes);
Class<?> callerClazz = loadit("tgt.StaticICallerTwo", rewrittenBytes);
// run the original
Result result = runUnguarded(callerClazz, "run");
assertEquals(123, result.returnValue);
intface.loadNewVersion("2", retrieveRename("tgt.SimpleITwo", "tgt.SimpleITwo002"));
result = runUnguarded(callerClazz, "run");
assertEquals(123, result.returnValue);
callerbytes = loadBytesForClass("tgt.StaticICallerTwo002");
callerbytes = ClassRenamer.rename("tgt.StaticICallerTwo002", callerbytes, "tgt.SimpleITwo002:tgt.SimpleITwo");
rewrittenBytes = MethodInvokerRewriter.rewrite(typeRegistry, callerbytes);
Class<?> callerClazz002 = loadit("tgt.StaticICallerTwo002", rewrittenBytes);
// ClassPrinter.print(rewrittenBytes);
// callee.loadNewVersion("2", retrieveRename("tgt.SimpleClass", "tgt.SimpleClass002"));
result = runUnguarded(callerClazz002, "run");
assertEquals("27", result.returnValue);
}
use of org.springsource.loaded.ReloadableType in project spring-loaded by spring-projects.
the class MethodInvokerRewriterTests method callingMethodIntroducedLaterReturningPrimitiveByte.
@Test
public void callingMethodIntroducedLaterReturningPrimitiveByte() 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, "callAppleRetByte", new Object[] { (byte) 54 });
// Load a version of Apple that does define that method
apple.loadNewVersion("002", retrieveRename("data.Apple", "data.Apple002"));
Result result = runUnguarded(callerClazz, "callAppleRetByte", new Object[] { (byte) 32 });
assertEquals((byte) 64, result.returnValue);
}
use of org.springsource.loaded.ReloadableType 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 org.springsource.loaded.ReloadableType in project spring-loaded by spring-projects.
the class ReloadableTypeTests method innerTypesLosingStaticModifier.
@Test
public void innerTypesLosingStaticModifier() throws Exception {
TypeRegistry typeRegistry = getTypeRegistry("inners.Outer$Inner");
byte[] sc = loadBytesForClass("inners.Outer$Inner");
ReloadableType rtype = typeRegistry.addType("inners.Outer$Inner", sc);
Class<?> simpleClass = rtype.getClazz();
Result r = null;
r = runUnguarded(simpleClass, "foo");
assertEquals("foo!", r.returnValue);
assertTrue(Modifier.isPublic((Integer) runUnguarded(simpleClass, "getModifiers").returnValue));
assertTrue(Modifier.isStatic((Integer) runUnguarded(simpleClass, "getModifiers").returnValue));
rtype.loadNewVersion("002", retrieveRename("inners.Outer$Inner", "inners.Outer2$Inner2"));
r = runUnguarded(simpleClass, "foo");
assertEquals("bar!", r.returnValue);
assertTrue(Modifier.isPublic((Integer) runUnguarded(simpleClass, "getModifiers").returnValue));
assertTrue(Modifier.isStatic((Integer) runUnguarded(simpleClass, "getModifiers").returnValue));
}
use of org.springsource.loaded.ReloadableType in project spring-loaded by spring-projects.
the class ScenarioTests method scenarioB_methodReplacement.
/**
* Scenario: A method is being discovered through reflection (getDeclaredMethods()). The method does not exist
* initially but is introduced later. Once found, an attempt is made to access annotations on this method (through
* getDeclaredAnnotations()) - these annotations do not exist initially but are then added.
*/
@Test
public void scenarioB_methodReplacement() throws Exception {
TypeRegistry typeRegistry = TypeRegistry.getTypeRegistryFor(binLoader);
// Configure it directly such that data.Apple is considered reloadable
configureForTesting(typeRegistry, "data..*");
ReloadableType scenarioB = typeRegistry.addType("data.ScenarioB", loadBytesForClass("data.ScenarioB"));
Class<?> scenarioClazz = scenarioB.getClazz();
Object o = scenarioClazz.newInstance();
String result = (String) runOnInstance(scenarioClazz, o, "methodAccessor").returnValue;
Assert.assertEquals("method not found", result);
result = (String) runOnInstance(scenarioClazz, o, "methodAccessor").returnValue;
Assert.assertEquals("method not found", result);
// load the version defining the method
scenarioB.loadNewVersion("002", retrieveRename("data.ScenarioB", "data.ScenarioB002"));
result = (String) runOnInstance(scenarioClazz, o, "methodAccessor").returnValue;
Assert.assertEquals("method found", result);
result = (String) runOnInstance(scenarioClazz, o, "methodAccessor").returnValue;
Assert.assertEquals("method found", result);
// now check for annotations
result = (String) runOnInstance(scenarioClazz, o, "annoAccessor").returnValue;
Assert.assertEquals("no annotations", result);
result = (String) runOnInstance(scenarioClazz, o, "annoAccessor").returnValue;
Assert.assertEquals("no annotations", result);
// load the version where the method is annotated
scenarioB.loadNewVersion("003", retrieveRename("data.ScenarioB", "data.ScenarioB003"));
result = (String) runOnInstance(scenarioClazz, o, "annoAccessor").returnValue;
Assert.assertEquals("found @data.Wiggle(value=default)", result);
result = (String) runOnInstance(scenarioClazz, o, "annoAccessor").returnValue;
Assert.assertEquals("found @data.Wiggle(value=default)", result);
}
Aggregations