I am writing a hibernate listener which will do some operation before and after passing to db. I have to get a value of a field using reflection. I have wrote a sample code to demonstrate my question.
public class Test {
FileAttachment attachment = new FileAttachment();
public Test() {
attachment.setData(new byte[1]);
}
void test() throws Exception{
for(Field field :attachment.getClass().getDeclaredFields()) {
if ((CloudPersistable.class).isAssignableFrom(field.getType())) {
System.out.println("BOom");
Class<?> x = Class.forName(attachment.getClass().getCanonicalName());
Field f = x.getDeclaredField("data");
f.setAccessible(true);
CloudPersistable attachment = (CloudPersistable) f.get(f.getClass());
System.out.println(attachment);
}
}
}
public static void main(String[] args)throws Exception {
new Test().test();;
}
}
** Please help to resolve line CloudPersistable attachment = (CloudPersistable) f.get(f.getClass());
Thanks in advance..!!!