/** * @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()); }
/** * 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 Reflect#isCollection(Object) */ @Test public void isCollection_shouldReturnTrueIfGivenObjectIsCollectionClass() { Assert.assertTrue(Reflect.isCollection(new ArrayList<>())); }
/** * @see Reflect#isCollection(Class<*>) */ @Test public void isCollection_shouldReturnTrueIfGivenFieldClassIsCollectionClass() { Assert.assertTrue(Reflect.isCollection(ArrayList.class)); }
/** * @see Reflect#isCollectionField(Field) */ @Test public void isCollectionField_shouldReturnFalseIfGivenFieldIsNotACollection() { Assert.assertFalse(Reflect.isCollection(NormalClass.class)); }
/** * @see Reflect#isCollection(Object) */ @Test public void isCollection_shouldReturnFalseIfGivenObjectIsNotACollection() { Assert.assertFalse(Reflect.isCollection(new NormalClass())); }
else if (Reflect.isCollection(mainArgument) && isOpenmrsObjectCollection(mainArgument)) {