java - Unused private methods, private fields and local variables -
we using sonar review our codebase. there few violations unused private method, unused private field , unused local variable.
as per understanding private methods , private fields can accessed outside of class through reflection , java native interface. not using jni in our code base, using reflection in places.
so planning complete workspace
search these methods , fields , if these not used anywhere through reflection, these commented out. again chances accessing private methods , fields through reflection less. safer side.
unused local variables can’t accessed outside of method. can comment out these.
do have other suggestions this?
i love reflection myself, put in few words: can nightmare. keep java reflection controlable (that is, stateless, no global/external variable usage) , minimal scope.
what for?
to find private fields , methods turned public, field#setaccessible()
, method#setaccessible()
, such examples below:
field privatenamefield = person.class.getdeclaredfield("name"); privatenamefield.setaccessible(true); method privatepersonmethod = person.class.getdeclaredmethod("personmeth", null); privatepersonmethod.setaccessible(true);
so, setaccessible()
smoke, getdeclaredfield()
, getdeclaredmethod()
fields accessed (what makes fire).
pay special attention values used in them, specially if variables (they be), determine field accessed.
do plain text search
also, doing plain text search field/method name on whole project folder useful. i'd say, if not sure, don't delete before doing full text search.
if have many other projects depend on 1 trying change; , if weren't (or didn't know) guy planted (bombs), i'd let go. change if really needed to. best action them 1 one when need make change code around it.
ah, and, if have them, running tests code coverage can big time in spotting unused code.
Comments
Post a Comment