From 0a0ef573f715ddc6394695fea2d70d4b9a6d3747 Mon Sep 17 00:00:00 2001 From: kiran Date: Thu, 18 Jun 2009 22:43:19 +0000 Subject: [PATCH] Methods for finding classes given a path and finding classes that implement a given interface. This stuff was mostly copied from private methods in WalkerManager, so there's some code redundancy. At some point, those calls could be replaced with these. git-svn-id: file:///humgen/gsa-scr1/gsa-engineering/svn_contents/trunk@1053 348d0f76-0448-11de-a6fe-93d51630548a --- .../sting/utils/PackageUtils.java | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100755 java/src/org/broadinstitute/sting/utils/PackageUtils.java diff --git a/java/src/org/broadinstitute/sting/utils/PackageUtils.java b/java/src/org/broadinstitute/sting/utils/PackageUtils.java new file mode 100755 index 000000000..cc99d630c --- /dev/null +++ b/java/src/org/broadinstitute/sting/utils/PackageUtils.java @@ -0,0 +1,56 @@ +package org.broadinstitute.sting.utils; + +import java.util.List; +import java.util.ArrayList; +import java.io.File; +import java.io.IOException; + +/** + * PackageUtils contains some useful methods for package introspection. + */ +public class PackageUtils { + /** + * Private constructor. No instantiating this class! + */ + private PackageUtils() {} + + /** + * Return the classes that implement the specified interface. + * + * @param iface the interface which returned classes should implement. + * @return the list of classes that implement the interface. How is that not clear by now?!!!!111one!! + */ + public static ArrayList getClassesImplementingInterface(Class iface) { + try { + final File location = JVMUtils.getLocationFor(iface); + + List potentialClasses = getClassesFromLocation(location); + ArrayList implementingClasses = new ArrayList(); + + for (Class potentialClass : potentialClasses) { + if (JVMUtils.isConcreteImplementationOf(potentialClass, iface)) { + implementingClasses.add(potentialClass); + } + } + + return implementingClasses; + } catch (IOException e) { + throw new StingException(String.format("Unable to inspect package containing '%s'", iface.getName())); + } + } + + /** + * Return a list of classes at the specified location + * @param location the location where we should start searching (as returned by JVMUtils.getLocationFor(Class)) + * @return a list of classes at this location + * @throws IOException thrown if the jar or directory cannot be inspected + */ + public static List getClassesFromLocation(File location) throws IOException { + if (location.getAbsolutePath().endsWith(".jar")) + return JVMUtils.loadInternalClassesFromJar(location); + else { + List classFileNames = PathUtils.findFilesInPath(location, "", "class", true); + return JVMUtils.loadInternalClasses(classFileNames); + } + } +}