use of org.springsource.loaded.TypeRegistry in project spring-loaded by spring-projects.
the class MethodInvokerRewriterTests method fieldOverloading.
// @Test
// public void accessingAnnotationTypeReflectively() throws Exception {
// String t = "annos.Play";
// TypeRegistry typeRegistry = getTypeRegistry(t);
// ReloadableType target = typeRegistry.addType(t, loadBytesForClass(t));
// // Class<?> callerClazz = loadit(t, loadBytesForClass(t));
// // Run the initial version which does not define toString()
// Result result = runUnguarded(target.getClazz(), "run");
// target.loadNewVersion("2", target.bytesInitial);
// result = runUnguarded(target.getClazz(), "run");
// System.out.println(result);
// // ClassPrinter.print(target.bytesLoaded);
// }
@Test
public void fieldOverloading() throws Exception {
TypeRegistry r = getTypeRegistry("fields..*");
ReloadableType one = loadType(r, "fields.One");
ReloadableType two = loadType(r, "fields.Two");
Class<?> oneClazz = one.getClazz();
Object oneInstance = oneClazz.newInstance();
Class<?> twoClazz = two.getClazz();
Object twoInstance = twoClazz.newInstance();
// Field 'a' is only defined in One and 'inherited' by Two
assertEquals("a from One", runOnInstance(oneClazz, oneInstance, "getOneA").returnValue);
assertEquals("a from One", runOnInstance(twoClazz, twoInstance, "getTwoA").returnValue);
runOnInstance(oneClazz, oneInstance, "setOneA", "abcde");
assertEquals("abcde", runOnInstance(oneClazz, oneInstance, "getOneA").returnValue);
runOnInstance(twoClazz, twoInstance, "setOneA", "abcde");
assertEquals("abcde", runOnInstance(twoClazz, twoInstance, "getTwoA").returnValue);
// Field 'b' is defined in One and Two
assertEquals("b from One", runOnInstance(oneClazz, oneInstance, "getOneB").returnValue);
assertEquals("b from Two", runOnInstance(twoClazz, twoInstance, "getTwoB").returnValue);
// Field 'c' is private in One and public in Two
assertEquals("c from One", runOnInstance(oneClazz, oneInstance, "getOneC").returnValue);
assertEquals("c from Two", runOnInstance(twoClazz, twoInstance, "getTwoC").returnValue);
// Now... set the private field 'c' in One then try to access the field c in both One and Two
// Should be different if the FieldAccessor is preserving things correctly
runOnInstance(twoClazz, twoInstance, "setOneC", "abcde");
assertEquals("abcde", runOnInstance(twoClazz, twoInstance, "getOneC").returnValue);
assertEquals("c from Two", runOnInstance(twoClazz, twoInstance, "getTwoC").returnValue);
}
use of org.springsource.loaded.TypeRegistry in project spring-loaded by spring-projects.
the class MethodInvokerRewriterTests method virtualDispatchCallingSubMethodIntroducedLater2.
/**
* Calling a method on a target that is initially satisfied by the subtype but is then added to the subtype with a
* different implementation. This is a 3 level hierarchy unlike the previous one, and the new method is added to the
* 'middle' type.
*/
@Test
public void virtualDispatchCallingSubMethodIntroducedLater2() throws Exception {
TypeRegistry tr = getTypeRegistry("invokevirtual..*");
ReloadableType x = loadType(tr, "invokevirtual.XX");
ReloadableType y = loadType(tr, "invokevirtual.YY");
// ReloadableType z =
loadType(tr, "invokevirtual.ZZ");
Method method1 = null;
Method method2 = null;
Method method3 = null;
String string = null;
Object object = x.getClazz().newInstance();
method1 = x.getClazz().getMethod("run1");
method2 = x.getClazz().getMethod("run2");
method3 = x.getClazz().getMethod("run3");
// First call to run() will dispatch on 'ZZ' but as ZZ doesn't implement foo, and neither does YY, then XX.foo will be used
string = method1.invoke(object).toString();
assertEquals("1111", string);
string = method2.invoke(object).toString();
assertEquals("1111", string);
string = method3.invoke(object).toString();
assertEquals("1111", string);
// Load a new version of Y that now implements foo
y.loadNewVersion("002", this.retrieveRename("invokevirtual.YY", "invokevirtual.YY002"));
string = method1.invoke(object).toString();
assertEquals("3333", string);
string = method2.invoke(object).toString();
assertEquals("3333", string);
string = method3.invoke(object).toString();
assertEquals("3333", string);
}
use of org.springsource.loaded.TypeRegistry 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());
}
}
use of org.springsource.loaded.TypeRegistry in project spring-loaded by spring-projects.
the class MethodInvokerRewriterTests method callingMethodIntroducedLaterPrimitiveParams.
// Method is 'String run2(String a, int b, String c, int d)' which does not exist in Apple but exists in
/**
* Targets for the calls here are: Apple then Apple002 Caller is Orange002
*/
@Test
public void callingMethodIntroducedLaterPrimitiveParams() throws Exception {
TypeRegistry typeRegistry = getTypeRegistry("data.Apple");
ReloadableType target = typeRegistry.addType("data.Apple", loadBytesForClass("data.Apple"));
byte[] callerb = ClassRenamer.rename("data.Orange", loadBytesForClass("data.Orange002"), "data.Apple002:data.Apple");
byte[] rewrittencallerb = MethodInvokerRewriter.rewrite(typeRegistry, callerb);
Class<?> callerClazz = loadit("data.Orange", rewrittencallerb);
// Method does not exist yet
runExpectNoSuchMethodException(callerClazz, "callApple2", 3);
// Load a version of Apple that does define that method
target.loadNewVersion("002", retrieveRename("data.Apple", "data.Apple002"));
Result result = runUnguarded(callerClazz, "callApple2", 4);
assertEquals("run2 4", result.returnValue);
// Load a version of Apple that doesn't define it
target.loadNewVersion("003", loadBytesForClass("data.Apple"));
runExpectNoSuchMethodException(callerClazz, "callApple2", new Object[] { 5 });
}
use of org.springsource.loaded.TypeRegistry in project spring-loaded by spring-projects.
the class ReloadableTypeTests method protectedFieldAccessors.
@Test
public void protectedFieldAccessors() throws Exception {
TypeRegistry tr = getTypeRegistry("prot.SubOne");
ReloadableType rtype = tr.addType("prot.SubOne", loadBytesForClass("prot.SubOne"));
Object instance = rtype.getClazz().newInstance();
runOnInstance(rtype.getClazz(), instance, "setPublicField", 3);
assertEquals(3, runOnInstance(rtype.getClazz(), instance, "getPublicField").returnValue);
runOnInstance(rtype.getClazz(), instance, "setProtectedField", 3);
assertEquals(3, runOnInstance(rtype.getClazz(), instance, "getProtectedField").returnValue);
runOnInstance(rtype.getClazz(), instance, "setProtectedShortField", (short) 33);
assertEquals((short) 33, runOnInstance(rtype.getClazz(), instance, "getProtectedShortField").returnValue);
runOnInstance(rtype.getClazz(), instance, "setProtectedByteField", (byte) 133);
assertEquals((byte) 133, runOnInstance(rtype.getClazz(), instance, "getProtectedByteField").returnValue);
runOnInstance(rtype.getClazz(), instance, "setProtectedCharField", (char) 12);
assertEquals((char) 12, runOnInstance(rtype.getClazz(), instance, "getProtectedCharField").returnValue);
runOnInstance(rtype.getClazz(), instance, "setProtectedBooleanField", true);
assertEquals(true, runOnInstance(rtype.getClazz(), instance, "isProtectedBooleanField").returnValue);
runOnInstance(rtype.getClazz(), instance, "setProtectedDoubleField", 3.1d);
assertEquals(3.1d, runOnInstance(rtype.getClazz(), instance, "getProtectedDoubleField").returnValue);
runOnInstance(rtype.getClazz(), instance, "setProtectedFloatField", 8f);
assertEquals(8f, runOnInstance(rtype.getClazz(), instance, "getProtectedFloatField").returnValue);
runOnInstance(rtype.getClazz(), instance, "setProtectedLongField", 888L);
assertEquals(888L, runOnInstance(rtype.getClazz(), instance, "getProtectedLongField").returnValue);
runOnInstance(rtype.getClazz(), instance, "setProtectedArrayOfInts", new int[] { 1, 2, 3 });
assertEquals("[1,2,3]", toString(runOnInstance(rtype.getClazz(), instance, "getProtectedArrayOfInts").returnValue));
runOnInstance(rtype.getClazz(), instance, "setProtectedArrayOfStrings", (Object) new String[] { "a", "b", "c" });
assertEquals("[a,b,c]", toString(runOnInstance(rtype.getClazz(), instance, "getProtectedArrayOfStrings").returnValue));
runOnInstance(rtype.getClazz(), instance, "setProtectedArrayOfArrayOfLongs", (Object) new long[][] { new long[] { 3L }, new long[] { 4L, 45L }, new long[] { 7L } });
assertEquals("[[3],[4,45],[7]]", toString(runOnInstance(rtype.getClazz(), instance, "getProtectedArrayOfArrayOfLongs").returnValue));
runOnInstance(rtype.getClazz(), instance, "setProtectedStaticField", 3);
assertEquals(3, runOnInstance(rtype.getClazz(), instance, "getProtectedStaticField").returnValue);
rtype.loadNewVersion(rtype.bytesInitial);
runOnInstance(rtype.getClazz(), instance, "setPublicField", 4);
assertEquals(4, runOnInstance(rtype.getClazz(), instance, "getPublicField").returnValue);
runOnInstance(rtype.getClazz(), instance, "setProtectedField", 3);
assertEquals(3, runOnInstance(rtype.getClazz(), instance, "getProtectedField").returnValue);
runOnInstance(rtype.getClazz(), instance, "setProtectedShortField", (short) 33);
assertEquals((short) 33, runOnInstance(rtype.getClazz(), instance, "getProtectedShortField").returnValue);
runOnInstance(rtype.getClazz(), instance, "setProtectedByteField", (byte) 133);
assertEquals((byte) 133, runOnInstance(rtype.getClazz(), instance, "getProtectedByteField").returnValue);
runOnInstance(rtype.getClazz(), instance, "setProtectedCharField", (char) 12);
assertEquals((char) 12, runOnInstance(rtype.getClazz(), instance, "getProtectedCharField").returnValue);
runOnInstance(rtype.getClazz(), instance, "setProtectedBooleanField", true);
assertEquals(true, runOnInstance(rtype.getClazz(), instance, "isProtectedBooleanField").returnValue);
runOnInstance(rtype.getClazz(), instance, "setProtectedDoubleField", 3.1d);
assertEquals(3.1d, runOnInstance(rtype.getClazz(), instance, "getProtectedDoubleField").returnValue);
runOnInstance(rtype.getClazz(), instance, "setProtectedFloatField", 8f);
assertEquals(8f, runOnInstance(rtype.getClazz(), instance, "getProtectedFloatField").returnValue);
runOnInstance(rtype.getClazz(), instance, "setProtectedLongField", 888L);
assertEquals(888L, runOnInstance(rtype.getClazz(), instance, "getProtectedLongField").returnValue);
runOnInstance(rtype.getClazz(), instance, "setProtectedArrayOfInts", new int[] { 1, 2, 3 });
assertEquals("[1,2,3]", toString(runOnInstance(rtype.getClazz(), instance, "getProtectedArrayOfInts").returnValue));
runOnInstance(rtype.getClazz(), instance, "setProtectedArrayOfStrings", (Object) new String[] { "a", "b", "c" });
assertEquals("[a,b,c]", toString(runOnInstance(rtype.getClazz(), instance, "getProtectedArrayOfStrings").returnValue));
runOnInstance(rtype.getClazz(), instance, "setProtectedArrayOfArrayOfLongs", (Object) new long[][] { new long[] { 3L }, new long[] { 4L, 45L }, new long[] { 7L } });
assertEquals("[[3],[4,45],[7]]", toString(runOnInstance(rtype.getClazz(), instance, "getProtectedArrayOfArrayOfLongs").returnValue));
runOnInstance(rtype.getClazz(), instance, "setProtectedStaticField", 3);
assertEquals(3, runOnInstance(rtype.getClazz(), instance, "getProtectedStaticField").returnValue);
}
Aggregations