Search in sources :

Example 1 with ClassFinder

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;
}
Also used : ClassFinder(org.broadinstitute.hellbender.cmdline.ClassFinder) ArrayList(java.util.ArrayList) GATKException(org.broadinstitute.hellbender.exceptions.GATKException)

Example 2 with ClassFinder

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());
}
Also used : ClassFinder(org.broadinstitute.hellbender.cmdline.ClassFinder) List(java.util.List) Modifier(java.lang.reflect.Modifier) Set(java.util.Set) GATKException(org.broadinstitute.hellbender.exceptions.GATKException) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) ClassFinder(org.broadinstitute.hellbender.cmdline.ClassFinder)

Example 3 with ClassFinder

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;
}
Also used : CommandLineProgram(org.broadinstitute.hellbender.cmdline.CommandLineProgram) CommandLineProgramProperties(org.broadinstitute.barclay.argparser.CommandLineProgramProperties) ClassFinder(org.broadinstitute.hellbender.cmdline.ClassFinder) UserException(org.broadinstitute.hellbender.exceptions.UserException)

Aggregations

ClassFinder (org.broadinstitute.hellbender.cmdline.ClassFinder)3 ArrayList (java.util.ArrayList)2 GATKException (org.broadinstitute.hellbender.exceptions.GATKException)2 Modifier (java.lang.reflect.Modifier)1 List (java.util.List)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 CommandLineProgramProperties (org.broadinstitute.barclay.argparser.CommandLineProgramProperties)1 CommandLineProgram (org.broadinstitute.hellbender.cmdline.CommandLineProgram)1 UserException (org.broadinstitute.hellbender.exceptions.UserException)1