views:

149

answers:

3

Is there something in Java that would allow for structuring the source code as it is done in C# with the pre-processor directives

#region

and

#endregion

?

+7  A: 

No, there is nothing equivalent.

Code folding is an IDE feature, not a language feature.

Eclipse has code folding for classes and members when Java files are loaded in it. It may be extended (for example) to add code folding on carefully crafted comment lines.

Oded
Pitty, anyway thanks. I am using idea (full version). Any equivalent to eclipse's folding?
@user268098 - I never used `idea`, so I can't help with that. Read the help file...
Oded
I highly recommend Eclipse for Java development. It allows folding of any methods or documentation blocks. AFAIK it doesn't allow arbitrary regions to be defined, however.
Erick Robertson
@use268098: Yes, IDEA has code folding. I think it's on by default, but configuration is at IDE Settings -> Editor -> Code Folding.
ColinD
@Oded: thanks again ... I feel ashamed, I should have considered RTFM myself ;-)
@Erick Robertson: IDEA is a seriously awesome IDE, for the record.
ColinD
@ColinD, @Erick Robertson: Without going too much off the topic, but IDEA really *is* awesome, especially when you use Spring. There is a good reason why I prefer to pay some hundreds of dollar for a Java IDE from my private money.
Is **arbitrary** code folding really necessary? I'm not convinced.
Kelly French
@Kelly French - necessary? No. Nice to have? Sometimes. Easy to abuse? Definitely.
Oded
Nitpick: although the ability to collapse code is indeed an IDE feature, it is nonetheless a language feature that it allows `#region` and `#endregion` and that the compiler enforces the existence of matching pairs (C# language specification, §2.5.6 Region directives).
Timwi
@Timwi agreed, I've had builds fail from unmatched regions. You would think that the compiler would ignore both terms and save the trouble.
Kelly French
@Kelly French: Let me put it this way: I found it extremely useful for me to fold regions ... and I really miss it in Java ...
I'm not knocking IDEA. I've never even heard of it, but I do not do Spring development. I posted my comment here before @user268098 edited his first comment to add that he was using IDEA.
Erick Robertson
+3  A: 

No, there's no built-in solution to that.

However there are some Java IDE's which support folding for certain comment-patterns. And of course all IDE's support folding the code by classes, methods etc.

Gamlor
+1  A: 

Not as such. Bear in mind that these preprocessor directives are more for VS's benefit than they are a part of the C# or VB language spec. Preprocessor directives in Java would be the IDE's business.

KeithS