Repository: j-easy/easy-random
Issue: "Pre-initialized" Bean Fields are nulled

When writing a dto, I often initialize collections to ensure they are never null. It appears that objenesis's approach to instantiation ends up producing an instance will null fields.Here's some code to show it. The first assert will pass, and the second will fail. Perhaps there is a perspective where this is desired behavior, but it was surprising to me, and not what I would prefer so I thought I would share.

`public class Main {
    public static void main(String[] args) {
        EnhancedRandomBuilder builder = new EnhancedRandomBuilder();
        builder.exclude(new FieldDefinition<>("myList", List.class, MyBean.class));

```
    assert new MyBean().getMyList().size() == 0;
    assert builder.build().nextObject(MyBean.class).getMyList().size() == 0;
}

public static class MyBean {
    private List<String> myList = new ArrayList<>();

    public List<String> getMyList() {
        return myList;
    }

    public void setMyList(List<String> myList) {
        this.myList = myList;
    }
}
```

}
`


Changed files:
random-beans/src/test/java/io/github/benas/randombeans/FieldExclusionTest.java, random-beans/src/main/java/io/github/benas/randombeans/ObjectFactory.java

diff --git a/random-beans/src/main/java/io/github/benas/randombeans/ObjectFactory.java b/random-beans/src/main/java/io/github/benas/randombeans/ObjectFactory.java
index 28c0b2fd..546bda9f 100644
--- a/random-beans/src/main/java/io/github/benas/randombeans/ObjectFactory.java
+++ b/random-beans/src/main/java/io/github/benas/randombeans/ObjectFactory.java
@@ -50,18 +50,24 @@ class ObjectFactory {
     private boolean scanClasspathForConcreteTypes;
 
     <T> T createInstance(final Class<T> type) {
-        T result;
         if (scanClasspathForConcreteTypes && isAbstract(type)) {
             Class<?> randomConcreteSubType = randomElementOf(getPublicConcreteSubTypesOf((type)));
             if (randomConcreteSubType == null) {
                 throw new InstantiationError("Unable to find a matching concrete subtype of type: " + type + " in the classpath");
             } else {
-                result = (T) objenesis.newInstance(randomConcreteSubType);
+                return (T) createNewInstance(randomConcreteSubType);
             }
         } else {
-            result = objenesis.newInstance(type);
+            return createNewInstance(type);
+        }
+    }
+
+    private <T> T createNewInstance(final Class<T> type) {
+        try {
+            return type.newInstance();
+        } catch (Exception exception) {
+            return objenesis.newInstance(type);
         }
-        return result;
     }
 
     Collection<?> createEmptyCollectionForType(Class<?> fieldType, int initialSize) {
diff --git a/random-beans/src/test/java/io/github/benas/randombeans/FieldExclusionTest.java b/random-beans/src/test/java/io/github/benas/randombeans/FieldExclusionTest.java
index a4e4465f..a0160ec7 100644
--- a/random-beans/src/test/java/io/github/benas/randombeans/FieldExclusionTest.java
+++ b/random-beans/src/test/java/io/github/benas/randombeans/FieldExclusionTest.java
@@ -40,6 +40,9 @@ import static io.github.benas.randombeans.FieldDefinitionBuilder.field;
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.mockito.Mockito.when;
 
+import java.util.ArrayList;
+import java.util.List;
+
 @RunWith(MockitoJUnitRunner.class)
 public class FieldExclusionTest {
 
@@ -218,4 +221,28 @@ public class FieldExclusionTest {
         assertThat(c.getB3()).isNotNull();
     }
 
+    @Test
+    public void whenFieldIsExcluded_thenItsInlineInitializationShouldBeUsedAsIs() {
+        enhancedRandom = aNewEnhancedRandomBuilder()
+            .exclude(new FieldDefinition<>("myList", List.class, InlineInitializationBean.class))
+            .build();
+
+        InlineInitializationBean bean = enhancedRandom.nextObject(InlineInitializationBean.class);
+
+        assertThat(bean).isNotNull();
+        assertThat(bean.getMyList()).isEmpty();
+    }
+
+    public static class InlineInitializationBean {
+        private List<String> myList = new ArrayList<>();
+
+        public List<String> getMyList() {
+            return myList;
+        }
+
+        public void setMyList(List<String> myList) {
+            this.myList = myList;
+        }
+    }
+
 }