title: Workaround for Java Collections wrappers breaking reflection
body: <p>Today, I discovered that using Collections.synchronizedXXX doesn't play well with reflection.</p>
  
  <p>Here's a simple example:</p>
  
  <pre><code>import java.util.ArrayList;
  import java.util.Collections;
  import java.util.List;
  
  public class Weird{
    public static void main(String[] args) {
      List&lt;String&gt; list = new ArrayList&lt;String&gt;();
      list.add("Hello World");
  
      List&lt;String&gt; wrappedList = Collections.synchronizedList(list);
  
      printSizeUsingReflection(list);
      printSizeUsingReflection(wrappedList);
    }
  
    private static void printSizeUsingReflection(List&lt;String&gt; list) {
      try {
        System.out.println(
            "size = " + list.getClass().getMethod("size").invoke(list));
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
  }
  </code></pre>
  
  <p>The first call to printSizeUsingReflection prints the size (i.e. "1"), the second call results in:</p>
  
  <pre><code>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)
  </code></pre>
  
  <p>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().</p>