• Keep in touch:
  • Linked In
  • Twitter
  • RSS

Finding types of objects?

In Version 2 of Transform SWF finding objects of a given type, e.g., dynamic text fields was easy. All you had to do was check the type used to encode the object when it was written to a file:

for (Iterator iter = array.iterator(); iter.hasNext();) {
	FSMovieObject object = (FSMovieObject)iter.next();

    if (object.getType() == FSMovieObject.DefineTextField) {
        ...
	}
}

For Version 3 (and JDK 5+) the instanceof operator is way more efficient than it was for JDK 1.4 so the decision was taken to remove the getType() method to create a cleaner model of the classes and avoid exposing low-level details that concerned how the objects were encoded as binary data. The result is that testing for a given type has two options: use instanceof or compare the names of the corresponding Class objects.

Using instanceof is trivially simple, particularly with the enhanced for loop syntax added in JDK 5.0.

for (MovieTag object : tags) {
    if (object instanceof ShowFrame) {
        list.add(object);
    }
}

Searching for objects by class name is also syntactically simple but there is a significant performance impact by having to get the Class object for every object in the list and then comparing the names.

final String className = "ShowFrame";
for (MovieTag object : tags) {
    if (object.getClass().getSimpleName().equals(className)) {
        list.add(object);
    }
}

You could simply use the latter, class name version in a single method to allow you to search through Movie and DefineMovieClip objects for any type of object (you would still need other methods to search line styles, fill styles, filters, etc.) but the effort in writing code for specific searches by using instance of is not significant and the performance should be comparable to the old style checks of getType() in version 2.0.