Today, I discovered that using Collections.synchronizedXXX doesn't play well with reflection.
Here's a simple example:
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Weird{
public static void main(String[] args) {
List<String> list = new ArrayList<String>();
list.add("Hello World");
List<String> wrappedList = Collections.synchronizedList(list);
printSizeUsingReflection(list);
printSizeUsingReflection(wrappedList);
}
private static void printSizeUsingReflection(List<String> list) {
try {
System.out.println(
"size = " + list.getClass().getMethod("size").invoke(list));
} catch (Exception e) {
e.printStackTrace();
}
}
}
The first call to printSizeUsingReflection prints the size (i.e. "1"), the second call results in:
java.lang.IllegalAccessException: Class Weird can not access a member of class
java.util.Collections$SynchronizedCollection with modifiers "public"
at sun.reflect.Reflection.ensureMemberAccess(Reflection.java:65)
at java.lang.reflect.Method.invoke(Method.java:588)
at Weird.printSizeUsingReflection(Weird.java:18)
at Weird.main(Weird.java:13)
This is a little surprising and annoying. Is there a good workaround? I know there is a thread-safe List implementation in java.util.concurrent, but that implementation seems slower than using Collections.synchronizedList().