javac -J?

Discussion in 'Java' started by Mark Modrall, Sep 14, 2006.

  1. Mark Modrall

    Mark Modrall Guest

    Hi...

    I was just wondering if it was possible to use javac -J-Dname=value to
    get compile-time constants into code, perhaps with annotations or
    something?

    For example, I'm trying to get something like

    public class foo
    {
    public static String compiledBy=@builder;
    ....
    }

    or the like - getting a compile-time constant to be a referencable
    quantity in .class.

    Or is runtime preference or properties the only way to do it?

    Thanks
    -Mark
     
    Mark Modrall, Sep 14, 2006
    #1
    1. Advertising

  2. Mark Modrall wrote:
    > I was just wondering if it was possible to use javac -J-Dname=value to
    > get compile-time constants into code, perhaps with annotations or
    > something?
    >
    > For example, I'm trying to get something like
    >
    > public class foo
    > {
    > public static String compiledBy=@builder;
    > ...
    > }
    >
    > or the like - getting a compile-time constant to be a referencable
    > quantity in .class.
    >
    > Or is runtime preference or properties the only way to do it?


    Java does not do preprocessing. Period.

    You can use code or you can use configuration.

    Maybe (just maybe) you could use some AOP (like AspectJ)
    to insert the code you want.

    Arne
     
    =?ISO-8859-1?Q?Arne_Vajh=F8j?=, Sep 14, 2006
    #2
    1. Advertising

  3. Mark Modrall

    Daniel Dyer Guest

    On Thu, 14 Sep 2006 23:50:18 +0100, Arne Vajhøj <> wrote:

    > Mark Modrall wrote:
    >> I was just wondering if it was possible to use javac -J-Dname=value to
    >> get compile-time constants into code, perhaps with annotations or
    >> something?
    >> For example, I'm trying to get something like
    >> public class foo
    >> {
    >> public static String compiledBy=@builder;
    >> ...
    >> }
    >> or the like - getting a compile-time constant to be a referencable
    >> quantity in .class.
    >> Or is runtime preference or properties the only way to do it?

    >
    > Java does not do preprocessing. Period.


    The JDK tools don't, but that doesn't mean you can't do it. Any number of
    general-purpose text processing tools or 3rd-party Java pre-processors
    could be used. You could even do it with core Ant tasks.

    Dan.

    --
    Daniel Dyer
    http://www.dandyer.co.uk
     
    Daniel Dyer, Sep 15, 2006
    #3
  4. Daniel Dyer wrote:
    > On Thu, 14 Sep 2006 23:50:18 +0100, Arne Vajhøj <> wrote:
    >> Java does not do preprocessing. Period.

    >
    > The JDK tools don't, but that doesn't mean you can't do it. Any number
    > of general-purpose text processing tools or 3rd-party Java
    > pre-processors could be used. You could even do it with core Ant tasks.


    True.

    But that has very little to do with Java.

    Arne
     
    =?UTF-8?B?QXJuZSBWYWpow7hq?=, Sep 15, 2006
    #4
  5. Mark Modrall

    Chris Uppal Guest

    [non-existent newsgroups removed]

    Arne Vajhøj wrote:

    > > The JDK tools don't, but that doesn't mean you can't do it. Any number
    > > of general-purpose text processing tools or 3rd-party Java
    > > pre-processors could be used. You could even do it with core Ant tasks.

    >
    > True.
    >
    > But that has very little to do with Java.


    One point of relevance is that you can do what the OP wanted (more or less)
    with a simple extra build step, but /without/ preprocessing. E.g you could
    write ordinary Java code like:

    import org.metagnostic.Compiletime;

    public MyClass
    {
    public static void
    main(String[] args)
    {
    System.out.println("Compiled at: "
    + Compiletime.COMPILATION_DATE
    + " "
    + Compiletime.COMPILATION_TIME);
    System.out.println("By: " + Compiletime.COMPILED_BY);
    }
    }

    where the constants in Compiletime were specified either automatically, or by
    configuration of the build. The code would be compiled by an unmodified
    compiler. And, since the constants /are/ constants ;-) they will be hard-wired
    into the .class file so that .class files created at different times (but part
    of the same program) will include potentially different values.

    (It's that last point which makes Java relevant since the technique depends on
    the defined semantics of constants.)

    The implementation is obvious -- just create org.metagnostic.Compiletime.java
    afresh at each build. How you do that depends on your build environment.

    -- chris
     
    Chris Uppal, Sep 15, 2006
    #5
    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. xarax
    Replies:
    1
    Views:
    1,475
    Andy Flowers
    Jul 2, 2003
  2. Jon Skeet
    Replies:
    5
    Views:
    578
    xarax
    Jul 4, 2003
  3. Richard Ivarson
    Replies:
    1
    Views:
    515
    Dario
    Jul 14, 2003
  4. z3
    Replies:
    0
    Views:
    503
  5. Ronald Fischer

    Ant javac : How to print classpath?

    Ronald Fischer, Sep 18, 2003, in forum: Java
    Replies:
    1
    Views:
    17,603
    Wayne Berke
    Sep 19, 2003
Loading...

Share This Page