fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.reflect.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public abstract static class ArrayListGeneric1<T> {
  11. private int capacity = 1;
  12. private int size = 0;
  13. T[] array = (T[]) Array.newInstance(getElementType(), capacity);
  14.  
  15. public ArrayListGeneric1() {}
  16.  
  17. Class<?> getElementType() {
  18. ParameterizedType pt = (ParameterizedType) getClass().getGenericSuperclass();
  19. Type[] typeArgs = pt.getActualTypeArguments();
  20.  
  21. // Will fail if it's not a class type.
  22. return (Class<?>) typeArgs[0];
  23. }
  24. }
  25.  
  26. public static void main (String[] args) throws java.lang.Exception
  27. {
  28. System.out.println(new ArrayListGeneric1<String[]>() {}.array.getClass());
  29. System.out.println(new ArrayListGeneric1<Integer>() {}.array.getClass());
  30. }
  31.  
  32. }
Success #stdin #stdout 0.09s 32992KB
stdin
Standard input is empty
stdout
class [[Ljava.lang.String;
class [Ljava.lang.Integer;