List<Gen<?>> assignableGenerators = null;
try {
classes = ClassSearcher
.getAssignableClasses(this.getSubjectClass());
} catch (Exception e) {
throw new GenerationError(e);
}
if (classes != null && !classes.isEmpty()) {
// search declared generators for assignable classes
assignableGenerators = new ArrayList<Gen<?>>();
for (Class<?> assignableClass : classes) {
// pick a random generator for the assignable (non-abstract)
// class
if (!Modifier.isAbstract(assignableClass.getModifiers())) {
Set<Gen<?>> set = Arbitrary
.getGeneratorsOf(assignableClass);
if (!set.isEmpty()) {
assignableGenerators.addAll(set);
}
}
}
if (assignableGenerators.size() > 0) {
int index = (int) Arbitrary.choose(random, 0,
assignableGenerators.size() - 1);
return (T) assignableGenerators.get(index).arbitrary(random,
minsize, maxsize);
} else { // creates a constructor based generator for any
// non-abstract class
// List<Class<?>> concreteSubclasses = new
// ArrayList<Class<?>>();
// for (Class<?> clazz : classes) {
// if (clazz.isAnnotation() || clazz.isEnum()
// || clazz.isArray() || clazz.isPrimitive()
// || !Modifier.isAbstract(clazz.getModifiers())
// && clazz.getConstructors().length > 0) {
// concreteSubclasses.add(clazz);
// }
// }
// if (concreteSubclasses.size() > 0) {
// new ConstructorBasedGenerator(concreteSubclasses
// .get((int) Arbitrary.choose(random, 0,
// concreteSubclasses.size()))).arbitrary(
// random, minsize, maxsize);
// } else {
throw new GenerationError(
"No generators available for classes in the class hierarchy");
// }
}
} else {
throw new GenerationError(
"No subclasses defined for this class");
}
}