Search in sources :

Example 76 with ReloadableType

use of org.springsource.loaded.ReloadableType 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);
}
Also used : ReloadableType(org.springsource.loaded.ReloadableType) Method(java.lang.reflect.Method) TypeRegistry(org.springsource.loaded.TypeRegistry) Test(org.junit.Test)

Example 77 with ReloadableType

use of org.springsource.loaded.ReloadableType 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);
}
Also used : ReloadableType(org.springsource.loaded.ReloadableType) TypeRegistry(org.springsource.loaded.TypeRegistry) Result(org.springsource.loaded.test.infra.Result) Test(org.junit.Test)

Example 78 with ReloadableType

use of org.springsource.loaded.ReloadableType in project spring-loaded by spring-projects.

the class MethodInvokerRewriterTests method rewriteInvokeStatic3.

/**
	 * Reloading target with a new static method that takes no parameters.
	 */
@Test
public void rewriteInvokeStatic3() 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);
    callerbytes = loadBytesForClass("tgt.StaticCaller003");
    callerbytes = ClassRenamer.rename("tgt.StaticCaller003", callerbytes, "tgt.SimpleClass003:tgt.SimpleClass");
    rewrittenBytes = MethodInvokerRewriter.rewrite(typeRegistry, callerbytes);
    Class<?> callerClazz002 = loadit("tgt.StaticCaller003", rewrittenBytes);
    callee.loadNewVersion("3", retrieveRename("tgt.SimpleClass", "tgt.SimpleClass003"));
    result = runUnguarded(callerClazz002, "run3");
    assertEquals("42", result.returnValue);
}
Also used : ReloadableType(org.springsource.loaded.ReloadableType) TypeRegistry(org.springsource.loaded.TypeRegistry) Result(org.springsource.loaded.test.infra.Result) Test(org.junit.Test)

Example 79 with ReloadableType

use of org.springsource.loaded.ReloadableType in project spring-loaded by spring-projects.

the class MethodInvokerRewriterTests method basicRewriteSingleNonStaticMethodCallNoArgumentsNoReturn2.

// Exercising the rewritten code
@Test
public void basicRewriteSingleNonStaticMethodCallNoArgumentsNoReturn2() throws Exception {
    TypeRegistry typeRegistry = getTypeRegistry("data.Apple");
    ReloadableType target = typeRegistry.addType("data.Apple", loadBytesForClass("data.Apple"));
    byte[] callerbytes = loadBytesForClass("data.Orange");
    byte[] rewrittenBytes = MethodInvokerRewriter.rewrite(typeRegistry, callerbytes);
    Class<?> callerClazz = loadit("data.Orange", rewrittenBytes);
    // run the original
    result = runUnguarded(callerClazz, "one");
    assertEquals("Apple.run() is running", result.stdout);
    target.loadNewVersion("002", retrieveRename("data.Apple", "data.Apple002"));
    result = runUnguarded(callerClazz, "one");
    assertEquals("Apple002.run() is running", result.stdout);
// run a modified version
// remove the target method - should fail
// replace the target method - should recover
// run(orangeClazz,"oneCodeBefore");
// run(orangeClazz,"oneCodeAfter");
// run(orangeClazz,"oneCodeBeforeAndAfter");
}
Also used : ReloadableType(org.springsource.loaded.ReloadableType) TypeRegistry(org.springsource.loaded.TypeRegistry) Test(org.junit.Test)

Example 80 with ReloadableType

use of org.springsource.loaded.ReloadableType in project spring-loaded by spring-projects.

the class MethodInvokerRewriterTests method callingMethodIntroducedLaterReturningPrimitiveBoolean.

@Test
public void callingMethodIntroducedLaterReturningPrimitiveBoolean() 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, "callAppleRetBoolean", new Object[] { true });
    // Load a version of Apple that does define that method
    apple.loadNewVersion("002", retrieveRename("data.Apple", "data.Apple002"));
    Result result = runUnguarded(callerClazz, "callAppleRetBoolean", new Object[] { true });
    assertEquals(false, result.returnValue);
}
Also used : ReloadableType(org.springsource.loaded.ReloadableType) TypeRegistry(org.springsource.loaded.TypeRegistry) Result(org.springsource.loaded.test.infra.Result) Test(org.junit.Test)

Aggregations

ReloadableType (org.springsource.loaded.ReloadableType)375 Test (org.junit.Test)320 TypeRegistry (org.springsource.loaded.TypeRegistry)287 Result (org.springsource.loaded.test.infra.Result)106 Method (java.lang.reflect.Method)37 InvocationTargetException (java.lang.reflect.InvocationTargetException)26 TestClassloaderWithRewriting (org.springsource.loaded.test.infra.TestClassloaderWithRewriting)22 Ignore (org.junit.Ignore)17 CurrentLiveVersion (org.springsource.loaded.CurrentLiveVersion)10 AccessibleObject (java.lang.reflect.AccessibleObject)9 ResultException (org.springsource.loaded.test.infra.ResultException)9 MethodMember (org.springsource.loaded.MethodMember)8 Field (java.lang.reflect.Field)6 TypeDescriptor (org.springsource.loaded.TypeDescriptor)6 IOException (java.io.IOException)5 Annotation (java.lang.annotation.Annotation)4 ZipEntry (java.util.zip.ZipEntry)4 ZipFile (java.util.zip.ZipFile)4 LoadtimeInstrumentationPlugin (org.springsource.loaded.LoadtimeInstrumentationPlugin)4 File (java.io.File)3