The Artima Developer Community
Sponsored Link

Java Answers Forum
Java Capability

13 replies on 1 page. Most recent reply: Dec 10, 2004 2:04 AM by Vincent O'Sullivan

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 13 replies on 1 page
Vikram V Hegde

Posts: 1
Nickname: vikram81
Registered: Aug, 2004

Java Capability Posted: Dec 2, 2004 11:30 PM
Reply to this message Reply
Advertisement
I have a question about java Please suggest any relavent answers,

Is there any option in java, to neglect a block of java code while complilation so that it(that neglected block of code) would not be included in generate Byte code/Class file? [ NOTE: Please ignore the commentting operator (/**/ or //) as solution ]

Thanks in Advance,
Vikram Vishwanath


Amol Brid

Posts: 43
Nickname: amolbrid
Registered: Feb, 2004

Re: Java Capability Posted: Dec 3, 2004 2:09 AM
Reply to this message Reply
Hi Vikram,

This is a strange requirement.
Why u want to do this?

Amol Brid.

toure

Posts: 2
Nickname: petert17
Registered: Nov, 2004

Re: Java Capability Posted: Dec 4, 2004 12:56 PM
Reply to this message Reply
please let me know when you got the solution
that's would fascinating!

stephen handley

Posts: 3
Nickname: one2one3
Registered: Dec, 2004

Re: Java Capability Posted: Dec 4, 2004 10:24 PM
Reply to this message Reply
would a conditionally returned inner class work?
public static YourClass classReturner (boolean doit) {
if (doit)
return new YourClass() {
//your class body with desired non compiled block of code
};
else
return null;
}

i'm guessing even if doit was declared as
final boolean doit = FALSE;
it would still compile? i'm not sure though.

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Java Capability Posted: Dec 5, 2004 7:02 PM
Reply to this message Reply
Sure; just write that block of code in the Whitespace programming language. See http://compsoc.dur.ac.uk/whitespace/

Vincent O'Sullivan

Posts: 724
Nickname: vincent
Registered: Nov, 2002

Re: Java Capability Posted: Dec 6, 2004 4:35 AM
Reply to this message Reply
Yes. If the condition in a conditional statement can be resolved at compile time then the 'false' part of the statement will not be compiled. For instance:
final String state = "live";
 
if {state == "live"}
{
    // Do live stuff.
    // e.g. Connect to the live database.
}
else
{
    // Do test stuff.
    // e.g. Connect to the test database.
}
This would compile to (the bytecode equivalent of):
String state = "live";
 
// Do live stuff.
// e.g. Connect to the live database.
Vince.

sandeep

Posts: 2
Nickname: sandeepgrp
Registered: Dec, 2004

Re: Java Capability Posted: Dec 6, 2004 8:46 PM
Reply to this message Reply
>final String state = "live";

>if {state == "live"}
>{
> // Do live stuff.
> // e.g. Connect to the live database.
>}
>else
>{
> // Do test stuff.
> // e.g. Connect to the test database.
>}

>This would compile to (the bytecode equivalent of):
>String state = "live";

>// Do live stuff.
>// e.g. Connect to the live database.



but javac compile all the code
as if we make an error in else part it give error at that point so java compile all the code (except commented)

Vincent O'Sullivan

Posts: 724
Nickname: vincent
Registered: Nov, 2002

Re: Java Capability Posted: Dec 7, 2004 2:36 AM
Reply to this message Reply
That doesn't really prove anything. You would not expect code that contains errors to compile...and it doesn't.

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: Java Capability Posted: Dec 7, 2004 5:36 AM
Reply to this message Reply
The question was not about the execution, but about how to write something without getting it compiled.


As far as I know that just isn't possible, as long as you don't use the comment option.

Vincent O'Sullivan

Posts: 724
Nickname: vincent
Registered: Nov, 2002

Re: Java Capability Posted: Dec 8, 2004 12:17 AM
Reply to this message Reply
> The question was not about the execution, but about how to
> write something without getting it compiled.

I see your point.

Nevertheless, the code isn't compiled (into the class file) but, because the 'unwanted' code is not a comment, the compiler still needs to check it for semantic correctness. If errors are found the compiler can no longer assume it has a block of unwanted code. It just sees the errors.

Either way the unwanted code does not make it into the class file.

V.

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: Java Capability Posted: Dec 9, 2004 12:41 AM
Reply to this message Reply
Unreachable code doesn't get stored into the .class file?

I didn't know that.

Vincent O'Sullivan

Posts: 724
Nickname: vincent
Registered: Nov, 2002

Re: Java Capability Posted: Dec 9, 2004 2:12 AM
Reply to this message Reply
> Unreachable code doesn't get stored into the .class file?
>
> I didn't know that.

Nor did I. That'll be why that wasn't what I said.

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Java Capability Posted: Dec 9, 2004 11:18 AM
Reply to this message Reply
But that is the correct conclusion, isn't it? If the compiler can determine at compile time that a chunk of code is unreachable, then it won't appear in the class file.

Vincent O'Sullivan

Posts: 724
Nickname: vincent
Registered: Nov, 2002

Re: Java Capability Posted: Dec 10, 2004 2:04 AM
Reply to this message Reply
> But that is the correct conclusion, isn't it?

Maybe, maybe not. It's a reasonable hypothesis but not necessarily a correct conclusion since it involves making a generalisation from a specific case.

For myself, I know that it happens if an if statemant can be resolved at compile time. I don't know if the same happens for all unreachable code.

Flat View: This topic has 13 replies on 1 page
Topic: Creating a conversion Swing Applet Previous Topic   Next Topic Topic: Anybody know the Answer

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use