如何在Java中获取java.lang.reflect.Type的简单名称。

huangapple go评论49阅读模式
英文:

how do I get the simple name of a java.lang.reflect.Type in java

问题

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

无法控制传递给 getSimpleName(Type type) 的变量,除非它是 Type 的实例。

当然,我知道在下面的示例中可以使用 p.getType().getSimpleName(),但重要的是解决以下示例。我想要展示的是,如果用户(我无法控制)通过调用 p.getParameterizedType() 来传递 getSimpleName(Type type),他仍然可以获得正确的结果(即 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){
    }
}

上述代码将打印 "java.util.List<java.lang.String>",但我想要获得 "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){
    }
}

上述代码将抛出 java.lang.ClassCastException

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

非常感谢。

英文:
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(&quot;foo&quot;, List.class);
        final Parameter p = m.getParameters()[0];
        System.out.println(p.getParameterizedType());
    }

    public void foo(List&lt;String&gt; 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(&quot;foo&quot;, List.class);
        final Parameter p = m.getParameters()[0];
        System.out.println(((Class&lt;?&gt;)p.getParameterizedType()).getSimpleName());
    }

    public void foo(List&lt;String&gt; strings){
    }
}

the above will throw java.lang.ClassCastException.

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

Thanks a lot.

答案1

得分: 2

你可以使用 getType()getSimpleName() 方法。

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

输出:

List
英文:

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

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

Output:

List

答案2

得分: 2

  1. Type对象强制转换为ParameterizedType,然后调用它的getRawType方法。
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){
    }
}

这将输出"interface java.util.List",虽然不完全符合您的要求,但接近。

  1. 使用正则表达式从"java.util.List<java.lang.String>"中提取您想要的名称。

但正如@deadshot指出的,您也可以使用getType()来获取ParameterClass,然后通过Class API获取其名称。

英文:

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(&quot;foo&quot;, List.class);
            final Parameter p = m.getParameters()[0];
            System.out.println(((ParameterizedType) p.getParameterizedType()).getRawType());
        }
    
        public void foo(List&lt;String&gt; 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 &quot;java.util.List&lt;java.lang.String&gt;&quot;.


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.

huangapple
  • 本文由 发表于 2020年7月22日 18:01:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/63031668.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定