views:

265

answers:

6

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.

+1  A: 

If you want operator overloading, you might consider moving to Scala. Scala code is interoperable with your Java classes and supports operator overloading.

Scharrels
+2  A: 

JFront is a pre-processor for the Java language that allows you to implement operator overloading for your own classes. Sadly, it does not seem to add this ability to standard classes (BigInteger et al. would be a great candidate for this).

Chinmay Kanchi
It would be a good trick if it could do it for the `java.*` API classes, but I assume it uses bytecode or source modification, which isn't an option for `java.*` (without some fancy classloader footwork, anyway).
skaffman
+1  A: 

Another approach is using Groovy libraries since, Groovy already have operator overloading.

https://o24j.dev.java.net/

The current implementation uses Groovy for the overloading. This approach has one drawback: the groovy libraries have to be delivered with the project.

JCasso
A: 

It doesn't allow operator overloading, but considering the links in your question, you may be interested in http://projectlombok.org/features/index.html

Confusion
+2  A: 

You can use byte code injection to replace a type like float with your own type. This can be done after compilation without changing the compiler.

Peter Lawrey
How would that help me achieve what I want? Can you please be more clear?
Red Hyena
you could write all your code using float (for example) with operators etc and use byte code replacement to change the code to use your desired type instead.
Peter Lawrey
+3  A: 

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.

Chinmay Kanchi
Why would you have to modify the language (syntax) to do that? Operator loading is a semantic issue. Shouldn't have to change the parsing stages, other than identifier disambiguation.
xcut
How would you add operator overloading without modifying the syntax? You would either have to add an `operator` keyword or allow `+`, `-` etc as function names. Both require changing what syntax is acceptable.
Chinmay Kanchi
I realise you can do this by providing special methods inside of every class, such as __sum__() etc. Note that I only mentioned parsing in conjunction with writing a pre-processor.
Chinmay Kanchi
This is not exactly the kind of answer I was expecting but I can't see any better answers coming up. So accepting your answer. Thanks!
Red Hyena
OH DEAR LORD, JFRONT. That code is a nightmare... it hurts my mind...
Vuntic