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

9Dec/094

Debugging Objects in Javascript

There are a lot of good JavaScript debugging tools available out there; FireBug for Firefox comes to mind as one of the best. But when when developing web applications across multiple browsers, particularly browsers like IE which have limited built-in debugging functionality, it's handy to have a function that can be called to debug a specific object on the page.

Here is a JavaScript function that I wrote several years ago. It's a bit of a kludge - I'm sure there is a more elegant approach - but having it available to quickly inspect a single element has saved more than a few hours of painful development:

function debugObject(elem, recurseIntoObjects, elemPropertyName) {
	var mesg = "";
	if ((elem == undefined) || (elem == null)) {
		alert("Passed element is not valid: "+elem);
		return;
	}
	for (var i in elem) {
		if ((i == undefined) || (i == null) || (typeof elem[i] == 'function') || (typeof elem[i] == 'undefined') || (elem[i] == null)) {
			continue;
		}
		if (recurseIntoObjects && typeof elem[i] == 'object') {
			if ((elemPropertyName != undefined) && (elem[i][elemPropertyName] != 'undefined')) {
				mesg += i+": "+elem[i][elemPropertyName]+"\n";
			}
			else {
				var elements = "";
				var elementCount = 0;
				for (var j in elem[i]) {
					if ((j == undefined) || (j == null) || (typeof elem[i][j] == 'function') || (typeof elem[i][j] == 'undefined') || (elem[i][j] == null)) {
						continue;
					}
					if (elementCount != 0) {
						elements += ", ";
					}
					elements += elem[i][j];
					elementCount++;
				}
				mesg += i+"["+elementCount+"]: "+elements+"\n";
			}
		}
		else {
			mesg += i+": "+elem[i]+"\n";
		}
	}
	alert(mesg);
	return mesg;
}
Filed under: dev, geek 4 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
30Aug/097

Welcome Back to a Secret Blog

Spy vs SpyToday is the beginning of a new era of privacy on this blog. Well, technically speaking it's really the re-beginning of an era... let me explain...

You see, I used to be able to create posts that were private and only viewable by logged in users. A year ago Wordpress released a new version, and while it was sparkly and wonderful it also broke my ability to do this (by breaking Fil's excellent private-post plugin "Post Levels"). So instead of exposing all of my private posts to the world I squirreled them away, hoping that someday I would be able to bring them back.

Ladies and Gentlemen: That day is today. Thanks to Peter Holiday's outstanding plugin WP Sentry (which is based on Fil's plugin), I am now able to control who can see certain posts.

Some things to know...

  1. You must have an account on this blog to see private posts. Log in here or create one.
  2. The most recent private post that currently exists is from February 28th, 2008. I will likely post more now that people can actually see them.
  3. To see ALL of my old private posts (there are 44 of them) click here.
  4. If you are logged in and still don't see any private posts please let me know - I probably didn't recognize the username or e-mail address you registered with.
Filed under: dev, geek, meta 7 Comments
5Jan/092

Removing WinFixer / Virtuamonde / Msevents / Trojan.vundo

Flu VirusI spent a couple hours over the weekend trying to figure out a particularly nasty virus called (among other things) "Virtuamonde" or "Virtumonde". What makes this trojan virus particularly nasty is that it redirects a lot of useful anti-virus and anti-spyware websites to point to localhost (127.0.0.1). The blocked websites include:

  • Windows update (windowsupdate.microsoft.com and update.microsoft.com)
  • Windows support sites (support.microsoft.com)
  • Spybot Search & Destroy website
  • AdAware website
  • ... and many other forums and support sites that provide solutions.

To help others who have this same issue and do not have the luxury of a second computer running linux, or cannot connect to these sites for other reasons, I am providing the necessary programs here to FIX the problem.

TO FIX:
Download, install, and run Malwarebytes' Anti-Malware. This was the only malware/spyware removal program that actually found and fixed the problem.

You may have to run this program in safe mode (reboot your computer and keep pressing F8 until you see a prompt for "Safe Mode"). If this still doesn't catch your problem you can download and install VundoFix.

(via bleepingcomputer, which is another site that is explicitly blocked by this trojan virus)

Filed under: dev, geek 2 Comments
3Oct/086

Website Error in IE

It has come to my attention that several readers are experiencing an error when visiting this site. The error will only occur when you are using Internet Explorer, and is most likely due to JavaScript in one of my plugins attempting to write to the DOM before it has finished rendering.

If you are tired of seeing this error, or do not wish to wait for me to fix it, then please download and install a REAL browser like Firefox, Opera, or Chrome. From a web developer's perspective, and in my professional opinion, Internet Explorer sucks donkey balls. It's a nightmare to support and a pain to debug. It doesn't conform to most standards and has abysmal JavaScript debugging tools.

So do yourself a favor and take 5 minutes to install a new browser. Your entire web experience will improve (especially if you're using IE6 - Firefox is much faster and more secure).

Filed under: dev, geek, meta 6 Comments