jeudi 21 janvier 2016

Any good reason to restore the original accessiblity value after changing it? [duplicate]

This question already has an answer here:

When accessing a non-visible field or method using reflection, it seems to be a common idiom to restore the original accessibility value after changing it:

Field field = ...
boolean origAccessibility = field.getAccessibility();
field.setAccessibility(true);
try {
  // access field
} finally {
  field.setAccessibility(origAccessibility);
}

I wonder if there is a good reason to restore the original accessibility state. I see the following cases:

  • Compiled code cannot access the field anyway.
  • Code that accesses a non-visible field using reflection would also call setAccessible(true)
  • Code that uses reflection just to query the method's accessibility would potentially get the wrong result anyway, while the try block is executed.

I tend to think that resetting the original state is useless. Am I missing something?





Aucun commentaire:

Enregistrer un commentaire