Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getTypeParameters returns a null TypeVariable array

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.

like image 730
Searock Avatar asked May 07 '26 18:05

Searock


2 Answers

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().

like image 97
Joachim Sauer Avatar answered May 10 '26 08:05

Joachim Sauer


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

like image 21
MByD Avatar answered May 10 '26 07:05

MByD



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!