getTypeParameters 返回空 TypeVariable 数组

发布于 2024-10-25 19:51:30 字数 2825 浏览 1 评论 0原文

我正在编写一个程序,显示类中的方法及其访问修饰符、返回类型和参数。

这是我的代码

import java.lang.reflect.*;
class RefTest1{

    public static void main(String[] args) throws Exception{
        Test obj = new Test();      
        Class<?> c = obj.getClass();

        System.out.printf("%n%s fields :-%n", obj.getClass());

        Field[] fields = c.getDeclaredFields();

        for(Field f : fields){
            f.setAccessible(true);
            int m = f.getModifiers();

            if(Modifier.isStatic(m)){
                System.out.printf("%s is static variable and its value is %s%n", f.getName(), f.get(obj));
            }else if(Modifier.isPublic(m)){
                System.out.printf("%s is public variable and its value is %s%n", f.getName(), f.get(obj));
            }else if(Modifier.isPrivate(m)){
                System.out.printf("%s is private variable and its value is %s%n", f.getName(), f.get(obj));
            }else if(Modifier.isProtected(m)){
                System.out.printf("%s is protected variable and its value is %s%n", f.getName(), f.get(obj));
            }
        }
        System.out.printf("%n%s methods :-%n", obj.getClass());     

        Method[] methods = c.getDeclaredMethods();

        for(Method meth : methods){
            int m = meth.getModifiers();
            meth.setAccessible(true);
            if(Modifier.isStatic(m)){
                System.out.printf("%s is static method%n", meth.getName());
            }else if(Modifier.isPublic(m)){
                System.out.printf("%s is public method%n", meth.getName());
            }else if(Modifier.isPrivate(m)){
                System.out.printf("%s is private method%n", meth.getName());
            }else if(Modifier.isProtected(m)){
                System.out.printf("%s is protected method%n", meth.getName());
            }

            System.out.printf("%nReturn Type :- %s%n", meth.getReturnType());
            System.out.printf("%nParameters:-%n");
            TypeVariable[] parameters = meth.getTypeParameters();

            for(TypeVariable param : parameters){
                System.out.printf("%s", param.getName());
            }


        }
        System.out.println();

    }

}

Test.java

class Test{

    private int x;
    public double y;
    protected String z;
    static long a;

    public Test(){
        x = 10;
        y = 20;
        z = "Hello";
        a = 15L;

    }

    public void Print(String a){
        a = a;
        System.out.println("Executing Print function.");
    }

    private void hidden(double b){
        b = b; 
        //private function
    }
}

一切正常,但我不明白为什么我在 TypeVariable[]parameters = meth.getTypeParameters(); 行得到一个 TypeVariable 的空白数组代码>

有人能给我指出正确的方向吗?

谢谢。

I am writing a program that displays the methods inside a Class along with it's access modifier, return type and parameters.

Here's my code

import java.lang.reflect.*;
class RefTest1{

    public static void main(String[] args) throws Exception{
        Test obj = new Test();      
        Class<?> c = obj.getClass();

        System.out.printf("%n%s fields :-%n", obj.getClass());

        Field[] fields = c.getDeclaredFields();

        for(Field f : fields){
            f.setAccessible(true);
            int m = f.getModifiers();

            if(Modifier.isStatic(m)){
                System.out.printf("%s is static variable and its value is %s%n", f.getName(), f.get(obj));
            }else if(Modifier.isPublic(m)){
                System.out.printf("%s is public variable and its value is %s%n", f.getName(), f.get(obj));
            }else if(Modifier.isPrivate(m)){
                System.out.printf("%s is private variable and its value is %s%n", f.getName(), f.get(obj));
            }else if(Modifier.isProtected(m)){
                System.out.printf("%s is protected variable and its value is %s%n", f.getName(), f.get(obj));
            }
        }
        System.out.printf("%n%s methods :-%n", obj.getClass());     

        Method[] methods = c.getDeclaredMethods();

        for(Method meth : methods){
            int m = meth.getModifiers();
            meth.setAccessible(true);
            if(Modifier.isStatic(m)){
                System.out.printf("%s is static method%n", meth.getName());
            }else if(Modifier.isPublic(m)){
                System.out.printf("%s is public method%n", meth.getName());
            }else if(Modifier.isPrivate(m)){
                System.out.printf("%s is private method%n", meth.getName());
            }else if(Modifier.isProtected(m)){
                System.out.printf("%s is protected method%n", meth.getName());
            }

            System.out.printf("%nReturn Type :- %s%n", meth.getReturnType());
            System.out.printf("%nParameters:-%n");
            TypeVariable[] parameters = meth.getTypeParameters();

            for(TypeVariable param : parameters){
                System.out.printf("%s", param.getName());
            }


        }
        System.out.println();

    }

}

Test.java

class Test{

    private int x;
    public double y;
    protected String z;
    static long a;

    public Test(){
        x = 10;
        y = 20;
        z = "Hello";
        a = 15L;

    }

    public void Print(String a){
        a = a;
        System.out.println("Executing Print function.");
    }

    private void hidden(double b){
        b = b; 
        //private function
    }
}

Every thing is working fine but I don't understand why I get a blank array of TypeVariable at line TypeVariable[] parameters = meth.getTypeParameters();

Can some one point me in a right direction?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

下雨或天晴 2024-11-01 19:51:30

getTypeParameters( ) 返回类型参数的数组在方法定义中使用。它返回参数类型数组。考虑这个方法:

 public <T> void foo(int bar);

getTypeParameters() 将返回一个包含 T 的数组(即 TypeVariable ,名称为 T ,边界为 { Object .class})。

getParameterTypes( ) 但是,将返回一个包含 int.class 的数组。

注意:如果您的参数类型包含类型参数,那么您需要使用 getGenericParameterTypes()

getTypeParameters() returns the array of type parameters used in the method definition. It does not return the array of argument types. Consider this method:

 public <T> void foo(int bar);

getTypeParameters() would return an array containing T (i.e. a TypeVariable with the name T and the bounds { Object.class }).

getParameterTypes() however, would return an array containing int.class.

Note: If your parameters types contain type parameters, then you'll need to use getGenericParameterTypes().

喜你已久 2024-11-01 19:51:30

我认为你应该使用 getParameterTypes() 它返回 Class[]

I think you should use getParameterTypes() which returns Class[]

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文