Barron of Blog Wife, Kids, and the Pursuit of Happiness

5Sep/090

(Gradual) Change To Believe In

Darwin: Gradual Change We Can Believe In
Or is it punctuated equilibrium we can believe in?

Filed under: funny, science No Comments
4Sep/090

Maine Golf

One morning in Maine several of us decided to go golfing. We arrived before the course opened and played for 3.5 hours as the sun cleared away the dew. I hope this starts a tradition - it was very nice walking the greens with some of the Barron men:

Filed under: family, sports, travel No Comments
3Sep/090

The Men Who Stare at Goats

An upcoming movie, called "The Men Who Stare at Goats", caught my eye the other day. It stars Ewan McGregor, George Clooney, Kevin Spacey, and Jeff "The Dude" Bridges... so at least their casting director knows how to pick a righteous cast.

Here's the trailer:

Filed under: cinema, videos No Comments
2Sep/095

Java: Reflecting to Get All Classes in a Package

It took me a little while to figure out that Java doesn't provide a way to reflect an entire package. In other words, there is no built-in way for me to dynamically retrieve a list of all the classes in a given package in Java through reflection. So I wrote my own method to do it.

Since it took a bit of googling and effort, I thought it would be nice to share the convenience method I wrote with the world. Enjoy:

/**
 * Given a package name, attempts to reflect to find all classes within the package
 * on the local file system.
 *
 * @param packageName
 * @return
 */
private static Set<Class> getClassesInPackage(String packageName) {
	Set<Class> classes = new HashSet<Class>();
	String packageNameSlashed = "/" + packageName.replace(".", "/")
	// Get a File object for the package
	URL directoryURL = Thread.currentThread().getContextClassLoader().getResource(packageNameSlashed);
	if (directoryURL == null) {
		LOG.warn("Could not retrieve URL resource: " + packageNameSlashed);
		return classes;
	}

	String directoryString = directoryURL.getFile();
	if (directoryString == null) {
		LOG.warn("Could not find directory for URL resource: " + packageNameSlashed);
		return classes;
	}

	File directory = new File(directoryString);
	if (directory.exists()) {
		// Get the list of the files contained in the package
		String[] files = directory.list();
		for (String fileName : files) {
			// We are only interested in .class files
			if (fileName.endsWith(".class")) {
				// Remove the .class extension
				fileName = fileName.substring(0, fileName.length() - 6);
				try {
					classes.add(Class.forName(packageName + "." + fileName));
				} catch (ClassNotFoundException e) {
					LOG.warn(packageName + "." + fileName + " does not appear to be a valid class.", e);
				}
			}
		}
	} else {
		LOG.warn(packageName + " does not appear to exist as a valid package on the file system.");
	}
	return classes;
}
Filed under: dev, geek 5 Comments
2Sep/090

Cadillac Mountain

After Bar Harbor we headed to the gorgeous Cadillac Mountain. A short rainstorm had just rolled through, which made the clouds beautiful and the weather windy.

Filed under: family, travel No Comments