/** * @see Reflect#isCollectionField(Field) */ @Test public void isCollectionField_shouldReturnTrueIfGivenFieldIsCollectionAndItsElementTypeIsGivenParameterized() { Reflect reflect = new Reflect(OpenmrsObject.class); List<Field> allFields = Reflect.getAllFields(OpenmrsObjectImp.class); Assert.assertEquals("subClassField", allFields.get(1).getName()); Assert.assertTrue(reflect.isCollectionField(allFields.get(1))); }
Reflect reflect = new Reflect(OpenmrsObject.class); List<Field> allInheritedFields = reflect.getInheritedFields(openmrsObjectClass); if (Reflect.isAnnotationPresent(openmrsObjectClass, field.getName(), Independent.class)) { continue; if (reflect.isCollectionField(field) && !isHandlerMarkedAsDisabled(handlerType, field)) {
/** * @see Reflect#hasField(Field) */ @Test public void xhasField_shouldReturnTrueIfGivenFieldIsDeclaredInParameterizedClassOrSubClass() { Reflect reflect = new Reflect(OpenmrsObject.class); List<Field> allFields = Reflect.getAllFields(OpenmrsObjectImp.class); Field genericCollectionField = findFieldByName(allFields, "genericCollectionField"); Assert.assertTrue(reflect.hasField(genericCollectionField)); }
/** * This method return all the fields (including private) until the given parameterized class * * @param subClass Class * @return List<Field> * @should return only the sub class fields of given parameterized class */ public List<Field> getInheritedFields(Class<?> subClass) { List<Field> allFields = getAllFields(subClass); for (Iterator<Field> iterator = allFields.iterator(); iterator.hasNext();) { Field field = iterator.next(); if (!hasField(field)) { iterator.remove(); } } return allFields; }
/** * This method validate the given field is Collection and the elements should be of * parameterized type * * @param field Field * @return boolean * @should return true if given field is Collection and its element type is given parameterized * class type * @should return false if given field is not a Collection * @should return false if given field is Collection and element type is other than given * parameterized class type */ @SuppressWarnings("unchecked") public boolean isCollectionField(Field field) { if (isCollection(field.getType())) { try { ParameterizedType type = (ParameterizedType) field.getGenericType(); if (type.getActualTypeArguments()[0] instanceof Class) { return (parametrizedClass.isAssignableFrom((Class) type.getActualTypeArguments()[0])); } else if (type.getActualTypeArguments()[0] instanceof TypeVariable) { return isSuperClass(type.getActualTypeArguments()[0]); } } catch (ClassCastException e) { // Do nothing. If this exception is thrown, then field is not a Collection of OpenmrsObjects } } return false; }
/** * @see RequiredDataAdvice#isHandlerMarkedAsDisabled(Class, java.lang.reflect.Field) */ @Test public void isHandlerMarkedAsDisabled_shouldReturnFalseIfFieldNotAnnotated() { Field persons = null; for (Field field : Reflect.getAllFields(ClassWithDisableHandlersAnnotation.class)) { if (field.getName().equals("notAnnotatedPersons")) { persons = field; } } Assert.assertFalse(RequiredDataAdvice.isHandlerMarkedAsDisabled(RetireHandler.class, persons)); }
/** * @see Reflect#isSuperClass(Object) */ @Test public void isSuperClass_shouldReturnFalseIfGivenObjectIsNotAccessibleFromGivenParameterizedClass() { Reflect reflect = new Reflect(OpenmrsObject.class); Assert.assertFalse(reflect.isSuperClass(NormalClass.class)); }
/** * @see Reflect#getInheritedFields(Class) */ @Test public void getInheritedFields_shouldReturnOnlyTheSubClassFieldsOfGivenParameterizedClass() { Reflect reflect = new Reflect(OpenmrsObject.class); List<Field> fields = reflect.getInheritedFields(OpenmrsObjectImp.class); List<Field> allFields = Reflect.getAllFields(OpenmrsObjectImp.class); findFieldByName(fields, "subClassField"); findFieldByName(fields, "nonCollectionField"); findFieldByName(fields, "genericCollectionField"); Field normalClassField = findFieldByName(allFields, "normalClassField"); Assert.assertFalse(fields.contains(normalClassField)); }
Reflect reflect = new Reflect(OpenmrsObject.class); if (reflect.isSuperClass(mainArgument)) { else if (Reflect.isCollection(mainArgument) && isOpenmrsObjectCollection(mainArgument)) {
/** * @param object Object * @return true if, given object is Collection otherwise returns false * @should return true if given object is Collection class * @should return false if given object is not a Collection */ public static boolean isCollection(Object object) { return isCollection(object.getClass()); }
/** * @param object Object * @return true if, given object is accessible from the parameterized class * @should return true if given object is accessible from given parameterized class * @should return false if given object is not accessible from given parameterized class */ public boolean isSuperClass(Object object) { return isSuperClass(object.getClass()); }
/** * @see Reflect#Reflect(Class) */ @Test(expected = NullPointerException.class) public void Reflect_shouldThrowExceptionWhenNullIsPassed() { new Reflect(null); }
/** * @see RequiredDataAdvice#isHandlerMarkedAsDisabled(Class, java.lang.reflect.Field) */ @Test public void isHandlerMarkedAsDisabled_shouldReturnTrueIfHandlerDisabledOnMetadata() { Field persons = null; for (Field field : Reflect.getAllFields(MetadataClassWithDisableHandlersAnnotation.class)) { if (field.getName().equals("concepts")) { persons = field; } } Assert.assertTrue(RequiredDataAdvice.isHandlerMarkedAsDisabled(UnretireHandler.class, persons)); }
/** * @see Reflect#isSuperClass(Type) */ @Test public void isSuperClass_shouldReturnFalseForAGenericWhoseBoundIsNotASubclass() { Reflect reflect = new Reflect(Number.class); Field field = ReflectionUtils.findField(Visit.class, "attributes"); ParameterizedType setOfAttr = (ParameterizedType) field.getGenericType(); Type genericType = setOfAttr.getActualTypeArguments()[0]; Assert.assertFalse(reflect.isSuperClass(genericType)); }
/** * @see Reflect#isCollection(Object) */ @Test public void isCollection_shouldReturnTrueIfGivenObjectIsCollectionClass() { Assert.assertTrue(Reflect.isCollection(new ArrayList<>())); }
/** * @param field * @return true if, given field is declared in parameterized class or its sub classes */ public boolean hasField(Field field) { return isSuperClass(field.getDeclaringClass()); }
/** * @see Reflect#isCollection(Class<*>) */ @Test public void isCollection_shouldReturnFalseIfGivenFieldClassIsNotACollectionClass() { Reflect reflect = new Reflect(OpenmrsObject.class); List<Field> allFields = Reflect.getAllFields(OpenmrsObjectImp.class); Field nonCollectionField = findFieldByName(allFields, "nonCollectionField"); Assert.assertFalse(reflect.isCollectionField(nonCollectionField)); }
/** * @see RequiredDataAdvice#isHandlerMarkedAsDisabled(Class, java.lang.reflect.Field) */ @Test public void isHandlerMarkedAsDisabled_shouldReturnFalseIfHandlerNotDisabled() { Field persons = null; for (Field field : Reflect.getAllFields(ClassWithDisableHandlersAnnotation.class)) { if (field.getName().equals("persons")) { persons = field; } } Assert.assertFalse(RequiredDataAdvice.isHandlerMarkedAsDisabled(RetireHandler.class, persons)); }
/** * @see Reflect#hasField(Field) */ @Test public void xhasField_shouldReturnFalseIfGivenFieldIsNotDeclaredInParameterizedClassOrItsSubClass() { Reflect reflect = new Reflect(OpenmrsObject.class); List<Field> allFields = Reflect.getAllFields(OpenmrsObjectImp.class); Field normalClassField = findFieldByName(allFields, "normalClassField"); Assert.assertFalse(reflect.hasField(normalClassField)); }
/** * @see Reflect#isSuperClass(Class) */ @Test public void isSuperClass_shouldReturnTrueIfGivenSubClassIsAccessibleFromGivenParameterizedClass() { Reflect reflect = new Reflect(OpenmrsObject.class); Assert.assertTrue(reflect.isSuperClass(OpenmrsObjectImp.class)); }