-2
public String getSimpleName(Type type){
    //how?
}

I can't controll variable passed into getSimpleName(Type type) except that it is instance of Type.

Of course I know that I can use p.getType().getSimpleName() in the following example, but it's not important to solve the following exmaple. what I want to show from it is that if the user (out of my control) passes p.getParameterizedType() calling getSimpleName(Type type),he still can get correct result (i.e. List).

public class App {

    public static void main(String[] args) throws NoSuchMethodException {
        final Method m = App.class.getMethod("foo", List.class);
        final Parameter p = m.getParameters()[0];
        System.out.println(p.getParameterizedType());
    }

    public void foo(List<String> strings){
    }
}

the above will print "java.util.List<java.lang.String>", but I want to get "List".

import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.List;

public class App {

    public static void main(String[] args) throws NoSuchMethodException {
        final Method m = App.class.getMethod("foo", List.class);
        final Parameter p = m.getParameters()[0];
        System.out.println(((Class<?>)p.getParameterizedType()).getSimpleName());
    }

    public void foo(List<String> strings){
    }
}

the above will throw java.lang.ClassCastException.

Exception in thread "main" java.lang.ClassCastException: sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl cannot be cast to java.lang.Class

Thanks a lot.

goushiso
  • 199
  • 1
  • 10

2 Answers2

2

You can use getType() and getSimpleName() methods.

System.out.println(p.getType().getSimpleName());

Output:

List
deadshot
  • 8,881
  • 4
  • 20
  • 39
  • I can't limit the variable passed into `public String getSimpleName(Type type)` except that it is instance of Type – goushiso Jul 22 '20 at 10:54
2

There are two approaches to getting a what you want from a Type.

  1. Cast the Type object to a ParameterizedType and call getRawType on it.

    import java.lang.reflect.*;
    import java.util.List;
    
    public class App {
    
        public static void main(String[] args) throws NoSuchMethodException {
            final Method m = App.class.getMethod("foo", List.class);
            final Parameter p = m.getParameters()[0];
            System.out.println(((ParameterizedType) p.getParameterizedType()).getRawType());
        }
    
        public void foo(List<String> strings){
        }
    }
    

    That outputs "interface java.util.List". Not exactly what you want, but closer.

  2. Use a regex to extract the name you want from "java.util.List<java.lang.String>".


But as @deadshot points out, you can get the Class for a Parameter using getType() and then get the name of that via the Class API.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • Not sure if it's guaranteed (probably not), but in this case it appears `ParameterizedType#getRawType()` returns an instance of `Class`, which means `getSimpleName()` can be used directly. – Slaw Jul 22 '20 at 11:22