use of org.springsource.loaded.TypeRegistry in project spring-loaded by spring-projects.
the class MethodInvokerRewriterTests method callingMethodIntroducedLaterReturningReferenceArray.
@Test
public void callingMethodIntroducedLaterReturningReferenceArray() 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, "callAppleRetArrayString", new Object[] { new String[] { "abc" } });
// Load a version of Apple that does define that method
apple.loadNewVersion("002", retrieveRename("data.Apple", "data.Apple002"));
Result result = runUnguarded(callerClazz, "callAppleRetArrayString", new Object[] { new String[] { "abc", "def" } });
assertEquals("abc", ((String[]) result.returnValue)[0]);
}
use of org.springsource.loaded.TypeRegistry in project spring-loaded by spring-projects.
the class MethodInvokerRewriterTests method rewriteInvokeInterface3.
/**
* Here an interface and the implementation are changed (to add a new method to both).
*/
@Test
public void rewriteInvokeInterface3() throws Exception {
TypeRegistry typeRegistry = getTypeRegistry("tgt.SimpleIClass,tgt.SimpleI");
ReloadableType intface = typeRegistry.addType("tgt.SimpleI", loadBytesForClass("tgt.SimpleI"));
ReloadableType impl = typeRegistry.addType("tgt.SimpleIClass", loadBytesForClass("tgt.SimpleIClass"));
byte[] callerbytes = loadBytesForClass("tgt.StaticICaller");
byte[] rewrittenBytes = MethodInvokerRewriter.rewrite(typeRegistry, callerbytes);
Class<?> callerClazz = loadit("tgt.StaticICaller", rewrittenBytes);
Result result = runUnguarded(callerClazz, "run");
assertEquals(123, result.returnValue);
// new interface on method and new implementation in the implementing class
intface.loadNewVersion("2", retrieveRename("tgt.SimpleI", "tgt.SimpleI003"));
impl.loadNewVersion("2", retrieveRename("tgt.SimpleIClass", "tgt.SimpleIClass003", "tgt.SimpleI003:tgt.SimpleI"));
// run the original working thing post-reload - check it is still ok
result = runUnguarded(callerClazz, "run");
assertEquals(123, result.returnValue);
callerbytes = loadBytesForClass("tgt.StaticICaller003");
callerbytes = ClassRenamer.rename("tgt.StaticICaller003", callerbytes, "tgt.SimpleI003:tgt.SimpleI", "tgt.SimpleIClass003:tgt.SimpleIClass");
rewrittenBytes = MethodInvokerRewriter.rewrite(typeRegistry, callerbytes);
Class<?> callerClazz003 = loadit("tgt.StaticICaller003", rewrittenBytes);
result = runUnguarded(callerClazz003, "run");
assertEquals("2.01232768false", result.returnValue);
}
use of org.springsource.loaded.TypeRegistry in project spring-loaded by spring-projects.
the class MethodInvokerRewriterTests method rewriteInvokeInterface4_methodDeletion.
/**
* A method is removed from an interface.
*/
@Test
public void rewriteInvokeInterface4_methodDeletion() throws Exception {
TypeRegistry typeRegistry = getTypeRegistry("tgt.SimpleIClass,tgt.SimpleI");
ReloadableType intface = typeRegistry.addType("tgt.SimpleI", loadBytesForClass("tgt.SimpleI"));
typeRegistry.addType("tgt.SimpleIClass", loadBytesForClass("tgt.SimpleIClass"));
byte[] callerbytes = loadBytesForClass("tgt.StaticICaller");
byte[] rewrittenBytes = MethodInvokerRewriter.rewrite(typeRegistry, callerbytes);
Class<?> callerClazz = loadit("tgt.StaticICaller", rewrittenBytes);
Result result = runUnguarded(callerClazz, "run");
assertEquals(123, result.returnValue);
// new interface version has method removed
intface.loadNewVersion("2", retrieveRename("tgt.SimpleI", "tgt.SimpleI004"));
try {
// run the original working thing post-reload - check it is still ok
result = runUnguarded(callerClazz, "run");
fail("Method no longer exists, should not have been callable");
} catch (InvocationTargetException ite) {
assertTrue(ite.getCause() instanceof NoSuchMethodError);
assertEquals("SimpleI.toInt(Ljava/lang/String;)I", ite.getCause().getMessage());
}
// new interface version has method re-added
intface.loadNewVersion("3", retrieveRename("tgt.SimpleI", "tgt.SimpleI"));
result = runUnguarded(callerClazz, "run");
assertEquals(123, result.returnValue);
}
use of org.springsource.loaded.TypeRegistry in project spring-loaded by spring-projects.
the class FieldReloadingTests method newFieldAdded.
// Field 'i' is added to Add on a reload and interacted with
@Test
public void newFieldAdded() throws Exception {
TypeRegistry r = getTypeRegistry("fields.Add");
ReloadableType add = loadType(r, "fields.Add");
Class<?> addClazz = add.getClazz();
Object addInstance = addClazz.newInstance();
assertEquals(0, runOnInstance(addClazz, addInstance, "getValue").returnValue);
// ClassPrinter.print(add.bytesLoaded, true);
add.loadNewVersion("2", retrieveRename("fields.Add", "fields.Add002"));
assertEquals(0, runOnInstance(addClazz, addInstance, "getValue").returnValue);
runOnInstance(addClazz, addInstance, "setValue", 45);
assertEquals(45, runOnInstance(addClazz, addInstance, "getValue").returnValue);
assertEquals(45, add.getField(addInstance, "i", false));
}
use of org.springsource.loaded.TypeRegistry in project spring-loaded by spring-projects.
the class FieldReloadingTests method switchToFromBoxing.
/**
* Switch some fields from their primitive forms to their boxed forms, check data is preserved, then switch them
* back and check data is preserved.
*/
@Test
public void switchToFromBoxing() throws Exception {
String e = "fields.E";
TypeRegistry tr = getTypeRegistry(e);
ReloadableType type = tr.addType(e, loadBytesForClass(e));
Class<?> clazz = type.getClazz();
Object rInstance = clazz.newInstance();
// Before reloading, check both routes get to the same field
assertEquals(100, runOnInstance(clazz, rInstance, "getInt").returnValue);
assertEquals(100, type.getField(rInstance, "i", false));
assertEquals((short) 200, runOnInstance(clazz, rInstance, "getShort").returnValue);
assertEquals((short) 200, type.getField(rInstance, "s", false));
assertEquals(324L, runOnInstance(clazz, rInstance, "getLong").returnValue);
assertEquals(324L, type.getField(rInstance, "j", false));
assertEquals(2.5d, runOnInstance(clazz, rInstance, "getDouble").returnValue);
assertEquals(2.5d, type.getField(rInstance, "d", false));
assertEquals(true, runOnInstance(clazz, rInstance, "getBoolean").returnValue);
assertEquals(true, type.getField(rInstance, "z", false));
assertEquals(32f, runOnInstance(clazz, rInstance, "getFloat").returnValue);
assertEquals(32f, type.getField(rInstance, "f", false));
assertEquals('a', runOnInstance(clazz, rInstance, "getChar").returnValue);
assertEquals('a', type.getField(rInstance, "c", false));
assertEquals((byte) 255, runOnInstance(clazz, rInstance, "getByte").returnValue);
assertEquals((byte) 255, type.getField(rInstance, "b", false));
type.loadNewVersion("2", retrieveRename(e, e + "2"));
// Now all boxed versions
assertEquals(100, runOnInstance(clazz, rInstance, "getInt").returnValue);
assertEquals(100, type.getField(rInstance, "i", false));
assertEquals((short) 200, runOnInstance(clazz, rInstance, "getShort").returnValue);
assertEquals((short) 200, type.getField(rInstance, "s", false));
assertEquals(324L, runOnInstance(clazz, rInstance, "getLong").returnValue);
assertEquals(324L, type.getField(rInstance, "j", false));
assertEquals(2.5d, runOnInstance(clazz, rInstance, "getDouble").returnValue);
assertEquals(2.5d, type.getField(rInstance, "d", false));
assertEquals(true, runOnInstance(clazz, rInstance, "getBoolean").returnValue);
assertEquals(true, type.getField(rInstance, "z", false));
assertEquals(32f, runOnInstance(clazz, rInstance, "getFloat").returnValue);
assertEquals(32f, type.getField(rInstance, "f", false));
assertEquals('a', runOnInstance(clazz, rInstance, "getChar").returnValue);
assertEquals('a', type.getField(rInstance, "c", false));
assertEquals((byte) 255, runOnInstance(clazz, rInstance, "getByte").returnValue);
assertEquals((byte) 255, type.getField(rInstance, "b", false));
// revert to unboxed
type.loadNewVersion("3", loadBytesForClass(e));
assertEquals(100, runOnInstance(clazz, rInstance, "getInt").returnValue);
assertEquals(100, type.getField(rInstance, "i", false));
assertEquals((short) 200, runOnInstance(clazz, rInstance, "getShort").returnValue);
assertEquals((short) 200, type.getField(rInstance, "s", false));
assertEquals(324L, runOnInstance(clazz, rInstance, "getLong").returnValue);
assertEquals(324L, type.getField(rInstance, "j", false));
assertEquals(2.5d, runOnInstance(clazz, rInstance, "getDouble").returnValue);
assertEquals(2.5d, type.getField(rInstance, "d", false));
assertEquals(true, runOnInstance(clazz, rInstance, "getBoolean").returnValue);
assertEquals(true, type.getField(rInstance, "z", false));
assertEquals(32f, runOnInstance(clazz, rInstance, "getFloat").returnValue);
assertEquals(32f, type.getField(rInstance, "f", false));
assertEquals('a', runOnInstance(clazz, rInstance, "getChar").returnValue);
assertEquals('a', type.getField(rInstance, "c", false));
assertEquals((byte) 255, runOnInstance(clazz, rInstance, "getByte").returnValue);
assertEquals((byte) 255, type.getField(rInstance, "b", false));
}
Aggregations