/** * @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()); }
/** * @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()); }
/** * @param t * @return true if given type is a subclass, or a generic type bounded by a subclass of the * parameterized class * @should return true for a generic whose bound is a subclass * @should return false for a generic whose bound is not a subclass */ public boolean isSuperClass(Type t) { if (t instanceof TypeVariable<?>) { TypeVariable<?> typeVar = (TypeVariable<?>) t; if (typeVar.getBounds() == null || typeVar.getBounds().length == 0) { return parametrizedClass.equals(Object.class); } for (Type typeBound : typeVar.getBounds()) { if (isSuperClass(typeBound)) { return true; } } return false; } else if (t instanceof Class<?>) { return isSuperClass((Class<?>) t); } else { throw new IllegalArgumentException("Don't know how to handle: " + t.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#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#isSuperClass(Type) */ @Test public void isSuperClass_shouldReturnTrueForAGenericWhoseBoundIsASubclass() { Reflect reflect = new Reflect(OpenmrsObject.class); Field field = ReflectionUtils.findField(Visit.class, "attributes"); ParameterizedType setOfAttr = (ParameterizedType) field.getGenericType(); Type genericType = setOfAttr.getActualTypeArguments()[0]; Assert.assertTrue(reflect.isSuperClass(genericType)); }
/** * @see Reflect#isSuperClass(Object) */ @Test public void isSuperClass_shouldReturnFalseIfGivenObjectIsNotAccessibleFromGivenParameterizedClass() { Reflect reflect = new Reflect(OpenmrsObject.class); Assert.assertFalse(reflect.isSuperClass(NormalClass.class)); }
/** * @see Reflect#isSuperClass(Class) */ @Test public void isSuperClass_shouldReturnTrueIfGivenSubClassIsAccessibleFromGivenParameterizedClass() { Reflect reflect = new Reflect(OpenmrsObject.class); Assert.assertTrue(reflect.isSuperClass(OpenmrsObjectImp.class)); }
/** * @see Reflect#isSuperClass(Class) */ @Test public void isSuperClass_shouldReturnFalseIfGivenSubClassIsNotAccessibleFromGivenParameterizedClass() { Reflect reflect = new Reflect(OpenmrsObject.class); Assert.assertFalse(reflect.isSuperClass(new NormalClass())); }
if (reflect.isSuperClass(mainArgument)) {
/** * @see Reflect#isSuperClass(Object) */ @Test public void isSuperClass_shouldReturnTrueIfGivenObjectIsAccessibleFromGivenParameterizedClass() { Reflect reflect = new Reflect(OpenmrsObject.class); Assert.assertTrue(reflect.isSuperClass(new OpenmrsObjectImp())); }