|
13 | 13 | import com.github.nylle.javafixture.specimen.SpecialSpecimen; |
14 | 14 | import com.github.nylle.javafixture.specimen.TimeSpecimen; |
15 | 15 |
|
16 | | -import io.github.classgraph.ClassGraph; |
17 | | -import io.github.classgraph.ClassInfo; |
18 | | -import io.github.classgraph.ScanResult; |
19 | | - |
20 | | -import java.lang.reflect.Type; |
21 | | -import java.util.Random; |
22 | | -import java.util.stream.Collectors; |
23 | | - |
24 | 16 | public class SpecimenFactory { |
25 | 17 |
|
26 | 18 | private final Context context; |
@@ -102,72 +94,19 @@ public <T> ISpecimen<T> build(final SpecimenType<T> type) { |
102 | 94 | } |
103 | 95 |
|
104 | 96 | private <T> ISpecimen<T> implementationOrProxy(final SpecimenType<T> interfaceType) { |
105 | | - try (ScanResult scanResult = new ClassGraph().enableAllInfo().scan()) { |
106 | | - var implementingClasses = scanResult.getClassesImplementing(interfaceType.asClass()).stream() |
107 | | - .filter(x -> isNotParametrized(x) || interfaceType.isParameterized()) |
108 | | - .filter(x -> isNotParametrized(x) || typeParametersMatch(x, interfaceType)) |
109 | | - .collect(Collectors.toList()); |
110 | | - |
111 | | - if (implementingClasses.isEmpty()) { |
112 | | - return new InterfaceSpecimen<>(interfaceType, context, this); |
113 | | - } |
114 | | - |
115 | | - var implementingClass = implementingClasses.get(new Random().nextInt(implementingClasses.size())); |
116 | | - if (isNotParametrized(implementingClass)) { |
117 | | - return new ObjectSpecimen<>(SpecimenType.fromClass(implementingClass.loadClass()), context, this); |
118 | | - } |
119 | | - |
120 | | - return new GenericSpecimen<>( |
121 | | - SpecimenType.fromRawType(implementingClass.loadClass(), resolveTypeArguments(interfaceType, implementingClass)), |
122 | | - context, |
123 | | - this); |
124 | | - } catch (Exception ex) { |
125 | | - return new InterfaceSpecimen<>(interfaceType, context, this); |
126 | | - } |
| 97 | + return new ClassPathScanner().findRandomClassFor(interfaceType) |
| 98 | + .map(x -> x.isParameterized() |
| 99 | + ? new GenericSpecimen<>(x, context, this) |
| 100 | + : new ObjectSpecimen<>(x, context, this)) |
| 101 | + .orElseGet(() -> new InterfaceSpecimen<>(interfaceType, context, this)); |
127 | 102 | } |
128 | 103 |
|
129 | 104 | private <T> ISpecimen<T> subClassOrProxy(final SpecimenType<T> abstractType) { |
130 | | - try (ScanResult scanResult = new ClassGraph().enableAllInfo().scan()) { |
131 | | - var subClasses = scanResult.getSubclasses(abstractType.asClass()).stream() |
132 | | - .filter(x -> !x.isAbstract()) |
133 | | - .filter(x -> isNotParametrized(x) || abstractType.isParameterized()) |
134 | | - .filter(x -> isNotParametrized(x) || typeParametersMatch(x, abstractType)) |
135 | | - .collect(Collectors.toList()); |
136 | | - |
137 | | - if (subClasses.isEmpty()) { |
138 | | - return new AbstractSpecimen<>(abstractType, context, this); |
139 | | - } |
140 | | - |
141 | | - var implementingClass = subClasses.get(new Random().nextInt(subClasses.size())); |
142 | | - if (isNotParametrized(implementingClass)) { |
143 | | - return new ObjectSpecimen<>(SpecimenType.fromClass(implementingClass.loadClass()), context, this); |
144 | | - } |
145 | | - |
146 | | - return new GenericSpecimen<>( |
147 | | - SpecimenType.fromRawType(implementingClass.loadClass(), resolveTypeArguments(abstractType, implementingClass)), |
148 | | - context, |
149 | | - this); |
150 | | - } catch (Exception ex) { |
151 | | - return new AbstractSpecimen<>(abstractType, context, this); |
152 | | - } |
153 | | - } |
154 | | - |
155 | | - private static boolean isNotParametrized(ClassInfo classInfo) { |
156 | | - return classInfo.getTypeSignature() == null || classInfo.getTypeSignature().getTypeParameters().isEmpty(); |
157 | | - } |
158 | | - |
159 | | - private static <T> boolean typeParametersMatch(ClassInfo implementingClass, SpecimenType<T> genericType) { |
160 | | - return resolveTypeArguments(genericType, implementingClass).length >= implementingClass.getTypeSignature().getTypeParameters().size(); |
161 | | - } |
162 | | - |
163 | | - private static <T> Type[] resolveTypeArguments(SpecimenType<T> genericType, ClassInfo implementingClass) { |
164 | | - var typeParameters = genericType.getTypeParameterNamesAndTypes(x -> x); |
165 | | - |
166 | | - return implementingClass.getTypeSignature().getTypeParameters().stream() |
167 | | - .map(x -> typeParameters.getOrDefault(x.getName(), null)) |
168 | | - .filter(x -> x != null) |
169 | | - .map(x -> x.asClass()) |
170 | | - .toArray(size -> new Type[size]); |
| 105 | + return new ClassPathScanner().findRandomClassFor(abstractType) |
| 106 | + .map(x -> x.isParameterized() |
| 107 | + ? new GenericSpecimen<>(x, context, this) |
| 108 | + : new ObjectSpecimen<>(x, context, this)) |
| 109 | + .orElseGet(() -> new AbstractSpecimen<>(abstractType, context, this)); |
171 | 110 | } |
172 | 111 | } |
173 | 112 |
|
0 commit comments