Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Operator Overloading Plugin for Java

I have seen some nice Java compiler hacks wherein you can replace assertions by throw-exception statements, you can have auto-generated properties for fields and so on...

I was just wondering...

Is it possible to hack Java compiler so that it supports Operator Overloading?

EDIT :

It's not that I am missing operator overloading in Java or something. I just want to know whether it is possible to implement it using the hacks mentioned in the links I have given above. So please do not suggest me to switch to some other language like Scala (which I am already learning anyway! :D ).

EDIT :

Please just tell me whether it is possible to do it using JSR 269 or something like that. :|

Thanks.

like image 640
Red Hyena Avatar asked Jan 06 '10 11:01

Red Hyena


2 Answers

In response to the edited question, the only way you can implement true operator overloading in Java is to add it to the language. This means that you either have to change the compiler to compile your code or write a preprocessor that converts your code to standard Java. For the former, you can download a nearly complete FOSS java implementation from either GNU or the OpenJDK project. For the latter, you can either write your own parser using things like JavaCC etc. (examples are on this page) or you can use JFront as a base to add your own syntax.

like image 140
Chinmay Kanchi Avatar answered Oct 03 '22 22:10

Chinmay Kanchi


It is possible to implement Java operator overloading using JSR 269 with some hacks. Look at java-oo plugins for javac and Eclipse JDT:

http://amelentev.github.com/java-oo/

like image 21
mart Avatar answered Oct 03 '22 23:10

mart