I got this private member private Map<Character, String> eTM = new HashMap<>();
.
I need to be able read its size from a test. I'd like to do it using Java reflection, however it seems that in order to be able to call size()
I'd have to get the field as a java.lang.reflect.Field. You can't call any Map methods on that so I'd have to cast the field to "Map<Character, String>" or "HashMap<Character, String>". That doesn't seem to be possible though.
I experimented a bit to see if you could somehow access the size method on a private Map, but I don't see a way to do this. For example:
Field eTmF = Holder.class.getDeclaredField("eTM");
HashMap castTM = (HashMap<String, Character>) eTmF; //cannot cast
castTM.size();//not available
This shows zero length: Holder.class.getDeclaredClasses().length;
and unsurprisingly this has nothing to print:
Class<?>[] classes = Holder.class.getDeclaredClasses();
Arrays.stream(classes).forEach(c -> System.out.println(c.getSimpleName()));
Is this simply as far as reflection goes or is there a way?
For now I've loosened the field to "protected", but if it's possible I'd like to learn. For one thing I was hoping for hints on reflection libraries that may be able to go deeper.
Aucun commentaire:
Enregistrer un commentaire