javac -J?

M

Mark Modrall

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
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Mark said:
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
 
D

Daniel Dyer

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.
 
G

Guest

Daniel said:
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
 
C

Chris Uppal

[non-existent newsgroups removed]
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,190
Latest member
Martindap

Latest Threads

Top