/** * 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; }
/** * @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 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 RequiredDataAdvice#isHandlerMarkedAsDisabled(Class, java.lang.reflect.Field) */ @Test public void isHandlerMarkedAsDisabled_shouldReturnFalseIfHandlerNotDisabledOnMetatdata() { Field persons = null; for (Field field : Reflect.getAllFields(MetadataClassWithDisableHandlersAnnotation.class)) { if (field.getName().equals("concepts")) { persons = field; } } Assert.assertFalse(RequiredDataAdvice.isHandlerMarkedAsDisabled(RetireHandler.class, persons)); }
/** * @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)); }
fieldsToExclude.addAll(Arrays.asList(otherfieldsToExclude)); List<Field> fields = Reflect.getAllFields(original.getClass()); for (Field field : fields) { if (fieldsToExclude.contains(field.getName())) {
private Obs createObs(Integer id) throws Exception { Obs obs = new Obs(id); List<Field> fields = Reflect.getAllFields(Obs.class); for (Field field : fields) { if (IGNORED_FIELDS.contains(field.getName())) { continue; } setFieldValue(obs, field, false); } assertFalse(obs.isDirty()); return obs; }
/** * @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))); }
/** * @see RequiredDataAdvice#isHandlerMarkedAsDisabled(Class, java.lang.reflect.Field) */ @Test public void isHandlerMarkedAsDisabled_shouldReturnTrueIfHandlerDisabled() { Field persons = null; for (Field field : Reflect.getAllFields(ClassWithDisableHandlersAnnotation.class)) { if (field.getName().equals("persons")) { persons = field; } } Assert.assertTrue(RequiredDataAdvice.isHandlerMarkedAsDisabled(SaveHandler.class, persons)); Assert.assertTrue(RequiredDataAdvice.isHandlerMarkedAsDisabled(VoidHandler.class, persons)); }
/** * @see Reflect#getAllFields(Class<*>) */ @Test public void getAllFields_shouldReturnAllFieldsIncludePrivateAndSuperClasses() { List<Field> allFields = Reflect.getAllFields(OpenmrsObjectImp.class); findFieldByName(allFields, "subClassField"); findFieldByName(allFields, "normalClassField"); findFieldByName(allFields, "nonCollectionField"); findFieldByName(allFields, "genericCollectionField"); }
/** * @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 Reflect#isCollectionField(Field) */ @Test public void isCollectionField_shouldReturnFalseIfGivenFieldIsCollectionAndElementTypeIsOtherThanGiven() { Reflect reflect = new Reflect(OpenmrsObject.class); List<Field> allFields = Reflect.getAllFields(OpenmrsObjectImp.class); Field genericCollectionField = findFieldByName(allFields, "genericCollectionField"); Assert.assertFalse(reflect.isCollectionField(genericCollectionField)); }
/** * @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)); }
/** * @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)); }
private void updateImmutableFieldsAndAssert(Obs obs, boolean assertion) throws Exception { //Set all fields to some random values via reflection List<Field> fields = Reflect.getAllFields(Obs.class); final Integer originalPersonId = obs.getPersonId(); //call each setter and check that dirty has been set to true for each for (Field field : fields) { String fieldName = field.getName(); if (IGNORED_FIELDS.contains(fieldName)) { continue; } if ("personId".equals(fieldName)) { //call setPersonId because it is protected so BeanUtils.setProperty won't work obs.setPersonId((Integer) generateValue(field, true)); } else { BeanUtils.setProperty(obs, fieldName, generateValue(field, true)); } assertEquals("Obs was not marked as dirty after changing: " + fieldName, obs.isDirty(), assertion); if ("person".equals(fieldName)) { //Because setPerson updates the personId we need to reset personId to its original value //that matches that of person otherwise the test will fail for the personId field obs.setPersonId(originalPersonId); } //reset for next field resetObs(obs); } }
/** * @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)); }
/** * @see RequiredDataAdvice#isHandlerMarkedAsDisabled(Class, java.lang.reflect.Field) */ @Test public void isHandlerMarkedAsDisabled_shouldReturnTrueIfSupertypeHandlerDisabled() { Field persons = null; for (Field field : Reflect.getAllFields(ClassWithDisableHandlersAnnotationForSupertype.class)) { if (field.getName().equals("persons")) { persons = field; } } // all the handlers should be marked as disabled, since the supertype (RequiredDataHandler) was specified to be ignored Assert.assertTrue(RequiredDataAdvice.isHandlerMarkedAsDisabled(SaveHandler.class, persons)); Assert.assertTrue(RequiredDataAdvice.isHandlerMarkedAsDisabled(VoidHandler.class, persons)); Assert.assertTrue(RequiredDataAdvice.isHandlerMarkedAsDisabled(UnvoidHandler.class, persons)); Assert.assertTrue(RequiredDataAdvice.isHandlerMarkedAsDisabled(RetireHandler.class, persons)); Assert.assertTrue(RequiredDataAdvice.isHandlerMarkedAsDisabled(UnretireHandler.class, persons)); }
/** * Convenience method that checks if the specified property is included among the allowed * missing properties of the given resource class via reflection * * @param clazz * @param fieldName * @return * @throws IllegalArgumentException * @throws IllegalAccessException * @throws InstantiationException */ @SuppressWarnings("rawtypes") private static boolean isallowedMissingProperty(Class resourceClass, String propName) throws IllegalArgumentException, IllegalAccessException, InstantiationException { List<Field> fields = Reflect.getAllFields(resourceClass); if (CollectionUtils.isNotEmpty(fields)) { for (Field field : fields) { if (field.getName().equals("allowedMissingProperties")) return ((Set) field.get(resourceClass.newInstance())).contains(propName); } } return false; }