anirudha-wegilant / lambdaj

Automatically exported from code.google.com/p/lambdaj
Apache License 2.0
0 stars 0 forks source link

Exception thrown if using forEach on a collection with an Interface generic #25

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
To reproduce:

public interface Animal {
 public String getNoise();
}

public class Dog {
 public String getNoise() {
  return "Woof!";
 }
}

public class Cat {
 public String getNoise() {
  return "Meeow!";
 }
}

public static void main(String[] args) throws Exception {
 Collection<Animal> animals = new ArrayList<Animal>();
 animals.add(new Dog());
 animals.add(new Cat());
 StringBuilder sb = new StringBuilder();
 sb.append(forEach(animals).getNoise());
 System.out.println(sb.toString());
}

Result:

ch.lambdaj.proxy.UnproxyableClassException: Unable to proxy the final class
[Ljava.lang.Object;

Expected:

"Woof!Meeow!"

If this is not possible, it should be documented.

Original issue reported on code.google.com by robert.e...@googlemail.com on 10 Dec 2009 at 6:15

GoogleCodeExporter commented 8 years ago
It is quite well documented that the forEach() method can't work when the Class 
that
it should return is a final one, like the String in your example.

In order to workaround this issue, some join methods have been explicitly 
provided to
work with Strings. For example, to achieve the result you wanted, you can simply
write this:

joinFrom(animals).getNoise()

I hope this helps

Original comment by mario.fu...@gmail.com on 12 Dec 2009 at 12:43