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(); } } 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);  ClassFieldsDefault(final Class clazz, final Boolean getInheritedFields); @Override Iterator<Field> getFields();  }
@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(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(expectedFields, actualFields.toArray()); }