Community
Participate
Working Groups
CollectionAnnotator must use WeakHashMap instead of HashMap<WeakReference, Object> to simplify interface and reduce memory usage.
Fixed in the SVN
It's not so simple, I'm afraid. After debugging for two hours the test that started failing since this morning, I've found this paragraph in the WeakHashMap javadoc: "This class is intended primarily for use with key objects whose equals methods test for object identity using the == operator. Once such a key is discarded it can never be recreated, so it is impossible to do a lookup of that key in a WeakHashMap at some later time and be surprised that its entry has been removed. This class will work perfectly well with key objects whose equals methods are not based upon object identity, such as String instances. With such recreatable key objects, however, the automatic removal of WeakHashMap entries whose keys have been discarded may prove to be confusing." The problem is that ArrayList (e.g. EolSequence) uses AbstractList's equals() method, which is based on content. For this reason, we will lose whatever annotations we had when we add an element to a collection. We will probably need to wrap the objects in a type whose equals() method uses object identity, I'm afraid. I'll try this and report back in a while.
I'm thinking of several options: - using a wrapper doesn't really work, as it'd be freed automatically (nobody holds a strong reference to the wrapper) - redefining equals() in Eol* can break existing code We could define our own equivalent of WeakHashMap which ignores the equals() method of the keys, but that sounds pretty difficult :-/. I'll keep on looking.
There are alternative maps which do have the desired semantics, according to this Stack Exchange article: http://stackoverflow.com/questions/1709965/java-implementation-of-notification-provider-vs-hashcode-driven-map In particular, Apache Commons has the ReferenceIdentityMap: http://commons.apache.org/collections/api-release/org/apache/commons/collections/map/ReferenceIdentityMap.html And Google Collections has its MapMaker: http://google-collections.googlecode.com/svn/trunk/javadoc/index.html?com/google/common/collect/MapMaker.html Both are under the Apache License 2.0. The Apache Commons class seems to be the easiest to use. I think there would be no legal issues if we included the Apache Commons Collections .jar as a dependency. Galileo has a bunch of Apache License third-party dependencies itself: http://www.eclipse.org/eclipse/development/project-log-files/eclipse_project_3_5_log.html What do you think?
The issue seems to have been resolved with the ReferenceIdentityMap from Apache Commons. Here is a sequence of patches: the first one adds Apache Commons as a dependency, and the next one changes CollectionAnnotator to use ReferenceIdentityMap. The first patch may probably not apply correctly if you're not using Git: just create a project from the latest JAR and add it to the dependencies and the eol.engine projects: http://mirror.fubra.com/ftp.apache.org//commons/collections/binaries/commons-collections-3.2.1-bin.tar.gz The second patch should apply cleanly, though.
Fixed in 0.9.1