@@ -77,11 +77,6 @@ public <T> T manufacture(final SpecimenType<T> type, CustomizationContext custom
7777 .orElseThrow (() -> new SpecimenException (format ("Cannot create instance of %s" , type .asClass ())));
7878 }
7979
80- private <T > boolean hasSpecimenTypeAsParameter (Method m , SpecimenType <T > type ) {
81- return stream (m .getGenericParameterTypes ())
82- .anyMatch (t -> t .getTypeName ().equals (type .asClass ().getName ()));
83- }
84-
8580 public <T > T instantiate (final SpecimenType <T > type ) {
8681 try {
8782 return type .asClass ().getDeclaredConstructor ().newInstance ();
@@ -95,21 +90,19 @@ public <T> Object proxy(final SpecimenType<T> type) {
9590 }
9691
9792 public <T > Object proxy (final SpecimenType <T > type , final Map <String , ISpecimen <?>> specimens ) {
98- if (type .isInterface ()) {
99- var proxyFactory = new ProxyFactory ();
100- proxyFactory .setInterfaces (new Class <?>[]{type .asClass ()});
101- try {
102- return proxyFactory .create (new Class [0 ], new Object [0 ], new ProxyInvocationHandler (specimenFactory , specimens ));
103- } catch (Exception e ) {
104- throw new SpecimenException (format ("Unable to construct interface %s: %s" , type .asClass (), e .getMessage ()), e );
105- }
106- }
107-
108- return createProxyForAbstract (type , specimens );
93+ return type .isInterface ()
94+ ? createProxyForInterface (type , specimens )
95+ : createProxyForAbstract (type , specimens );
10996 }
11097
11198 public <G , T extends Collection <G >> T createCollection (final SpecimenType <T > type ) {
112- return type .isInterface () ? createCollectionFromInterfaceType (type .asClass ()) : createCollectionFromConcreteType (type );
99+ return type .isInterface ()
100+ ? createCollectionFromInterfaceType (type .asClass ())
101+ : createCollectionFromConcreteType (type );
102+ }
103+
104+ private <T > boolean hasSpecimenTypeAsParameter (Method m , SpecimenType <T > type ) {
105+ return stream (m .getGenericParameterTypes ()).anyMatch (t -> t .getTypeName ().equals (type .asClass ().getName ()));
113106 }
114107
115108 private <T > T construct (final SpecimenType <T > type , final Constructor <?> constructor , CustomizationContext customizationContext ) {
@@ -124,11 +117,9 @@ private <T> T construct(final SpecimenType<T> type, final Constructor<?> constru
124117 }
125118
126119 private static Object defaultValue (Class <?> type ) {
127- if (type .isPrimitive ()) {
128- return primitiveDefaults .get (type );
129- } else {
130- return null ;
131- }
120+ return type .isPrimitive ()
121+ ? primitiveDefaults .get (type )
122+ : null ;
132123 }
133124
134125 private Object createParameter (Parameter parameter , CustomizationContext customizationContext ) {
@@ -138,8 +129,19 @@ private Object createParameter(Parameter parameter, CustomizationContext customi
138129 if (customizationContext .getCustomFields ().containsKey (parameter .getName ())) {
139130 return customizationContext .getCustomFields ().get (parameter .getName ());
140131 }
141- var specimen = specimenFactory .build (SpecimenType .fromClass (parameter .getParameterizedType ()));
142- return specimen .create (new CustomizationContext (List .of (), Map .of (), customizationContext .useRandomConstructor ()), new Annotation [0 ]);
132+ return specimenFactory
133+ .build (SpecimenType .fromClass (parameter .getParameterizedType ()))
134+ .create (new CustomizationContext (List .of (), Map .of (), customizationContext .useRandomConstructor ()), new Annotation [0 ]);
135+ }
136+
137+ private <T > Object createProxyForInterface (SpecimenType <T > type , Map <String , ISpecimen <?>> specimens ) {
138+ try {
139+ var proxyFactory = new ProxyFactory ();
140+ proxyFactory .setInterfaces (new Class <?>[]{type .asClass ()});
141+ return proxyFactory .create (new Class [0 ], new Object [0 ], new ProxyInvocationHandler (specimenFactory , specimens ));
142+ } catch (Exception e ) {
143+ throw new SpecimenException (format ("Unable to proxy interface %s: %s" , type .asClass (), e .getMessage ()), e );
144+ }
143145 }
144146
145147 private <T > T createProxyForAbstract (final SpecimenType <T > type , final Map <String , ISpecimen <?>> specimens ) {
@@ -148,7 +150,7 @@ private <T> T createProxyForAbstract(final SpecimenType<T> type, final Map<Strin
148150 factory .setSuperclass (type .asClass ());
149151 return (T ) factory .create (new Class <?>[0 ], new Object [0 ], new ProxyInvocationHandler (specimenFactory , specimens ));
150152 } catch (Exception e ) {
151- throw new SpecimenException (format ("Unable to construct abstract class %s: %s" , type .asClass (), e .getMessage ()), e );
153+ throw new SpecimenException (format ("Unable to create instance of abstract class %s: %s" , type .asClass (), e .getMessage ()), e );
152154 }
153155 }
154156
0 commit comments