• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Rob Spoor
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Carey Brown
Bartenders:

Do we have chance to change a method?

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I want to change a method body and not to change existing source code running on web app.
Does spring offer that magical function?
What I mean by change is not to replace totally but do some small changes. It's more like below to insert new logic at some point or row:


The scenario is we want to inspect a large of projects ,when new records are inserted or update in these projects running on tomcat , I hope all these data also be synchronized to my inspection database so that we can summarize latest data that need to be checked by administrator. So I need to do some extra work to the existing code, but cause the number of projects is pretty big , adding logic for every single class method is not reasonable, so I just want to add synchronizing code in a relative easy and common way. Basically we have got all the methods we need to handle and enough materials .
If it can't be possible, we had to change source code ,that would be the last way we wanna choose.
Thanks.
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's what AOP is all about adding code to your code through Aspects and Advices.

Mark
 
Kurt Xu
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mark Spritzler wrote:That's what AOP is all about adding code to your code through Aspects and Advices.

Mark



Hi, Mark:
I think aop can't insert some logic into some certain row of existing method code , can just do something other before or after existing method . If I'm wrong ,can you please show me some sample?
After interceptor is the first idea I can think of , but is abandoned right now, because the method to be intercept doesn't throw a exception,if we use after interceptor or around interceptor, we can't know whether the transaction is successful or not. the existing code is like below:

So pure after interceptor can't handle that. data must be synchronized only when inspected data updating is successful. we need insert code into some certain point of existing code and the new logic can access the rest of the method. Such as define a boolean variable to save transaction state to determine whether do synchronizing.

 
Mark Spritzler
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kurt Xu wrote:

Mark Spritzler wrote:That's what AOP is all about adding code to your code through Aspects and Advices.

Mark



Hi, Mark:
I think aop can't insert some logic into some certain row of existing method code , can just do something other before or after existing method . If I'm wrong ,can you please show me some sample?
After interceptor is the first idea I can think of , but is abandoned right now, because the method to be intercept doesn't throw a exception,if we use after interceptor or around interceptor, we can't know whether the transaction is successful or not. the existing code is like below:

So pure after interceptor can't handle that. data must be synchronized only when inspected data updating is successful. we need insert code into some certain point of existing code and the new logic can access the rest of the method. Such as define a boolean variable to save transaction state to determine whether do synchronizing.



You are correct, only before or after(around). So, say you want code added at line 4. Just refactor. Make two methods, one with lines 1-3 and another with the remaining lines and make an After after the first method or a Before for the second method.

You see for me, if before or after wouldn't work, that is sign to me that I need to refactor and look at my design, because if I need to add code in the middle, probably means my method isn't Highly Cohesive, it is probably doing more than one thing.

Mark
 
reply
    Bookmark Topic Watch Topic
  • New Topic