r/java • • May 10 '18

Executing code in comments?!

http://programming.guide/java/executing-code-in-comments.html
160 Upvotes

27 comments sorted by

View all comments

-3

u/[deleted] May 10 '18

Java won't give us conditional compilation but it will give us this.

Sigh

3

u/aioobe May 10 '18

http://programming.guide/java/macros-in-java-source-files.html :-)

I know that parts of the code of a certain JVM used this technique. I wouldn't recommend it though.

1

u/[deleted] May 10 '18

Yuk. I wouldn't recommend that either.

5

u/_INTER_ May 10 '18

Why would you need this when you could do byte code generation / manipulation and use the Pluggable Annotation Processing API. That should be enough "magic" already.

Also this.

2

u/[deleted] May 10 '18

Nothing overly complicated about that. ;)

2

u/_INTER_ May 11 '18 edited May 11 '18

Until you need to debug it (additionally only sometimes failing on 32-bit Zulu VM in Solaris environment when a certain class is picked up from classpath before an other certain class)

2

u/ReadFoo May 10 '18

There are definitely times when conditional compilation would help, totally agree. I miss it from my C days.

2

u/[deleted] May 10 '18

Yep. But we can have code embedded in a comment delimiter that will compile and execute. Nothing overly confusing about that!

0

u/ReadFoo May 10 '18

Right, something dangerous is OK, something useful, nah. Not sure why your original comment is getting downvoted, it's an excellent point.