1

Given a function that returns either an object e.g. String or null - how can I easily find all locations in code where the return value is not being either assigned to a String variable (case1) or immediately evaluated as part of a condition (case2)?

e.g.

public String getMessageInLog(String toFind) {
    // return String or null, depending on if found
}

String lineInLog = getMessageInLog("test"); // case1
Assert.assertNotNull(getMessageInLog("test"); // case2
Assert.assertNotNull("Failure error message", getMessageInLog("test"); // case2 (extended example - why searching for " getMessageInLog(" does not work.
getMessageInLog("test") // case3 - what I'm trying to find

This type of call is being used thousands of times in tests and I would like to identify where it is being used like in case3.

  • Only for this specific method called `getMessageInLog`? – luk2302 May 17 '19 at 15:21
  • Look through the settings of Eclipse. It may have an option to warn when a return value isn't used. You'll get a lot of irrelevant warnings along with it though. – Carcigenicate May 17 '19 at 15:22
  • @luk2302 For this method (and others overrides of it with more/different parameters). I know I can use the Call Hierarchy view on each but it's very time consuming. –  May 17 '19 at 15:23

2 Answers2

1

Perform a regex search Eclipse, regular expression search and replace for

$\s+getMessageInLog
luk2302
  • 55,258
  • 23
  • 97
  • 137
  • I suppose I need to expand my example as there is often `Assert.assertNotNull("Error message", getMessageInLog("test"));` which would be picked up with this regex but is actually `case2` –  May 17 '19 at 15:30
  • @ReadyPlayer2 that should not be caught since it does not only have whitespaces in front of the method name. – luk2302 May 17 '19 at 15:31
0

You can write an Eclipse plugin which would be able to

  1. find references to methods and classes with SearchEngine.
  2. interact with the Java compiler using the JDT plugin (Java Development Tooling) to create AST's for the references you found.
  3. traverse the AST's a bit to see how the references are used (if condition, right side of assignment or stand-alone statement)
Chris Gerken
  • 16,221
  • 6
  • 44
  • 59