Tom Schober Rotating Header Image

Implementing Orphan Finder for Your Flex App

I recently posted how to detect unused classes (orphans) in your Flex Application (Bigger, Badder, Orphan Finder) so here’s an implementation of it and a downloadable version:

Download Here: findOrphans

Here’s how you can write a script of your own to use it and save yourself a lot of typing:

#!/bin/bash FLEXHOME="/Applications/Adobe Flash Builder 4/sdks/4.1.0" PROJECT_ROOT="/path/to/your/flex/project/" BUILD_DIR="/path/to/your/flex/project/build/dir" SOURCE_DIRS="/proj1dir/src/main/flex,/proj2/src/main/flex" OUTPUT_DIR="/path/to/desired/output" SWF_PATH="/path/to/project/swf/dir/MyProject.swf" echo "Updating code..." svn update "$PROJECT_ROOT" mvn -f "$BUILD_DIR/pom.xml" clean install ./findOrphans -f "$FLEXHOME" -p $PROJECT_ROOT -l $SOURCE_DIRS -o $OUTPUT_DIR -s $SWF_PATH

Dont forget to chmod +x myProjectScript

15 Comments

  1. [...] the term “Orphan,”  but in software development, they ARE evil.  So after you run the findOrphans script I made, kill them!  Use the file created by the script called UnusedClassFiles.  It has [...]

  2. csakduk says:

    Tom,

    this sounds like a really useful script!
    I gave it a try, but it stops at “ORPHAN TEST :”…

  3. Tom Schober says:

    The script just fails silently and escapes? Or do you get an error message? Which line in the script do you think causes is to stop?

  4. [...] Orphan Finder script adds detection for this, but I had to update it because the regular expression I use to [...]

  5. Tom says:

    Looks like your code assumes folks are using the default Maven project directory structure, which is probably why csakduk sees it hang at “ORPHAN TEST”. I had the same problem, and made this change at around line 159 or so…

    for validFile in $(<validFiles) ;
    do
    pathSegment=$(expr "$validFile" : ".*$SOURCEFOLDERS\(.*\)\..*")

    where you had hard-coded src\main\flex.

    Seems to be working now. Great script!!

  6. Tom says:

    Another issue I came across is that if you’re analyzing an RSL from a library project, the classpaths are in the swfdump output in the form of com.abc.def:ClassName, so I had to make a modification to account for that. Also, had to trim a leading slash from $pathSegment.

  7. Shaun says:

    Hey this is a great starting point for me. I’ve already coded this up myself in the form of an AIR application in the past but then lost the source. I know that seems ridiculous but it happened. I was just iterating across all the source directories and keeping a list of all the file names seen, then looking through the text of each source file for any references to the list I had previously created in the first pass. Your solution of using the byte code seems like it would be way more robust since I wasn’t accounting for things like references that were only within comments. The nice thing was I was able to have it generate a CS style graph of nodes and edges showing where the references came from, but in a huge project there were so many nodes it was unmanageable in the way I had it setup anyhow (you had to drag and drop the nodes to sort them out it didn’t automatically position them in any intelligent way, I used randomness, and had it spit out a text version of the orphaned classes :). Anyhow my plan is to take your notes from this and your previous post and build them into a maven plugin I’m not sure how to deploy these outside of my companies maven repository but if you’d like it when I’m done e-mail me and I’ll e-mail it back to you to post.

  8. Shaun says:

    Hey I got it done. On a project that outputs a 4.6MB swf it produces at 100MB output from swf dump when all is said and done it takes about 2 min 30 seconds to get through the whole process on a 2.6Ghz Quad-core. My guess is grep has got to be faster, but the maven plugin sure makes it easy (also I run Windows for the most part so I’d be forced into cygwin which isn’t the greatest… or virtual box but in either case that’s added complications, the plugin will work on any system). Please Micro$oft give us a *nix terminal!!! :) So I’ve got the plugin working against the project I really needed it for and managed to get rid of 143 orphaned classes… yes I really really needed this (also allowed me to clean up the service layer albeit a more manual task). I need to abstract two things (namely the swf location and the swfdump.jar location) then I should be good to distribute shoot me an e-mail at shaun dot husain at gmail dot com (I know parses can figure that email out but I’ll make them work to spam me).

  9. Shaun says:

    One more note I forgot to include, I added a bit to call svn delete on all the files locally so at the end of the process it shows you the list and the count and asks if you’d like it to mark them for deletion, then you can run a build to be sure all is well before committing (if you don’t have or use svn you can just answer n and it’ll stop it also outputs the list to a text file so you can manually deal with it if you’d like).

  10. Thank you very much. Very, very useful!

  11. Ajay says:

    Awesome & Looking great :)

    But my application is modular app where main application swf loads different modules in side main app.

    I tried this with mentioning out put swf as main.swf and seen Unused classes which are not fair though the classes are being used in another modules.

    Can you please help me on how to fix this problem when we have sub-modules for main app.

    Ajay

  12. Tom Schober says:

    Off the top of my head, what you’d have to do in that case is include all the sources for each module and loop over the disassembler output for each module swf. I’ll think about this some more soon.

  13. Ajay says:

    Thanks Tom.

    I modified it to loop through all individual swfs and now it is working like charm.

    Awesome & Great script ,really useful:)

    Ajay

  14. Tom Schober says:

    Ajay… great to hear. I think I’ll put it up on github and allow some contributors if you’d like to join in.

Leave a Reply

StackOverflow
profile for TomSchober at Stack Overflow, Q&A for professional and enthusiast programmers
GIS
profile for TomSchober at Geographic Information Systems Stack Exchange, Q&A for cartographers, geographers and GIS professionals