Menu

#42 Exclude methods used by reflection (Swing EventHandlers, JSP)

Next_Release
open
None
5
2015-02-09
2013-09-26
No

Hi,

it's quite common, with java beans event / swing, to avoid the creation of anonymous classes to handle all the various events by using the utility class java.beans.EventHandler, which is used this way, for example:

button.addActionListener(EventHandler.create(
    ActionListener.class, someInstance, "someMethod")
);

This is quite a recognizable pattern that does introspection calls to someInstance.someMethod(). IMHO, UCDetector should then not mark someMethod() as never referenced.

I understand introspection is always a problem with static dead code analysis. But here it's a common pattern that is documented and even suggested in some java documentation to avoid having to much anonymous classes that just delegates to another method.

I know it's possible to mark those methods to be ignored by UCDetector, but that mean we lose track of them in the future and, in big projects, that mean a lot of markers to add.

Could this be added as a feature request?
David D.

Discussion

  • Joerg Spieler

    Joerg Spieler - 2013-09-26

    To realize this feature request we need a new setting "Search method name in java files", which searches "someMethod" (including quotes) in java files.

    This setting is similar to "Search class name in files" in UCDetector preferences on "Detect" tab.

     
  • Anonymous

    Anonymous - 2013-10-21

    I am not sure if this is a bug or if I just have the preferences set up incorrectly.

    • Both Spring injections
    • Use of jsp ability to not use get, set, is on attributes to access from the bean class

    I have noticed that UCDetector is reporting methods as unused when the method is used.

    Here is an example of what I am talking about.

    JSF application… The following is in the jsp.

    <f:selectItems
      value="#{ClassFormBean.producerDropdownHelper.sortByProducerMap}" />
    

    The ClassFormBean has the methods: getProducerDropdownHelper()
    The producerDropdownHelper attribute is spring injected

    The ProducerDropdownHelper class has the method getSortByProducerMap()

    • When I look at the ProducerDropdownHelper the getSortByProducerMap() method is marked as unused
    • When I look at the ClassFormBean the getProducerDropdownHelper() method is marked as unused

    Hopefully, this is clear enough to determine if the issue is on my end or if this is a bug in UCDetector.

    Let me know if I can provide more information.

    By the way, I really like the ease of use so far. I have got around the above issue by using Search -> File and looking for the attribute name. If it shows in the jsp, I then track to the bean to make sure if it is used or not.

    Jim

     
  • Joerg Spieler

    Joerg Spieler - 2013-10-21
    • summary: Support for swing EventHandler class --> Exclude methods used by reflection (Swing EventHandlers, JSP)
     
  • Joerg Spieler

    Joerg Spieler - 2013-10-22

    From user hstoerr posted in Open Discussion

    When looking for unused bean properties it would be helpful if UCDetector could optionally look for occurrences of the bean property without the get prefix in e.g. JSP files or other expression languages. I do realize that that would mark common properties like name and id as used, but this might help if the bean properties are named descriptively.

    More specifically: the Method

    ~~~~~~
    FooBean.getSomethingNicelyNamed()
    ~~~~~~
    

    would be counted as referenced if there is a String somethingNicelyNamed in *.jsp. Probably this feature would need a configuration of the file types recognized as expression language. It might be worth looking at the character(s) before and after the String - these should be word separators, e.g. ", ., spaces etc

     

Anonymous
Anonymous

Add attachments
Cancel