mercredi 6 avril 2016

Get the list of property of Object from List of Object dynamically in java

I am trying to get a list of property ,means I have a class User and it has a property name and I have a public method getName() to get the name of user, Now my problem is I want to create a method which takes a generic list , and method name , And with helping of stream API I get the list of property of class.

Means if I pass the list of user and method name 'getName' in test method then it returns the list of username.

public static boolean test(List<?> propertyList) throws Exception{
                Class<?> c = Class.forName(propertyList.get(0).getClass().getCanonicalName());
                Method method = c.getDeclaredMethod(methodName);

             !propertyList.stream().map(c::method).allMatch(new HashSet<>()::add);
        }





Aucun commentaire:

Enregistrer un commentaire