开发者

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.


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.


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/


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


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.


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


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜