use of org.springsource.loaded.TypeRegistry 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.TypeRegistry 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.TypeRegistry in project spring-loaded by spring-projects.
the class ReloadingJVMCommandProcess method isReloadableTypeCheck.
private static void isReloadableTypeCheck(String classname) {
try {
Class<?> clazz = Class.forName(classname);
TypeRegistry tr = TypeRegistry.getTypeRegistryFor(clazz.getClassLoader());
if (tr.getReloadableType(clazz) != null) {
System.out.println(classname + " is reloadable type");
} else {
System.out.println(classname + " is not reloadable type");
}
} catch (Exception e) {
e.printStackTrace(System.out);
}
}
use of org.springsource.loaded.TypeRegistry 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);
}
use of org.springsource.loaded.TypeRegistry in project spring-loaded by spring-projects.
the class MethodInvokerRewriterTests method callingMethodIntroducedLaterPrimitiveParamsLongDouble.
@Test
public void callingMethodIntroducedLaterPrimitiveParamsLongDouble() 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);
// public String callApple3(String s, int i, double d, String t, int[] is) {
runExpectNoSuchMethodException(callerClazz, "callApple3x", new Object[] { "abc", 1, 2.0d, "def", new int[] { 42, 53 } });
// Load a version of Apple that does define that method
apple.loadNewVersion("002", retrieveRename("data.Apple", "data.Apple002"));
Result result = runUnguarded(callerClazz, "callApple3x", new Object[] { "abc", 1, 2.0d, "def", new int[] { 42, 53 } });
assertEquals("abc12.0def42", result.returnValue);
// Load a version of Apple that doesn't define it
apple.loadNewVersion("003", loadBytesForClass("data.Apple"));
runExpectNoSuchMethodException(callerClazz, "callApple3x", new Object[] { "abc", 1, 2.0d, "def", new int[] { 42, 53 } });
}
Aggregations