tags:

views:

124

answers:

2

I'm using Guava-05-snapshot, with Sun's JDK 1.6 The code blows up executing this snippet:

List<String> badpasswords = Lists.newArrayList( Password.badWords);
Collections.sort(badpasswords);
ImmutableList<String> tmp = ImmutableList.copyOf(badpasswords);

Specifically on the ImmutableList.copyOf() call. This code has worked for months, using the old Google-Collections code.

java.lang.NoSuchMethodError: com.google.common.collect.ImmutableList.copyOf(Ljava/util/Collection;)Lcom/google/common/collect/ImmutableList;

The Password.badWords is an ImmutableSet<String> and the creation of the writable array and the sort work perfectly. But attempts to convert the Array into an ImmutableList fail.

A: 

This also works fine for me using the official (non-snapshot) guava-r05 release from Maven. Incidentally, this might be a little nicer way of doing the same thing:

ImmutableList<String> sorted = Ordering.natural()
    .immutableSortedCopy(Password.badWords);
ColinD
I like that version. Thanks
fishtoprecords
+7  A: 

Guava is a fully compatible superset of Google Collections -- we did not change anything in an incompatible way. (This is tested by running the entire Google Collections test suite (which is extensive) against the lastest guava jar.)

I believe you have a copy of google-collect-*.jar still makings its way into your classpath. Either explicitly, or because some other jar included it without repackaging it. You just have to find it and remove it.

In Google Collections, there was an ImmutableList.copyOf(Iterable) method, and there was no public ImmutableList.copyOf(Collection) method. Which is fine, because a collection is also an iterable. In Guava, we've added the Collection overload. This is completely compatible, as all source that used to compile still can, and any source previously compiled will simply still reference the original method.

The problem comes in if you compile against Guava but then run against Google Collections. I believe that is likely what is happening.

Kevin Bourrillion
The problem was a side effect of Netbeans keeping too many things in caches. A "clean and build" did nothing to fix things. I did a complete clean and build down into every jar in the project.The solution was to exit Netbeans, go to ~/netbeans/6.7/var/cache and do a rm -rf *
fishtoprecords