use of org.springsource.loaded.TypeRegistry in project spring-loaded by spring-projects.
the class MethodInvokerRewriterTests method rewriteInvokeStatic.
/**
* Rewrite of a simple INVOKESTATIC call.
*/
@Test
public void rewriteInvokeStatic() throws Exception {
TypeRegistry typeRegistry = getTypeRegistry("tgt.SimpleClass");
ReloadableType r = typeRegistry.addType("tgt.SimpleClass", loadBytesForClass("tgt.SimpleClass"));
byte[] callerbytes = loadBytesForClass("tgt.StaticCaller");
// @formatter:off
checkMethod(callerbytes, "run", " L0\n" + " LDC 123\n" + " INVOKESTATIC tgt/SimpleClass.toInt(Ljava/lang/String;)I\n" + " IRETURN\n" + " L1\n");
// @formatter:on
byte[] rewrittenBytes = MethodInvokerRewriter.rewrite(typeRegistry, callerbytes);
// @formatter:off
checkMethod(rewrittenBytes, "run", " L0\n" + " LDC 123\n" + " LDC " + r.getId() + "\n" + " LDC toInt(Ljava/lang/String;)I\n" + " INVOKESTATIC org/springsource/loaded/TypeRegistry.istcheck(ILjava/lang/String;)Ljava/lang/Object;\n" + " DUP\n" + " IFNULL L1\n" + " CHECKCAST tgt/SimpleClass__I\n" + " ASTORE 1\n" + // it would remove from here:
" LDC 1\n" + // load 1
" ANEWARRAY java/lang/Object\n" + // new array of size 1
" DUP_X1\n" + // put it under the argument (it'll be under and on top)
" SWAP\n" + // put it under and under, arg on top
" LDC 0\n" + // load 0
" SWAP\n" + // swap
" AASTORE\n" + // to here
" ALOAD 1\n" + // load the target
" SWAP\n" + // put the target at the bottom
" ACONST_NULL\n" + // load the instance (static call so null)
" LDC toInt(Ljava/lang/String;)I\n" + // load the name+descriptor
" INVOKEINTERFACE tgt/SimpleClass__I.__execute([Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/String;)Ljava/lang/Object;\n" + " CHECKCAST java/lang/Integer\n" + " INVOKEVIRTUAL java/lang/Integer.intValue()I\n" + " GOTO L2\n" + " L1\n" + " POP\n" + " INVOKESTATIC tgt/SimpleClass.toInt(Ljava/lang/String;)I\n" + " L2\n" + " IRETURN\n" + " L3\n");
// @formatter:on
Class<?> callerClazz = loadit("tgt.StaticCaller", rewrittenBytes);
// ClassPrinter.print(r.bytesLoaded);
Result result = runUnguarded(callerClazz, "run");
assertEquals(123, result.returnValue);
}
use of org.springsource.loaded.TypeRegistry in project spring-loaded by spring-projects.
the class MethodInvokerRewriterTests method superCallsDynamicDispatcher.
/**
* This is similar to the first case except the hierarchy is split such that a middle type exists which does not
* initially implement the methods, they are added in a reload. This variant of the testcase is checking dispatch
* through the dynamic dispatch __execute method will work.
*/
@Test
public void superCallsDynamicDispatcher() throws Exception {
TypeRegistry tr = getTypeRegistry("invokespecial..*");
loadType(tr, "invokespecial.Top");
ReloadableType rt = loadType(tr, "invokespecial.Able2");
ReloadableType st = loadType(tr, "invokespecial.Simple2");
rt.loadNewVersion("002", this.retrieveRename("invokespecial.Able2", "invokespecial.Able2002"));
Object object = st.getClazz().newInstance();
Method method = null;
String string = null;
// ClassPrinter.print(rt.bytesLoaded);
method = st.getClazz().getMethod("withParamSuperCaller");
string = (String) method.invoke(object);
assertEquals("2323", string);
method = st.getClazz().getMethod("withDoubleSlotParamSuperCaller");
string = (String) method.invoke(object);
assertEquals("3030", string);
// this call is checking the private field access in the reloaded method has been
// changed to use the accessors into the type that can access the field from outside
method = st.getClazz().getMethod("withParamSuperCallerPrivateVariable");
string = (String) method.invoke(object);
assertEquals("44", string);
}
use of org.springsource.loaded.TypeRegistry 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 org.springsource.loaded.TypeRegistry in project spring-loaded by spring-projects.
the class MethodInvokerRewriterTests method superCallsControlCheck.
/**
* This is the 'control' testcase that loads a pair of types in a hierarchy, and calls methods on the subtype that
* simply make super calls to the supertype. Different variants are tested - with/without parameters, double slot
* parameters and methods that access private instance state. There is no reloading here, it is basically checking
* that the format of the rewritten super calls is OK.
*/
@Test
public void superCallsControlCheck() throws Exception {
TypeRegistry tr = getTypeRegistry("invokespecial..*");
loadType(tr, "invokespecial.Able");
ReloadableType rt = loadType(tr, "invokespecial.Simple");
Object object = rt.getClazz().newInstance();
Method method = rt.getClazz().getMethod("superCaller");
String string = (String) method.invoke(object);
assertEquals("abc", string);
method = rt.getClazz().getMethod("withParamSuperCaller");
string = (String) method.invoke(object);
assertEquals("23", string);
method = rt.getClazz().getMethod("withDoubleSlotParamSuperCaller");
string = (String) method.invoke(object);
assertEquals("30", string);
method = rt.getClazz().getMethod("withParamSuperCallerPrivateVariable");
string = (String) method.invoke(object);
assertEquals("1", string);
}
use of org.springsource.loaded.TypeRegistry in project spring-loaded by spring-projects.
the class MethodInvokerRewriterTests method rewriteInvokeVirtual1.
// TODO review visibility runtime checking. In this next test a static method is changed from public to private. It does
// not currently trigger an error - whether we need to check kind of depends on if we support deployment of broken code. A
// compiler could not create code like this, it can only happen when one end of a call has been deployed but the other end hasnt
// /**
// * If the static method is made non-visible (private), here is what happens in the java case:
// *
// * <pre>
// * Exception in thread "main" java.lang.IllegalAccessError: tried to access method B.foo()V from class A
// * at A.main(A.java:3)
// * </pre>
// */
// @Test
// public void rewriteInvokeStatic7() 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);
//
// Result result = runUnguarded(callerClazz, "run");
// assertEquals(123, result.returnValue);
//
// // new version of SimpleClass where target static method has been made private
// callee.loadNewVersion("7", retrieveRename("tgt.SimpleClass", "tgt.SimpleClass007"));
//
// try {
// ClassPrinter.print(rewrittenBytes);
// result = runUnguarded(callerClazz, "run");
// System.out.println(result.returnValue);
// fail here because the visibility of the changed static method has not been policed
// Assert.fail();
// } catch (RuntimeException rt) {
// rt.printStackTrace();
// InvocationTargetException ite = (InvocationTargetException) rt.getCause();
// Throwable t = ite.getCause();
// IncompatibleClassChangeError icce = (IncompatibleClassChangeError) t;
// assertEquals("Expected static method SimpleClass.toInt(Ljava/lang/String;)I", icce.getMessage());
// }
// }
/**
* The simplest thing - calling a method with no params and no return (keeps generated code short!)
*/
@Test
public void rewriteInvokeVirtual1() throws Exception {
TypeRegistry typeRegistry = getTypeRegistry("invokevirtual.B");
ReloadableType b = loadType(typeRegistry, "invokevirtual.B");
byte[] callerbytes = loadBytesForClass("invokevirtual.A");
byte[] rewrittenBytes = MethodInvokerRewriter.rewrite(typeRegistry, callerbytes);
Class<?> callerClazz = loadit("invokevirtual.A", rewrittenBytes);
Result result = runUnguarded(callerClazz, "run");
Assert.assertNull(result.returnValue);
callerbytes = loadBytesForClass("invokevirtual.A2");
callerbytes = ClassRenamer.rename("invokevirtual.A2", callerbytes, "invokevirtual.B2:invokevirtual.B");
rewrittenBytes = MethodInvokerRewriter.rewrite(typeRegistry, callerbytes);
Class<?> callerClazz002 = loadit("invokevirtual.A2", rewrittenBytes);
b.loadNewVersion("2", retrieveRename("invokevirtual.B", "invokevirtual.B2"));
result = runUnguarded(callerClazz002, "run");
Assert.assertNull(result.returnValue);
}
Aggregations