use of org.broadinstitute.hellbender.cmdline.ClassFinder in project gatk by broadinstitute.
the class ClassUtils method makeInstancesOfSubclasses.
/**
* Finds and creates objects of all concrete subclasses of the given class in the package.
* The public no-arg constructor is called to create the objects.
*
* GATKException is thrown if creation of any object fails.
* @param clazz class to be instantiated
* @param pack package in which the class will be searched for
*/
@SuppressWarnings("unchecked")
public static <T> List<T> makeInstancesOfSubclasses(final Class<? extends T> clazz, final Package pack) {
Utils.nonNull(clazz, "class");
Utils.nonNull(pack, "package");
final ClassFinder finder = new ClassFinder();
finder.find(pack.getName(), clazz);
final Set<Class<?>> classes = finder.getClasses();
final List<T> results = new ArrayList<>(classes.size());
for (final Class<?> found : classes) {
if (canMakeInstances(found)) {
try {
results.add((T) found.newInstance());
} catch (InstantiationException | IllegalAccessException e) {
throw new GATKException("Problem making an instance of " + found + " Do check that the class has a non-arg constructor", e);
}
}
}
return results;
}
use of org.broadinstitute.hellbender.cmdline.ClassFinder in project gatk by broadinstitute.
the class ClassUtils method knownSubInterfaces.
/**
* Finds all subinterfaces of the given interface (in the same package).
*/
public static Set<Class<?>> knownSubInterfaces(final Class<?> iface) {
final ClassFinder finder = new ClassFinder();
finder.find(iface.getPackage().getName(), iface);
return finder.getClasses().stream().filter(cl -> !cl.equals(iface) && cl.isInterface()).collect(Collectors.toSet());
}
use of org.broadinstitute.hellbender.cmdline.ClassFinder in project gatk by broadinstitute.
the class Main method extractCommandLineProgram.
/**
* Returns the command line program specified, or prints the usage and exits with exit code 1 *
*/
private static CommandLineProgram extractCommandLineProgram(final String[] args, final List<String> packageList, final List<Class<? extends CommandLineProgram>> classList, final String commandLineName) {
/** Get the set of classes that are our command line programs **/
final ClassFinder classFinder = new ClassFinder();
for (final String pkg : packageList) {
classFinder.find(pkg, CommandLineProgram.class);
}
String missingAnnotationClasses = "";
final Set<Class<?>> toCheck = classFinder.getClasses();
toCheck.addAll(classList);
final Map<String, Class<?>> simpleNameToClass = new LinkedHashMap<>();
for (final Class<?> clazz : toCheck) {
// No interfaces, synthetic, primitive, local, or abstract classes.
if (ClassUtils.canMakeInstances(clazz)) {
final CommandLineProgramProperties property = getProgramProperty(clazz);
// Check for missing annotations
if (null == property) {
if (missingAnnotationClasses.isEmpty())
missingAnnotationClasses += clazz.getSimpleName();
else
missingAnnotationClasses += ", " + clazz.getSimpleName();
} else if (!property.omitFromCommandLine()) {
/** We should check for missing annotations later **/
if (simpleNameToClass.containsKey(clazz.getSimpleName())) {
throw new RuntimeException("Simple class name collision: " + clazz.getSimpleName());
}
simpleNameToClass.put(clazz.getSimpleName(), clazz);
}
}
}
if (!missingAnnotationClasses.isEmpty()) {
throw new RuntimeException("The following classes are missing the required CommandLineProgramProperties annotation: " + missingAnnotationClasses);
}
final Set<Class<?>> classes = new LinkedHashSet<>();
classes.addAll(simpleNameToClass.values());
if (args.length < 1) {
printUsage(classes, commandLineName);
} else {
if (args[0].equals("-h") || args[0].equals("--help")) {
printUsage(classes, commandLineName);
} else {
if (simpleNameToClass.containsKey(args[0])) {
final Class<?> clazz = simpleNameToClass.get(args[0]);
try {
return (CommandLineProgram) clazz.newInstance();
} catch (final InstantiationException | IllegalAccessException e) {
throw new RuntimeException(e);
}
}
printUsage(classes, commandLineName);
throw new UserException(getUnknownCommandMessage(classes, args[0]));
}
}
return null;
}
Aggregations