mercredi 30 janvier 2019

Java: Getting the name of parameter of constructor

I am trying to access the name of the parameter of the constructor. But I am not able to do so.

//Constructor
        public User (String userId,String username,String passWord,String roles)
        {
          ...
        }

//step to get names
        Class<?> clazz = Class.forName(user.getClass().getName());
        // I am accessing the class name also dynamicall as of my 
        // requirement.

        for (Constructor<?> ctor : clazz.getConstructors()) {
            Parameter[] paramNames = ctor.getParameters();
            for (Parameter p :paramNames)           {
                System.out.print(p.getName());
               //output is arg0 arg1 arg2 arg3
            }
        }

Isn't this the right way? Or I am missing something? Is it possible or not?





Aucun commentaire:

Enregistrer un commentaire