use of org.springsource.loaded.test.infra.ResultException in project spring-loaded by spring-projects.
the class ClassReflectionTests method test_callReloadableMethodWithNonReloadableImplementation.
/**
* Scenario where method lookup spills over from the reloadable world into the non-reloadable world. This can only
* happen if are looking for a method that is declared on a reloadable type, but we need to find the implementation
* in a non-reloadable one.
*/
@Test
public void test_callReloadableMethodWithNonReloadableImplementation() throws Exception {
//Scenario requires a method that is reloadable but not implemented by a reloadable
//class. The followig circumstances trigger this condition:
// Situation involves three types:
// A non reloadable superClass
String superClassName = "reflection.NonReloadableSuperClass";
Class<?> superClass = nonReloadableClass(superClassName);
// A reloadable interface
String interfaceName = TARGET_PACKAGE + ".InterfaceTarget";
ReloadableType interfaceTarget = reloadableClass(interfaceName);
ClassPrinter.print(interfaceTarget.getBytesLoaded());
// A reloadable class
String subclassName = TARGET_PACKAGE + ".SubClassImplementsInterface";
ReloadableType subClass = reloadableClass(subclassName);
// These types relate to one another as follows:
//1) the method 'm' must be declared in the reloadable interface
String interfaceMethodName = "interfaceMethod";
Result r = getDeclaredMethod(interfaceTarget.getClazz(), interfaceMethodName);
assertMethod("public abstract java.lang.String " + interfaceName + "." + interfaceMethodName + "()", r);
Method m = (Method) r.returnValue;
//2) The reloadable type implements this interface (without providing its own implementation of m)
assertTrue(interfaceTarget.getClazz().isAssignableFrom(subClass.getClazz()));
try {
r = getDeclaredMethod(subClass.getClazz(), interfaceMethodName);
fail("Assuming that interface implementation is inherited, not directly implemented");
} catch (ResultException e) {
assertNoSuchMethodException(subclassName + "." + interfaceMethodName + "()", e);
}
//3) A non-reloadable superclass provides the actual implementation of m via inheritance
r = getDeclaredMethod(superClass, interfaceMethodName);
assertMethod("public java.lang.String " + superClassName + "." + interfaceMethodName + "()", r);
//4) Invoke the interface method on an instance of the subclass... should cause lookup
// to find implementation in non-reloadable superclass
Object subInstance = subClass.getClazz().newInstance();
r = invokeOn(subInstance, m);
assertEquals("NonReloadableSuperClass.interfaceMethod", r.returnValue);
}
use of org.springsource.loaded.test.infra.ResultException in project spring-loaded by spring-projects.
the class ConstructorAdHocTest method test_accessDeletedConstructor.
// /**
// * When a Constructor is "regotten" the newly gotten Field will have to be a "fresh" object with its accessibility flag NOT
// * set.
// */
// private void doTestAccessibleFlagIsRefreshed(Constructor<?> cons) throws Exception {
// registry = getTypeRegistry("reflection.constructors..*");
// targetClass = reloadableClass("reflection.constructors.ClassForNewInstance");
//
// callerClazz = nonReloadableClass(INVOKER_CLASS_NAME);
// callerInstance = newInstance(callerClazz);
//
// String fieldToAccess = scope + "Field";
// String expectMsg = "Class " + INVOKER_CLASS_NAME + " " + "can not access a member of class " + targetClass.dottedtypename + " "
// + "with modifiers \"" + (scope.equals("default") ? "" : scope) + "\"";
//
// //First... we do set the Access flag, it should work!
// Result r = runOnInstance(callerClazz, callerInstance, "getFieldWithAccess", targetClass.getClazz(), fieldToAccess, true);
// Assert.assertEquals(r.returnValue, fieldToAccess + " value");
//
// //Then... we do not set the Access flag, it should fail!
// try {
// r = runOnInstance(callerClazz, callerInstance, "getFieldWithAccess", targetClass.getClazz(), fieldToAccess, false);
// Assert.fail("Without setting access flag shouldn't be allowed!");
// } catch (ResultException e) {
// assertIllegalAccess(expectMsg, e);
// }
//
// //Finally, this should also still work... after we reload the type!
// reloadType(targetClass, "002");
//
// //First... we do set the Access flag, it should work!
// r = runOnInstance(callerClazz, callerInstance, "getFieldWithAccess", targetClass.getClazz(), fieldToAccess, true);
// Assert.assertEquals("new "+fieldToAccess + " value", r.returnValue);
//
// //Then... we do not set the Access flag, it should not be allowed!
// try {
// r = runOnInstance(callerClazz, callerInstance, "getFieldWithAccess", targetClass.getClazz(), fieldToAccess, false);
// Assert.fail("Without setting access flag shouldn't be allowed!");
// } catch (ResultException e) {
// assertIllegalAccess(expectMsg, e);
// }
// }
// @Test
// public void test_accessibleFlagIsRefreshedForPrivate() throws Exception {
// doTestAccessibleFlagIsRefreshed("private");
// }
//
// @Test
// public void test_accessibleFlagIsRefreshedForProtected() throws Exception {
// doTestAccessibleFlagIsRefreshed("protected");
// }
//
// @Test
// public void test_accessibleFlagIsRefreshedForDefault() throws Exception {
// doTestAccessibleFlagIsRefreshed("default");
// }
@Test
public void test_accessDeletedConstructor() throws Exception {
registry = getTypeRegistry("reflection.constructors..*");
targetClass = reloadableClass("reflection.constructors.ClassForNewInstance");
// Object targetInstance =
newInstance(targetClass.getClazz());
Constructor<?> c = jlClassGetConstructor(targetClass.getClazz(), char.class, char.class);
Assert.assertEquals("public reflection.constructors.ClassForNewInstance(char,char)", c.toString());
// First try to access the Constructor before reloading
callerClazz = nonReloadableClass(INVOKER_CLASS_NAME);
callerInstance = newInstance(callerClazz);
Result r = runOnInstance(callerClazz, callerInstance, "callNewInstance", c, (Object) new Object[] { 'a', 'b' });
Assert.assertEquals(targetClass.getClazz(), r.returnValue.getClass());
// Now reload the class... constructor is deleted
reloadType(targetClass, "002");
try {
r = runOnInstance(callerClazz, callerInstance, "callNewInstance", c, (Object) new Object[] { 'a', 'b' });
Assert.fail("Expected an error");
} catch (ResultException re) {
Throwable e = re.getCause();
// e.printStackTrace();
Assert.assertEquals(InvocationTargetException.class, e.getClass());
//Nested exception
e = e.getCause();
e.printStackTrace();
Assert.assertEquals(NoSuchMethodError.class, e.getClass());
//Example error message from Sun JVM:
// Exception in thread "main" java.lang.NoSuchMethodError: Target.<init>(C)V
// at Main.main(Main.java:10)
Assert.assertEquals("reflection.constructors.ClassForNewInstance.<init>(CC)V", e.getMessage());
}
}
use of org.springsource.loaded.test.infra.ResultException in project spring-loaded by spring-projects.
the class FieldAdHocTest method doTestAccessibleFlagIsRefreshed.
/**
* When a Field is "regotten" the newly gotten Field will have to be a "fresh" object with its accessibility flag
* NOT set.
*/
private void doTestAccessibleFlagIsRefreshed(String scope) throws Exception {
registry = getTypeRegistry("reflection.fields..*");
targetClass = reloadableClass("reflection.fields.FieldSetAccessTarget");
callerClazz = nonReloadableClass(INVOKER_CLASS_NAME);
callerInstance = newInstance(callerClazz);
String fieldToAccess = scope + "Field";
String expectMsg = "Class " + INVOKER_CLASS_NAME + " " + "can not access a member of class " + targetClass.dottedtypename + " " + "with modifiers \"" + (scope.equals("default") ? "" : scope) + "\"";
//First... we do set the Access flag, it should work!
Result r = runOnInstance(callerClazz, callerInstance, "getFieldWithAccess", targetClass.getClazz(), fieldToAccess, true);
Assert.assertEquals(r.returnValue, fieldToAccess + " value");
//Then... we do not set the Access flag, it should fail!
try {
r = runOnInstance(callerClazz, callerInstance, "getFieldWithAccess", targetClass.getClazz(), fieldToAccess, false);
Assert.fail("Without setting access flag shouldn't be allowed!");
} catch (ResultException e) {
assertIllegalAccess(expectMsg, e);
}
//Finally, this should also still work... after we reload the type!
reloadType(targetClass, "002");
//First... we do set the Access flag, it should work!
r = runOnInstance(callerClazz, callerInstance, "getFieldWithAccess", targetClass.getClazz(), fieldToAccess, true);
Assert.assertEquals("new " + fieldToAccess + " value", r.returnValue);
//Then... we do not set the Access flag, it should not be allowed!
try {
r = runOnInstance(callerClazz, callerInstance, "getFieldWithAccess", targetClass.getClazz(), fieldToAccess, false);
Assert.fail("Without setting access flag shouldn't be allowed!");
} catch (ResultException e) {
assertIllegalAccess(expectMsg, e);
}
}
use of org.springsource.loaded.test.infra.ResultException in project spring-loaded by spring-projects.
the class FieldAdHocTest method test_accessDeletedField.
@Test
public void test_accessDeletedField() throws Exception {
registry = getTypeRegistry("reflection.fields..*");
targetClass = reloadableClass("reflection.fields.ClassTarget");
Object targetInstance = newInstance(targetClass.getClazz());
Field f = jlClassGetField(targetClass.getClazz(), "myDeletedField");
Assert.assertEquals("myDeletedField", f.getName());
// First try to access the field before reloading
callerClazz = nonReloadableClass(INVOKER_CLASS_NAME);
callerInstance = newInstance(callerClazz);
Result r = runOnInstance(callerClazz, callerInstance, "callGet", f, targetInstance);
Assert.assertEquals(100, r.returnValue);
// Now reload the class... field is deleted
reloadType(targetClass, "002");
try {
r = runOnInstance(callerClazz, callerInstance, "callGet", f, targetInstance);
Assert.fail("Expected an error");
} catch (ResultException re) {
Throwable e = re.getCause();
// e.printStackTrace();
Assert.assertEquals(InvocationTargetException.class, e.getClass());
//Nested exception
e = e.getCause();
Assert.assertEquals(NoSuchFieldError.class, e.getClass());
Assert.assertEquals("myDeletedField", e.getMessage());
}
}
use of org.springsource.loaded.test.infra.ResultException in project spring-loaded by spring-projects.
the class FieldGetAnnotationsTest method test.
@Override
public Result test() throws ResultException, Exception {
try {
Result r = runOnInstance(callerClazz, callerInstance, testedMethodCaller, field);
Assert.assertTrue(r.returnValue instanceof List<?>);
return r;
} catch (ResultException e) {
throw new Error(e);
}
}
Aggregations