use of org.springsource.loaded.test.infra.ResultException in project spring-loaded by spring-projects.
the class FieldReloadingTests method changingFieldFromStaticToNonstaticWithSubtypes.
/**
* A static field is accessed from a subtype, then the supertype is reloaded where the field has been made
* non-static. Should be an error when the subtype attempts to access it again.
*/
@Test
public void changingFieldFromStaticToNonstaticWithSubtypes() throws Exception {
String y = "fields.Yb";
String z = "fields.Zb";
TypeRegistry tr = getTypeRegistry(y + "," + z);
ReloadableType rtype = tr.addType(y, loadBytesForClass(y));
ReloadableType ztype = tr.addType(z, loadBytesForClass(z));
Class<?> clazz = ztype.getClazz();
Object instance = clazz.newInstance();
assertEquals(5, runOnInstance(clazz, instance, "getJ").returnValue);
runOnInstance(clazz, instance, "setJ", 4);
assertEquals(4, runOnInstance(clazz, instance, "getJ").returnValue);
rtype.loadNewVersion("2", retrieveRename(y, y + "002"));
// Now should be an IncompatibleClassChangeError
try {
runOnInstance(clazz, instance, "setJ", 4);
fail("Should not have worked, field has changed from static to non-static");
} catch (ResultException re) {
assertTrue(re.getCause() instanceof InvocationTargetException);
assertTrue(re.getCause().getCause() instanceof IncompatibleClassChangeError);
// When compiled with AspectJ vs Eclipse JDT the GETSTATIC actually varies.
// With AspectJ it is: PUTSTATIC fields/Yb.j : I
// With JDT (4.3) it is: PUTSTATIC fields/Zb.j : I
// hence the error is different
assertEquals("Expected static field fields.Zb.j", re.getCause().getCause().getMessage());
}
// Now should be an IncompatibleClassChangeError
try {
runOnInstance(clazz, instance, "getJ");
fail("Should not have worked, field has changed from static to non-static");
} catch (ResultException re) {
assertTrue(re.getCause() instanceof InvocationTargetException);
assertTrue(re.getCause().getCause() instanceof IncompatibleClassChangeError);
// When compiled with AspectJ vs Eclipse JDT the GETSTATIC actually varies.
// With AspectJ it is: GETSTATIC fields/Yb.j : I
// With JDT (4.3) it is: GETSTATIC fields/Zb.j : I
// hence the error is different
assertEquals("Expected static field fields.Zb.j", re.getCause().getCause().getMessage());
}
}
use of org.springsource.loaded.test.infra.ResultException in project spring-loaded by spring-projects.
the class FieldReloadingTests method changingFieldFromNonstaticToStaticWithSubtypes.
/**
* An instance field is accessed from a subtype, then the supertype is reloaded where the field has been made
* static. Should be an error when the subtype attempts to access it again.
*/
@Test
public void changingFieldFromNonstaticToStaticWithSubtypes() throws Exception {
String y = "fields.Ya";
String z = "fields.Za";
TypeRegistry tr = getTypeRegistry(y + "," + z);
ReloadableType rtype = tr.addType(y, loadBytesForClass(y));
ReloadableType ztype = tr.addType(z, loadBytesForClass(z));
Class<?> clazz = ztype.getClazz();
Object instance = clazz.newInstance();
assertEquals(5, runOnInstance(clazz, instance, "getJ").returnValue);
runOnInstance(clazz, instance, "setJ", 4);
assertEquals(4, runOnInstance(clazz, instance, "getJ").returnValue);
rtype.loadNewVersion("2", retrieveRename(y, y + "002"));
try {
runOnInstance(clazz, instance, "setJ", 4);
fail("should not have worked, field has changed from non-static to static");
} catch (ResultException re) {
assertTrue(re.getCause() instanceof InvocationTargetException);
assertTrue(re.getCause().getCause() instanceof IncompatibleClassChangeError);
assertEquals("Expected non-static field fields.Za.j", re.getCause().getCause().getMessage());
}
// Now should be an IncompatibleClassChangeError
try {
runOnInstance(clazz, instance, "getJ");
fail("should not have worked, field has changed from non-static to static");
} catch (ResultException re) {
assertTrue(re.getCause() instanceof InvocationTargetException);
assertTrue(re.getCause().getCause() instanceof IncompatibleClassChangeError);
assertEquals("Expected non-static field fields.Za.j", re.getCause().getCause().getMessage());
}
}
use of org.springsource.loaded.test.infra.ResultException in project spring-loaded by spring-projects.
the class TypeRewriterTests method reflectiveFieldGet.
/**
* Testing reflective field access when the field flips from static to non-static.
*/
@Test
public void reflectiveFieldGet() throws Exception {
String a = "reflect.FieldAccessing";
TypeRegistry r = getTypeRegistry(a);
ReloadableType type = r.addType(a, loadBytesForClass(a));
Object o = type.getClazz().newInstance();
// Access the fields
result = runOnInstance(type.getClazz(), o, "geti");
assertEquals(4, ((Integer) result.returnValue).intValue());
result = runOnInstance(type.getClazz(), o, "getj");
assertEquals(5, ((Integer) result.returnValue).intValue());
// Load a new version that switches them from static<>non-static
type.loadNewVersion("2", retrieveRename(a, a + "2"));
try {
result = runOnInstance(type.getClazz(), o, "geti");
fail();
} catch (ResultException re) {
Throwable cause = re.getCause();
assertTrue(cause instanceof InvocationTargetException);
cause = ((InvocationTargetException) cause).getCause();
assertTrue(cause instanceof IncompatibleClassChangeError);
assertEquals("Expected non-static field reflect/FieldAccessing.i", cause.getMessage());
}
try {
result = runOnInstance(type.getClazz(), o, "getj");
fail();
} catch (ResultException re) {
Throwable cause = re.getCause();
assertTrue(cause instanceof InvocationTargetException);
cause = ((InvocationTargetException) cause).getCause();
assertTrue(cause instanceof IncompatibleClassChangeError);
assertEquals("Expected static field reflect/FieldAccessing.j", cause.getMessage());
}
}
use of org.springsource.loaded.test.infra.ResultException in project spring-loaded by spring-projects.
the class ClassGetFieldsTest method test.
@SuppressWarnings("unchecked")
@Override
public Result test() throws ResultException, Exception {
try {
Result r = runOnInstance(callerClazz, callerInstance, targetMethodName, targetClass);
collectFieldNames((List<Field>) r.returnValue);
return r;
} catch (ResultException e) {
throw new Error(e);
}
}
use of org.springsource.loaded.test.infra.ResultException in project spring-loaded by spring-projects.
the class MethodGetParamAnnotationsTest method test.
@Override
public Result test() throws ResultException, Exception {
try {
Result r = runOnInstance(callerClazz, callerInstance, testedMethodCaller, method);
Assert.assertTrue(r.returnValue instanceof List<?>);
return r;
} catch (ResultException e) {
throw new Error(e);
}
}
Aggregations