I am trying to created a commonMethod which acccepts a Generic List allData type and prints out the values of the alldata object received. Additionally, I am sending the className as a parameter so that we can retrieve the Class and the Fields through Reflection.
I am almost there except the part where I want to declare the className in a forloop. I know the rest of the code works, because i tried hardcoding a ClassName and it works.
public void commonMethod(List<?> alldata, String className) {
Class c = Class.forName(className);
Field[] fields = c.getDeclaredFields();
//I would like to define incoming className here..but failing
for (className c : alldata) {
for (Field field : fields) {
field.setAccessible(true);
Object value = null;
value = field.get(c);
System.out.println(value);
//use the value in some other code
}
}
}
How do I change this part for (className c : alldata) to get it working..
Thanks
for (Object c: allData).public <T> void commonMethod(List<T> alldata, Class<T> c) { /* removed: Class c = Class.forName(className); */then use T