for (Iterator i=resource.getClassifiers().iterator(); i.hasNext();) {
    JavaClass curr = (JavaClass) i.next();
    // do something
}

This will iterate through all top-level classes in a given source file (in most cases there is only one top-level class).

Source: NetBeans FAQ