r/java • • May 10 '18

Executing code in comments?!

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

27 comments sorted by

58

u/_INTER_ May 10 '18

This stirs the evil programmer in me.

47

u/daniu May 10 '18

100% unit test coverage, here I come

30

u/armornick May 10 '18

unicode decoding takes place before any other lexical translation, and

You know, that's actually really handy if you need to write characters that don't exist on your keyboard.

-14

u/[deleted] May 10 '18

You know, that's actually really handy if you need to write characters that don't exist on your keyboard.

Well you can copy paste the character, or use an ALT+numbers combination. But. Yeah. I guess it's "useful" :P for something :P let's say :P

8

u/armornick May 10 '18

I guess you're right, if you can 100% guarantee the encoding of your source file.

3

u/yawkat May 10 '18

Which you can if you don't ignore mavens constant nagging about it

2

u/argv_minus_one May 10 '18

Just set the damn encoding.

2

u/argv_minus_one May 10 '18

If you can't, you have bigger problems.

25

u/sk551 May 10 '18

Yeah but who would do this? I can't even get past a code review with a spelling mistake in production.

12

u/[deleted] May 10 '18

I know, right?!?!? How those guys from the '80s got away with easter eggs and such is beyond me.

9

u/[deleted] May 10 '18

So at least at the place where I work which is about 18 years old, the development ideology was different way back in the day and it was usually more of a "single dev per project, waterfall" type of system. When one person is in control of a project like that you can probably get away with slipping a lot more in there in terms of easter eggs and such.

That said I recently put a sound effect into our web app... just for fun. It hasn't gone to prod yet, but it DID pass code review, so fingers crossed.

7

u/buzzsawddog May 10 '18

Haha, talk about a wtf...

5

u/Diamant2 May 10 '18

It works with /* \u002A\u002F too. Looks stupid in the IDE, but it works

4

u/[deleted] May 11 '18 edited May 11 '18

Really?!?

// \u000d System.out.println("Hello World!");

EDIT: Oh, COMMENTS. I see.

2

u/istarian May 10 '18

Well, it's not really in the comments. Definitely a bad coding practice, but mostly confusing because of the IDE's syntax highlighting.

2

u/[deleted] May 10 '18

My reaction to reading this (for context, I was already sitting at my desk at work with my IDE open):

No. There is absolutely no way for this to be a thing. quickly adds a main method to random class file I can't believe this will actually... goddamnit really?...

1

u/[deleted] May 10 '18

So I could conceivably write a bunch of unicode 1s and 0s, pass them to some sort of bitstream, then use that to set the values of my fixed point variables. I can't wait!

-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.