Unused private methods, private fields and local variables

We use Sonar to review our code base. There are several violations for Unused private method, Unused private field and Unused local variable.

According to my understanding, private methods and private fields can be accessed outside the class only through reflection and the Java Native Interface. We do not use JNI in our code base, but use reflection in some places.

So, we plan to do a full workspace search for these methods and fields, and if they are not used anywhere, even through reflection, they will be commented out. Again, the chances of accessing private methods and fields through reflection are very small. This is for the safer side.

Unused local variables are not accessible outside the method. Therefore, we can comment on them.

Do you have any other suggestions for this?

+4
source share
2 answers

I like to reflect myself, but in the words: it can be a nightmare. Saving java reflection is very controllable (that is, stateless, without using global / external variables) and minimal scope.

What to look for?

To find private fields and methods that have become public, find Field#setAccessible() and Method#setAccessible() , for example, the following examples:

 Field privateNameField = Person.class.getDeclaredField("name"); privateNameField.setAccessible(true); Method privatePersonMethod = Person.class.getDeclaredMethod("personMeth", null); privatePersonMethod.setAccessible(true); 

So, setAccessible() will breathe for you, but getDeclaredField() and getDeclaredMethod() really where the fields are available (which really makes the fire).

Pay particular attention to the values ​​used in them, especially if they are variables (they are likely to be), as they determine access to the field.

Search in plain text

It is also useful to use text search for the field / method name in the entire project folder. I would say if you are not sure, do not delete before performing a full text search .

If you have many other projects that depend on this, which you are trying to change; and if you weren’t (or didn’t know) the guy who planted these (bombs), I would let him go. If only it would have changed, if really really needed. The best action would be to get them one by one when you need to make changes to the code around it.

And, and if you have, running tests with code coverage can also help you spend a lot of time looking for unused code.

+4
source

Calling an unused method using reflection is just weird. And unused fields can only be used as a deposit through reflection and used through reflection. Strange too.

Reflection is more used as a general means of copying a bean.

Thus, radical cleaning should be absolutely hassle-free. It would be better to take the time to look at using java.reflect; whether the reflection code is legal. This is a more sensible job than finding the use of private fields in rows.

And yes, remove it from the source code, which speeds up reading in seconds.

(Of course, I realized that it was a question like: I was watching something.)

0
source

Source: https://habr.com/ru/post/1480398/


All Articles