• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

inserting a line of code in every method

 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'd like to know if there's a way to put a line of code in every method in all the class in certain package so that I don't have to type in that same line of code in all those codes.
the line of code could be something like logging. so perhaps we make a base class and all the class that wants such behavior needs only to extend that base class, and automatically all the methods in that child class will have the first line of code of logging inserted. or perhaps instead of subclassing, annotation will work better for this case? thanks.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's what AOP (Aspect Oriented Programming) was supposed to be about. It was supposed to address "cross-cutting concerns", i.e. concerns which applied across many methods of a class, precisely so you didn't have to write the same code in every method. The only concern I ever saw mentioned as an example was logging.

As you can probably tell I never looked into AOP closely. However I'm pretty sure that implementations of AOP do exist, so you could look into that.

I also have to say that when I wrote logging statements in my code I always did the logging from the business point of view. In other words I logged significant events such as successful completion of a file upload; the simple fact that a method was called wasn't usually significant. Unless my code was seriously out of control and I had no idea of what it was doing, that is. In that case I would log calls to a particular method. But logging calls to every method? I never found a use for that.
 
Andrew Cane
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well, actually it's useful when the code didn't produce any error, just wrong result. and add high developer turnover making it hard to trace since the new guy wouldn't know the program flow well. that's why if there's a way to trace the method calls, it would be a lot easier to trace the bug. at least that's what I think.
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah, in the case of incomprehensible code you might be right. On the other hand you might be inundated with so much logging that you wouldn't be able to figure out the problem. Personally I would prefer to run the code in debug mode to see what's happening, but sometimes that isn't feasible.
 
Rancher
Posts: 2759
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
AOP will do exactly what you need but you need a container like Spring to do the weaving for you.

If you don't have spring, you can implement your own java.Lang.reflect.InvocationHandler and use java.lang.reflect.Proxy to "inject" the invocation handler into your classes. It will be possibleto use Proxy if you have coded to interfaces, otherwise you are screwed. Also, it will be a lot easier if you have factory methods, because it means that all you do is change factory class to return the Proxy. Otherwise, you will have to find all the places where you are creating an instance
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Andrew Cane wrote:well, actually it's useful when the code didn't produce any error, just wrong result.


Hmmm. I wonder about that. Sounds to me like a methodology that expects problems, rather than eliminating them; or that you don't trust
  • the code
  • your practises
  • your programmers
  • to do their jobs properly.

    And I'm not sure that any system can cater for that.

    Winston
     
    Rancher
    Posts: 43081
    77
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    As an alternative to using Spring or rolling your own, you could check out AspectJ, which does the weaving for you.
     
    Bartender
    Posts: 612
    7
    Mac OS X Python
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    if this is really what you want to do, look at python - you can easily (say 100 lines of code or less) write a script that can traverse a series of directories adding lines of code to methods.

    And it is not that hard to add rules to not add to all methods (i.e.: getters / setters).

    -steve
     
    Andrew Cane
    Ranch Hand
    Posts: 91
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    @Steve Fahlbusch:
    are you saying that it's possible to mix phython script with existing java codes? haven't tried it before. I'm assuming this is what you're saying, not to convert the entire project to python.
    thanks
     
    Steve Fahlbusch
    Bartender
    Posts: 612
    7
    Mac OS X Python
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    no what i am saying is that you can use python to easily interrogate your source files and add line(s) of code to your source - which you would then re-compile and run.

    But also realize that python is dynamic so that you can add attributes and methods to a class on the fly, but no reason to convert to python, just use it to add code to source - say ----

    feed it a list of directories (maybe a file with a directory on each line) - then for each directory - find all the .java files, then each one - rename it, copy from the new name to the old name - adding a line or lines of code to each method, depending on what you want to the new file (old file name).

    when done with all, recompile, deploy and run.
     
    Rancher
    Posts: 989
    9
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    This is not a good solution to the problem at all.
    If there are bugs in the code write test cases to wean them out.
    Replicating lines of code is the same as copy paste programming which smells of bad design which is why code generators are bad.
    Logging is already solved by the many popular logging frameworks in existence many of which allow you to change the logging settings without restarting the application. If you have a bug then logging in many methods of many classes is not going to help. You need logging only in the methods that are executed before the bug. Logging at the start of methods won't help either and what you should log to find out the problem depends on the context of the bug. Often you'll want to have a log statement inside an if or a while logging the right variables that expose the problem, so random log statements at the start of methods is not going to cut it.
     
    Won't you be my neighbor? - Fred Rogers. tiny ad:
    a bit of art, as a gift, that will fit in a stocking
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic