-1

I am trying to run a method after returning data in a set of code.

If I put it below the return, it will become unreachable code. If I put in above, I am not able to extract data out of it since it will only insert the data into SQL after the return method.

I am using an open source James Email Server to do this.

So for example.. the code is like this..

public class testList
  extends GenericMatcher
{
    public Collection test(Mail mail) {
    /*All the processing of the mail datas and checks will be done here.. 
    after which, return mail.getRecipients(); at the end will send 
    the result back to.. I dont know where? This return will then 
    insert it into the SQL table. So I would like to move this data 
    from that SQL table to another SQL table that is why I      
    need to access only after the return method.*/
        if (test.contains(test1)) {
            return mail.getRecipients();
            //call other method here
        } else {
            return null;
        }
    }
}

So is there a way for me to call the method or do some more data processing after the return method? I have no idea where does the return method go to too.

https://i.stack.imgur.com/woWYh.jpg

Nicky
  • 63
  • 1
  • 2
  • 11
  • 2
    Capture the return from `mail.getRecipients()` in a variable and use that before returning the value of the variable. – Sean May 18 '16 at 12:24
  • `try { return something; } finally { someMethod(); }` is also possible –  May 18 '16 at 12:26
  • `"will send the result back to.. I dont know where?"` - It will send the result back to whatever *called* this method. What are you actually trying to do here? – David May 18 '16 at 12:26
  • store `mail.getRecipients();` result in variable and return that variable at last. during initialization of variable set variable value as null so you don't need else part too. – Jitendra Joshi May 18 '16 at 12:27
  • The return result will send back the result in order to insert automatically into the SQL.. but I do not know which method calls this.. – Nicky May 18 '16 at 12:35
  • @Nicky: Did you not write this code? Can your IDE show you what references this method? Anything could be calling this, and that other code could be doing anything. It doesn't sound like you're really asking the right question here, it just sounds like you're kind of lost in the code. – David May 18 '16 at 12:43
  • @David Yeah I am pretty much lost in the codes because I feel that only after it returns the recipients that I stored, it then stores the data into the database. I could not get the data but when I go mysql db and check for the data, its there. So basically its performing before the insertion is done – Nicky May 18 '16 at 12:59
  • @Nicky: Honestly, you'd probably benefit from some introductory tutorials on Java before trying to modify this code. A `return` statement doesn't insert anything into a database, it just returns a value to whatever code called this function. It might help to place a debugging breakpoint in this method and then step through the code once that breakpoint is hit, so you can see what called the method and maybe find where this database interaction is taking place. – David May 18 '16 at 13:02

3 Answers3

4

You can use the finally keyword like this:

try {
    doThings();
    return;
} finally {
    doThingsAfterReturn();
}
Guus
  • 399
  • 2
  • 15
  • I tried doing this, it executed the codes after return at finally block but it did not show the information inserted after return. After the return method, I will do a resultset to extract data inserted in try. But there is no information from resultset – Nicky May 18 '16 at 12:34
  • I feel like even at finally, the things are do at try is still not inserted into the database. So I could not extract any datas out of the database.. – Nicky May 18 '16 at 12:39
  • @Nicky: That's because the code you're showing doesn't insert *anything* into a database. It just returns a value. The code you're *looking for* is whatever actually inserts into the database. – David May 18 '16 at 12:44
  • @David I added in the picture to this code. I did not write the code, it is open source email server called james and I am only writing my custom matchers – Nicky May 18 '16 at 12:53
  • So just do something like this then Collection c = mail.getRecipients(); Then you can do stuff with c and afterwards return c. If c is empty then you need to override something else within the server. – Guus May 18 '16 at 12:54
  • 1
    @Nicky: You seem to be *very* lost here, and the answers posted to this question are serving only to confuse you further. The code you're showing just returns a value, nothing more. There is no interaction with a database, there is nothing to do "after the return". Whatever you're looking for is somewhere else in the system, not in this method. There is nothing you can write in this method which will somehow affect the code which *calls* this method. – David May 18 '16 at 12:58
0

You can use Exception handling mechanism to do this. Place the code in finally block, it will then definitely execute.

CherryDT
  • 25,571
  • 5
  • 49
  • 74
SivaTeja
  • 378
  • 1
  • 3
  • 16
0

actually you could call the "other method" after calling the test method in testlist class , which would have the same effect

Collection abc=new testList().test();

doMethodAfterReturn();

shihas
  • 1