ClassFieldsDefault implements ClassFields { @Override public Iterator<Field> getFields() { if (declaredFields == null) { if (getInheritedFields) { declaredFields = getAllFields(clazz).toArray(new Field[getAllFields(clazz).size()]); } else { declaredFields = clazz.getDeclaredFields(); } } final Iterator<Field> fields = Arrays.stream(declaredFields) .filter(field -> !field.getName().equals("serialVersionUID")) .filter(field -> !field.getName().equals("this$0")) .iterator(); return fields; } ClassFieldsDefault(final Class clazz, final Boolean getInheritedFields);  ClassFieldsDefault(final Class clazz); @Override Iterator<Field> getFields(); @Override Class getClazz();  }
@Test public void getFields() throws Exception { final String[] expectedFields = {"id", "label"}; final ClassFields classFields = new ClassFieldsDefault(TestClass.class); final Iterator<Field> fields = classFields.getFields(); final List<String> actualFields = new ArrayList<>(); while (fields.hasNext()) { final Field f = fields.next(); actualFields.add(f.getName()); } Assert.assertArrayEquals("Check declared fields", expectedFields, actualFields.toArray()); }
@Test public void getInheritedFields() throws Exception { final String[] expectedFields = {"id", "label", "valid"}; final ClassFields classFields = new ClassFieldsDefault(TestClassImproved.class, true); final Iterator<Field> fields = classFields.getFields(); final List<String> actualFields = new ArrayList<>(); while (fields.hasNext()) { final Field f = fields.next(); actualFields.add(f.getName()); } Assert.assertArrayEquals("Check declared & inherited fields", expectedFields, actualFields.toArray()); }