FLiFnotes

January 23, 2010

Using Reflection in Java

Filed under: Coding — Tags: , — Deborah Hawkins @ 1:15 pm

Last semester, my two main programming courses were CS 315 and CS 310 where we learned to use Lisp and LC-3 assembly code respectively. In both, programs and data were of the same format, so modifying a program was as simple as modifying data. In Java, however, it’s a bit more awkward. Sure, you can treat a .java file as text and modify it, but what if you only have the .class file? Or what if you just want an easier way? Fortunately, Java provides a solution called reflection.

In our Software Design course, our first assignment of the semester is to use reflection to create Java programs called adaptors. I’ve just started with the assignment by playing around with the Java reflection library and figuring out how to use these tools, but I thought I’d share a simple example which may be much quicker to understand than the article linked to above.

import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

public class Main {
    public static void main(String[] args) { 

        // select the class to look into
        Class myclass = String.class;

        // check how many methods the class declares
        Method[] methods = myclass.getDeclaredMethods();
        System.out.println( "Number of methods in "
                + myclass.getName() + " class: " + methods.length + "\n" );

        // select a method from the class
        int methodNum = 52;
        Method mymethod = methods[methodNum];
        System.out.println( "Checking method #" + methodNum + ":" );

        // name of method
        System.out.println( "Name: " + mymethod.getName() );

        // modifiers such as protected or static
        // note: getModifiers() returns an int that you need the Modifier
        //       class to makes sense of
        System.out.println( "Modifiers: "
                + Modifier.toString(mymethod.getModifiers()) );

        // return type of method
        System.out.println( "Return Type: " + mymethod.getReturnType().getName() );

        // list of method's parameters
        Class[] mytypes = mymethod.getParameterTypes();
        for (Class c: mytypes)
            System.out.println( "Parameter: " + c.getName() );
    }
}

The output for this program is:

Number of methods in java.lang.String class: 68

Checking method #52:
Name: substring
Modifiers: public
Return Type: java.lang.String
Parameter: int
Parameter: int

January 22, 2010

How do you design a favicon?

Filed under: RandoMe — Tags: , — Deborah Hawkins @ 5:38 pm

From all the classes I’ve taken in college, Mike Scott and Don Batory are two of my favorite professors. Aside of being well-prepared for lectures and such, the small details really do matter. The favicons on their websites for CS 307 and CS 378 are fantastic. Whenever I’m scrounging through my bookmarks or just looking among a dozen open tabs, having a quickly recognizable image helps me find just what I’m looking for right away.

A few years ago, I had fun using Gimp for one of the first times in creating the favicon for Chavez360. It was just a smiley face with a red headband. The favicon was very simple, but it symbolized something easily recognizable to any Chavez fan.

This blog is a whole different story, though. Even after almost a year of occasional posts, I don’t have a main focus so a true symbol is out of the question. However, after putting it off for so long, I had to come up with something. Fortunately, I have a short word in the title and used a online favicon generator to come up with something reasonable. Then not knowing what else to put in all the blank space left over, I added in some symmetrical symbols. Adding it to WordPress was simple.

No, it’s not the best favicon in the world, but it sure beats the default icon that Firefox provides so now when I visit, it feels just a little more like home.

January 8, 2010

Major Oops

Filed under: Uncategorized — Tags: , , , — Deborah Hawkins @ 11:37 pm

Yesterday I discovered the hard way that I had let my domain registration lapse. My credit card number changed a few months ago so the registration couldn’t be renewed, and my contact info was old as well.

When I went to my site to post a few article updates, it was gone. Just a generic landing page with annoying ads instead of the fansite that I had toiled over for so long.

I immediately went to the GoDaddy site to renew my domain as well as my webhosting, which I discovered had also expired. As I was scrambling to type in various numbers, I was thinking to myself: When was the last time I backed up the database? Are all the files on my local machine the same as those that were online when my site was last up? So this is what all those best practices are really about.

Fortunately, shortly after my payment went through, everything was back to normal. All the files, the databases–it was all saved even though GoDaddy could’ve gotten rid of it all if they wanted to. I just have to thank them for making it so easy to get it all back.

Then I went straight to the forum to post an apology to any visitors who had seen that awful page. By evening, there was a response from someone who was afraid I had just given up on the site and was thankful it was back up.

Yep, that’s what makes it all worthwhile.

Powered by WordPress