can annotations add code?

Discussion in 'Java' started by Timasmith, Nov 29, 2006.

  1. Timasmith

    Timasmith Guest

    Hi,

    One thing that would be wonderful is if I could add an annotation say

    @logthis
    public void doSomething() {
    int i=0;
    i=10;
    }

    which inserted code into the beginning and end of the method such as

    public void doSomething() {
    startLogging("doSomething");
    int i=0;
    i=10;
    stopLoggin("doSomething":);
    }

    Do annotations come close to providing that?

    thanks
     
    Timasmith, Nov 29, 2006
    #1
    1. Advertising

  2. On Nov 29, 7:09 pm, "Timasmith" <> wrote:
    > Hi,
    >
    > One thing that would be wonderful is if I could add an annotation say
    >
    > @logthis
    > public void doSomething() {
    > int i=0;
    > i=10;
    >
    > }which inserted code into the beginning and end of the method such as
    >
    > public void doSomething() {
    > startLogging("doSomething");
    > int i=0;
    > i=10;
    > stopLoggin("doSomething":);
    >
    > }Do annotations come close to providing that?
    >
    > thanks


    No
     
    andrewmcdonagh, Nov 29, 2006
    #2
    1. Advertising

  3. Timasmith

    Wesley Hall Guest

    Timasmith wrote:
    > Hi,
    >
    > One thing that would be wonderful is if I could add an annotation say
    >
    > @logthis
    > public void doSomething() {
    > int i=0;
    > i=10;
    > }
    >
    > which inserted code into the beginning and end of the method such as
    >
    > public void doSomething() {
    > startLogging("doSomething");
    > int i=0;
    > i=10;
    > stopLoggin("doSomething":);
    > }
    >
    > Do annotations come close to providing that?


    No, but AOP does.
     
    Wesley Hall, Nov 29, 2006
    #3
  4. Timasmith

    Daniel Dyer Guest

    On Wed, 29 Nov 2006 19:09:35 -0000, Timasmith <>
    wrote:

    > Hi,
    >
    > One thing that would be wonderful is if I could add an annotation say
    >
    > @logthis
    > public void doSomething() {
    > int i=0;
    > i=10;
    > }
    >
    > which inserted code into the beginning and end of the method such as
    >
    > public void doSomething() {
    > startLogging("doSomething");
    > int i=0;
    > i=10;
    > stopLoggin("doSomething":);
    > }
    >
    > Do annotations come close to providing that?


    You might want to look into Aspect Oriented Programming (AOP).

    Dan.

    --
    Daniel Dyer
    http://www.uncommons.org
     
    Daniel Dyer, Nov 29, 2006
    #4
  5. Timasmith

    hiwa Guest

    > Do annotations come close to providing that?
    Yes. You must be an apt expert to do that.
     
    hiwa, Nov 29, 2006
    #5
  6. hiwa wrote:
    >> Do annotations come close to providing that?

    > Yes. You must be an apt expert to do that.


    APT can be used to generate new code. It does not alter existing code.

    I'm sure there are plenty of AOP-style tools that will help, either
    modifying the byte code of the .class files, modifying classes in a
    custom class loader or using instrumentation APIs on loading the code.

    Tom Hawtin
     
    Thomas Hawtin, Nov 30, 2006
    #6
  7. Timasmith

    Timasmith Guest

    Thomas Hawtin wrote:
    > hiwa wrote:
    > >> Do annotations come close to providing that?

    > > Yes. You must be an apt expert to do that.

    >
    > APT can be used to generate new code. It does not alter existing code.
    >
    > I'm sure there are plenty of AOP-style tools that will help, either
    > modifying the byte code of the .class files, modifying classes in a
    > custom class loader or using instrumentation APIs on loading the code.
    >
    > Tom Hawtin



    Ok, I guess ideally the annotation would drive the AOP- tool, I'll look
    into that.
     
    Timasmith, Nov 30, 2006
    #7
  8. Timasmith

    hiwa Guest

    Timasmith wrote:
    > Thomas Hawtin wrote:
    > > hiwa wrote:
    > > >> Do annotations come close to providing that?
    > > > Yes. You must be an apt expert to do that.

    > >
    > > APT can be used to generate new code. It does not alter existing code.
    > >
    > > I'm sure there are plenty of AOP-style tools that will help, either
    > > modifying the byte code of the .class files, modifying classes in a
    > > custom class loader or using instrumentation APIs on loading the code.
    > >
    > > Tom Hawtin

    >
    >
    > Ok, I guess ideally the annotation would drive the AOP- tool, I'll look
    > into that.

    Some 'experts' on Java annotation are talking about ....
    http://forum.java.sun.com/thread.jspa?threadID=739960&start=0&tstart=20
    http://forum.java.sun.com/thread.jspa?threadID=735736&messageID=4227834#4227834
     
    hiwa, Nov 30, 2006
    #8
  9. On 29.11.2006 20:09, Timasmith wrote:
    > Hi,
    >
    > One thing that would be wonderful is if I could add an annotation say
    >
    > @logthis
    > public void doSomething() {
    > int i=0;
    > i=10;
    > }
    >
    > which inserted code into the beginning and end of the method such as
    >
    > public void doSomething() {
    > startLogging("doSomething");
    > int i=0;
    > i=10;
    > stopLoggin("doSomething":);
    > }
    >
    > Do annotations come close to providing that?


    Others have answered that already. However, if you just want to trace
    execution then there are plenty tools out there. These work either on
    VM level or inside (limpy log or so).

    robert
     
    Robert Klemme, Nov 30, 2006
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Efy.
    Replies:
    1
    Views:
    564
    Nicole Calinoiu
    Sep 22, 2003
  2. Replies:
    3
    Views:
    563
    Chris Uppal
    Jan 25, 2005
  3. Ingo R. Homann
    Replies:
    0
    Views:
    582
    Ingo R. Homann
    Jul 26, 2005
  4. Vidar S. Ramdal

    Annotations and Retention

    Vidar S. Ramdal, Aug 18, 2005, in forum: Java
    Replies:
    2
    Views:
    671
    Vidar S. Ramdal
    Aug 19, 2005
  5. Alex J
    Replies:
    7
    Views:
    837
    Alex J
    Sep 12, 2011
Loading...

Share This Page