| Author |
Is conditional compiling possible in JAVA?
|
|
| Suma Shanbhog 2006-08-18, 8:03 am |
| Hello All,
I wanted to know if conditional compiling ( if #def <Something> in C++) is
possible in java and if yes then how?
Regards
Suma
| |
| Thomas Weidenfeller 2006-08-18, 8:03 am |
| Suma Shanbhog wrote:
> I wanted to know if conditional compiling ( if #def <Something> in C++) is
> possible in java and if yes then how?
No, and one usually doesn't need it. Among your options are:
- Use boolean constants and let the compiler figure out if code can be
ever reached [ if(debug) { ...} ].
- Use annotations and the JDK's apt tool to implement some code generation.
- Use a general-purpose macro processor (I prefer Unix' good old M4)
- Use a C/C++ preprocessor. Java's syntax is so similar to C/C++ that
many C/C++ preprocessors don't barf on Java input
- Use some special Java preprocessor. People have written a few. Non of
which is AFAIK in widespread use (an indication that one really doesn't
need a preprocessor in Java).
- Use reflection to conditionally load and execute code, depending on
platform, etc.
There are probably more alternatives.
/Thomas
--
The comp.lang.java.gui FAQ:
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS...ng/java/gui/faq
http://www.uni-giessen.de/faq/archi...g.java.gui.faq/
| |
| Suma Shanbhog 2006-08-22, 4:01 am |
| Thanks!
Will check out the links....
Suma
"Thomas Weidenfeller" <nobody@ericsson.invalid> wrote in message
news:ec4747$feu$1@news.al.sw.ericsson.se...
> Suma Shanbhog wrote:
is[color=darkred]
>
> No, and one usually doesn't need it. Among your options are:
>
> - Use boolean constants and let the compiler figure out if code can be
> ever reached [ if(debug) { ...} ].
>
> - Use annotations and the JDK's apt tool to implement some code
generation.
>
> - Use a general-purpose macro processor (I prefer Unix' good old M4)
>
> - Use a C/C++ preprocessor. Java's syntax is so similar to C/C++ that
> many C/C++ preprocessors don't barf on Java input
>
> - Use some special Java preprocessor. People have written a few. Non of
> which is AFAIK in widespread use (an indication that one really doesn't
> need a preprocessor in Java).
>
> - Use reflection to conditionally load and execute code, depending on
> platform, etc.
>
> There are probably more alternatives.
>
> /Thomas
> --
> The comp.lang.java.gui FAQ:
> ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS...ng/java/gui/faq
> http://www.uni-giessen.de/faq/archi...g.java.gui.faq/
| |
| ge0rge 2006-08-22, 8:01 am |
| Suma Shanbhog wrote:
> "Thomas Weidenfeller" <nobody@ericsson.invalid> wrote in message
> news:ec4747$feu$1@news.al.sw.ericsson.se...
>
> is possible in java and if yes then how?
>
> Thanks!
> Will check out the links....
>
> Suma
>
Those links won't answer your query. He's already given you the options in the
post itself.
I like the very first option ...[color=darkred]
For example -
private final static boolean debug=false;
public static void somemethod() {
if (debug) {
bla bla bla
}
//other stuff
}
All you do is change debug to true or false and if set to false, the compiler
gives you
public static void somemethod() {
//other stuff
}
which is exactly your #ifdef (but make sure you include the keyword final so
that the compiler can remove the unreachable code).
--
If you want your spouse to listen and pay strict attention to every
word you say, talk in your sleep.
| |
| Hendrik Maryns 2006-08-22, 8:01 am |
| -----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
ge0rge schreef:
> Suma Shanbhog wrote:
> Those links won't answer your query. He's already given you the options
> in the post itself.
> I like the very first option ...
> For example -
> private final static boolean debug=false;
>
> public static void somemethod() {
> if (debug) {
> bla bla bla
> }
> //other stuff
> }
>
> All you do is change debug to true or false and if set to false, the
> compiler gives you
>
> public static void somemethod() {
> //other stuff
> }
>
> which is exactly your #ifdef (but make sure you include the keyword
> final so that the compiler can remove the unreachable code).
You can check that this works, because e.g. switching debug to true in a
debugger will still cause the debug parts to be skipped (at least in
Eclipse).
H.
- --
Hendrik Maryns
==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)
iD8DBQFE6uWNe+7xMGD3itQRAoMaAJwLdo+pZcbo
vZFgDd7tboRf8kd5RQCfdrAA
ozxiabs5DMj3IlLvCko0VQ0=
=/crV
-----END PGP SIGNATURE-----
| |
| ge0rge 2006-08-22, 8:01 am |
| Hendrik Maryns wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> ge0rge schreef:
>
>
>
> You can check that this works, because e.g. switching debug to true in a
> debugger will still cause the debug parts to be skipped (at least in
> Eclipse).
I hope that you meant '...switching debug to *false*
--
Twenty years of romance make a woman look like a ruin; but twenty years of
marriage make her something like a public building.
-- Oscar Wilde
| |
| ge0rge 2006-08-22, 8:01 am |
| ge0rge wrote:
> Hendrik Maryns wrote:
>
>
>
> I hope that you meant '...switching debug to *false*
>
oops! I think I misunderstood your comment. Set variable debug=false and as you
rightly said - switch debug to true in the eclipse debugger.
hmm...maybe to avoid confusion, it would be better to call the variable some
name other than debug.
--
Some people only open up to tell you that they're closed.
| |
| Hendrik Maryns 2006-08-22, 8:01 am |
| -----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
ge0rge schreef:
> Hendrik Maryns wrote:
>
> I hope that you meant '...switching debug to *false*
I didn’t, but whatever, you get the point, right?
H.
- --
Hendrik Maryns
==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)
iD8DBQFE6vdee+7xMGD3itQRAmCLAJ9KrxIvvzFu
sHvJ71+FDWAODjLQFwCfW+Qc
JGHqKsGVz4l8icEqG4rb1uk=
=MJYW
-----END PGP SIGNATURE-----
|
|
|
|