Abusing toString()

So I’m working on a program that has some /r/Glitch_Art inspired image filters for fun, and even has a fancy little GUI. I love thinking of new algorithms to screw up images, but hate having to write GUI code for every new filter I add. My solution? Let toString() do the work by un-camel-casing and spacing the class names.

public String toString() {
    String s = this.getClass().getName();
    s = s.replace(this.getClass().getPackage().getName()+".", "");
        
    for(int i = 1; i < s.length(); i++)
        if (Character.isUpperCase(s.charAt(i))) {
            s = s.substring(0, i)+" "+s.substring(i);
            i++;
        }
    s = s.toLowerCase(); 
    return Character.toUpperCase(s.charAt(0)) + s.substring(1);    
}

The result:

Screen Shot 2016-02-11 at 4.42.10 PM

Unfortunately it seems there’s no way to dynamically get all subclasses of something in a running program, so i still have to manually add an instance of each filter to a list. What a chore. I’ll keep looking for a way to misuse reflection to do it though, and will let you know if I figure something out.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s