"Compiler" variables  
Author Message
Oliver Hirschi





PostPosted: 2004-2-2 22:08:00 Top

java-programmer, "Compiler" variables Hi,

Is there in java a possibility to define "compiler"-variables, for
example to except some code parts from the compilation?

If so, how does it work?

--
Oliver Hirschi
http://www.FamilyHirschi.ch

 
Christophe Vanfleteren





PostPosted: 2004-2-2 22:12:00 Top

java-programmer >> "Compiler" variables Oliver Hirschi wrote:

> Hi,
>
> Is there in java a possibility to define "compiler"-variables, for
> example to except some code parts from the compilation?
>
> If so, how does it work?
>

No.
The only thing you can do is conditionally compile code the following way:

public clas AClass {
private static final boolean b = false;

private void someMethod() {
if(b)
//this code will not get in the classfile
//it would never get here anyway
}
}
}
When you change b to true, the code inside the if will get compiled.

But this relies on javac reachability analysis and is not "official".

--
Kind regards,
Christophe Vanfleteren
 
Jon A. Cruz





PostPosted: 2004-2-8 10:20:00 Top

java-programmer >> "Compiler" variables Christophe Vanfleteren wrote:
>
> But this relies on javac reachability analysis and is not "official".
>

Actually, it's present in the Java Language Specification. Specifically,
at the end of ?4.20 covers flag variables, optimizing compilers and the
rationale for it.

Thus, it seems fairly "official" to me.


 
 
Darryl L. Pierce,,,





PostPosted: 2004-2-9 20:57:00 Top

java-programmer >> "Compiler" variables Oliver Hirschi wrote:

> Is there in java a possibility to define "compiler"-variables, for
> example to except some code parts from the compilation?

You can use constant values to do so (we use this in my current project). We
have a patch of code that we only want available on Nokia handsets, so at
the point where that code exists we have:

if(NOKIA_RUNTIME)
{
// the code
}

In order for this to work, we modify the containing source code during our
build process with ANT. If the value for the constant boolean NOKIA_RUNTIME
is true, then the above test isn't included in the compiled code (since
it's *always* going to be true at runtime) and the code within the block is
just included. If the value is false, then the above test is again not
included in the compiled code, but the code within the block isn't included
*either* since, again, the test will never succeed as the value tested is a
constant value and always false.

HTH.

--
Darryl L. Pierce <email***@***.com>
Visit the Infobahn Offramp - <http://mypage.org/mcpierce>
"What do you care what other people think, Mr. Feynman?"
 
 
Dale King





PostPosted: 2004-2-10 2:55:00 Top

java-programmer >> "Compiler" variables "Jon A. Cruz" <email***@***.com> wrote in message
news:email***@***.com...
> Christophe Vanfleteren wrote:
> >
> > But this relies on javac reachability analysis and is not "official".
> >
>
> Actually, it's present in the Java Language Specification. Specifically,
> at the end of ?4.20 covers flag variables, optimizing compilers and the
> rationale for it.
>
> Thus, it seems fairly "official" to me.


But the JLS only allows and does not require a compiler to actually optimize
away the code. It is required to actually detect the situation as it is an
exception the reachability rules so it would be stupid not to eliminate the
code. But once again it is not required to do so.

The specific JLS statement is:

if (false) { x=3; }

... An optimizing compiler may realize that the statement x=3; will never be
executed and may choose to omit the code for that statement from the
generated class file...

Note the word may.

In reality though I think it is pretty safe to bank on this happening.

 
 
pifpafpuf





PostPosted: 2004-2-10 17:32:00 Top

java-programmer >> "Compiler" variables "Oliver Hirschi" <email***@***.com> wrote in message news:<bvllhh$u2kgf$email***@***.com>...

> Is there in java a possibility to define "compiler"-variables, for
> example to except some code parts from the compilation?
>
> If so, how does it work?

You are free to pass your source code through cpp
before handing it to javac:-)

Just kidding,
Harald.
 
 
Oliver Hirschi





PostPosted: 2004-2-10 20:11:00 Top

java-programmer >> "Compiler" variables
"Harald Kirsch" <email***@***.com> schrieb im Newsbeitrag
news:email***@***.com...
> "Oliver Hirschi" <email***@***.com> wrote in message
news:<bvllhh$u2kgf$email***@***.com>...
>
> > Is there in java a possibility to define "compiler"-variables, for
> > example to except some code parts from the compilation?
> >
> > If so, how does it work?
>
> You are free to pass your source code through cpp
> before handing it to javac:-)

You amiss the newsgroup. This entry is better off in a *humor*
newsgroup.

--
Oliver Hirschi
http://www.FamilyHirschi.ch