There is an object

ArrayList<String> list = new ArrayList<>(); 

I want to get the type of this object in this form

 java.util.ArrayList<java.lang.String> 

But using the method .getClass I get the type in this form:

 java.util.ArrayList 

How can I get the type of object in the right format for me

This is the code I did not work.

  private void invoke(String arg1, String arg2, Object dataObject) { Method[] methods = MyClass.class.getDeclaredMethods(); for (Method m : methods) { if (m.getName().equals("invoke")) { Type[] methodTypes = m.getGenericParameterTypes(); for (int i = 0; i < methodTypes.length; i++) { Log.wtf(TAG, "Check class: " + methodTypes[i]); if (methodTypes[i] instanceof ParameterizedType) { ParameterizedType t = (ParameterizedType) methodTypes[i]; Class<?> cls = (Class<?>) t.getActualTypeArguments()[0]; Log.d(TAG, T.class.getName() + "<" + cls.getName() + ">"); } } } } } 

The logs will be this:

 java.lang.String java.lang.String java.lang.Object 
  • Look here , though it’s not quite what you want - MrFylypenko
  • here this option does not fit, I have already seen! - Kirill Stoianov
  • Is the list empty? - post_zeew
  • Does it matter? no, it is not empty, there are objects in it! - Kirill Stoianov
  • @KirillStoianov, Yes, it does. See the answer. - post_zeew

4 answers 4

If this is a specific class field, then like this:

 public class Main5 { List<String> stringList = new ArrayList<String>(); List<Integer> integerList = new ArrayList<Integer>(); public static void main(String... args) throws Exception { Field stringListField = Main5.class.getDeclaredField("stringList"); ParameterizedType stringListType = (ParameterizedType) stringListField.getGenericType(); System.out.println(stringListType);//java.util.List<java.lang.String> Field integerListField = Main5.class.getDeclaredField("integerList"); ParameterizedType integerListType = (ParameterizedType) integerListField.getGenericType(); System.out.println(integerListType);//java.util.List<java.lang.Integer> } } 
  • No, this is not a field of a particular class, this object is passed as an argument to the method. - Kirill Stoianov

If the list not empty, then the problem can be solved by obtaining the type of one of the list objects:

 ArrayList<String> arrayList = new ArrayList<>(); arrayList.add("Example"); System.out.println(arrayList.getClass().getName() + "<" + arrayList.get(0).getClass().getName() + ">"); 

UPD. A more general case (the list may be empty):

 public class Main { public static void main(String[] args) { ArrayList<String> arrayList = new ArrayList<>(); someMethod(arrayList); } public static void someMethod(ArrayList<String> arrayList) { Method[] methods = Main.class.getDeclaredMethods(); Type[] types = methods[1].getGenericParameterTypes(); ParameterizedType pType = (ParameterizedType) types[0]; Class<?> cls = (Class<?>) pType.getActualTypeArguments()[0]; System.out.println(arrayList.getClass().getName() + "<" + cls.getName() + ">"); } } 

where in the line

 Type[] types = methods[1].getGenericParameterTypes(); 

1 is the number of the method someMethod(...) in the class Main .

and in line

 ParameterizedType pType = (ParameterizedType) types[0]; 

0 is the number of the arrayList parameter of the method someMethod(...) .

  • Nice try, but it's a crutch rather than a universal solution! - Kirill Stoianov
  • @KirillStoianov; I agree with that, yes. - post_zeew
  • @KirillStoianov, added the answer. - post_zeew
  • I probably needed to immediately lay out my code, I as a parameter accept a non-typed object, I need to somehow get the type from the object itself, and not from the types of the method arguments. - Kirill Stoianov
  • And, that is, there is a method someMethod(Object obj) that you call, for example, someMethod(new String("str")) , and in the method you want to find out what type of object was passed to the method (in this case, String )? - post_zeew

As far as I remember, in Java, generics exist only during compilation, but they are not in bytecode, so it is most likely that you will not be able to get a type with a generic parameter in runtime.

PS: If I am mistaken, write a comment, delete the answer.

    As already written @Qwertiy in java in runtime it is impossible to get the type of parameterized collection. Objects of the type Object are stored there. Type-safe operations are provided by explicit type casting. This caste takes place at compile time.

    The most that can be done is to determine the type for the array. For them, for some reason, erasure does not occur.

     @SuppressWarnings("unchecked") private static <T> Class<T> getType(T[] array) { return (Class<T>) array.getClass().getComponentType(); }