Operator Overloading Hack in Java, or o24j
In one of my past projects there was a need to perform dynamic computations on Java-Objects. The user was able to input some complex formulas, which had to be interpreted.
I could write of course a custom language for this purpose, but because of my laziness I just passed the objects as well as the formula to groovy, which interpreted and executed the formula for me.
During the review of the new JSR-310 (the Date And Time API for Java) and especially the fluent api, I got the next idea: simulation of the operator overloading for Java-Objects.
So I wanted to do something like this:
BigDecimal two = new BigDecimal(2);
BigDecimal three = new BigDecimal(3);
BigDecimal minusOne = Overload.eval("p1-p2", two,three);
assertEquals(-1,minusOne.intValue());
I materialized the idea in the o24j opensource project. It already works - the sources are checked in.
I use groovy internally because it already provides operator overloading. I would like to do the same with JavaScript.
Then no additional libraries would be required.
o24j works with JSR-310 as well :-).
with annotation, there is a most easy way to add
operator overload support into java. Look at IDEA @NotNull
compiler support.
But why not?
Sun is stupid?
God knows.
All we need do is that:
class Number {
@operator("+")
public Number add(Number num){...}
}
Number x=3;
Number y=4;
Number z = x+y;
God print GouGu numbers=25;:)
It is a peace cake.
Why not?
It seems to like assemble, just take "+" as a method invocation.
Why not?
Sadan knows!
Posted by qinxian on July 04, 2007 at 08:51 AM CEST #
I also evaluated Annotations for this purpose (not exactly oo but similar stuff...) but most interesting things are currently not possible without some compiler extentions.
I like the idea adding "pseudo" oo without any specification changes.
2 classes for a java.net project - thats a record ;)
Posted by Michael Bien on July 04, 2007 at 03:23 PM CEST #
qinxian,
1. But you have to provide a kind of pre/postprocessing. It is a IntelliJ specific solution - isn't it? It only works in IntelliJ...
2. The o24j solution works without changing the origin class - also with BigDecimal
3. Who is Sadan? :-)
regards && thanks,
adam
Posted by Adam Bien on July 04, 2007 at 09:00 PM CEST #
Michael,
it's only one class and one interface. But the interface is optional - I don't like over-engineering :-).
The cool stuff: it even works with simple numbers :-)
regards && thanks,
adam
Posted by Adam Bien on July 04, 2007 at 09:02 PM CEST #
Hi Adam,
At first I think you are right.
I have to claim that my says not object 2 orient to you and o24j.
I just do say that java self and operator.
And I not agree with your solution in most cases:
Java not most perfect, But sometimes we must do something to enhance its defect, such o24j.
What a pain thing.
I think we need real o22(objorient-overoperator), isn't it?
But Who give us a real java.
God says, yourself.
We say, ourself.
I say, myself.
Sadan, :) not remember spelling, it's just evil God.:)
Nice to talk with you.
Regards,
Qinxian
Posted by qinxian on July 05, 2007 at 09:29 AM CEST #
Qinxian,
If you are saying native operator overloading would be nice in Java, it's also my opinion. But we do not have it - and o24j was the simplest, smallest not intrusive solution which worked in my projects :-).
But even if we had operator overloading, there would be no way to evaluate the computations at runtime - java is a static language...
regards,
adam
Posted by Adam Bien on July 05, 2007 at 11:04 AM CEST #
Hi adam,
Yes, agree.
Though java is a static, But we can add some static
functionalities to static java, for example, MOF.
pseudo code like this:
Class<Number> numberClass=?;
numberClass.assertAdd(SomeKindOfOperator("+"));
Number x, y=1,2;
Number z=x+y;//may throw NoSuchMethodError;
//maybe compile-time warning, no operator.
//if no exception, z must be 3.
//if God error, z maybe not be 3:-)
Ok, back to o24j enviroments, there is no solution for disable producting compile error.
So, bytecode waver-like compiler, such as aspectj, maybe needed.
wa-oo, again back to another compiler:)
Yes, No way to do at now, by normal or easy kind.
Always need some style of compiler.
You are right again:)
But I am right too:)
Because we need a new Java and a new Java compiler.
maybe it should not be called as Java,
Be J2, or O^2J.:)
hei, adam, I back to this blog again, In a dynamic senerio, I think the above codes can be applied.
We just do a class generator, by using tool like asm, for enhancement class.
And the class generator, just perform as "another compiler".en, Yes it is.
It's a flark thought, I must think it about by details.
Good night!
(Maybe you are sleeping,
I'm working in orient(with out object):))
Regards,
Qinxian
Posted by qinxian on July 06, 2007 at 09:19 AM CEST #
Interesting. However, why not just use Scala instead? It's amazingly interoperable with Java and much more interesting as a language than Groovy (imho).
Posted by Marcus on July 06, 2007 at 10:25 PM CEST #
Marcus,
scala is also interesting - but has also be shipped with o24j. JavaScript, or some tricks with reflection would be better - no additional libraries would be needed then.
But if you like to experiment with scala - go ahead, although o24j works, it is more an experiment :-),
regards,
adam
Posted by Adam Bien on July 07, 2007 at 05:47 PM CEST #
Good Morning Qinxian,
thank you for your comment. If you like to experiment with o24j - go ahead. Regardless what we are doing I have only one restriction: the solution should be as easy as possible to use, understand, deploy and maintain.
regards,
adam
Posted by Adam Bien on July 07, 2007 at 05:50 PM CEST #
The JGA library has supported exactly this usage for a few years, via the included JFXG parser. BigDecimal is generally assumed when numeric literals with a decimal point are entered in expressions.
Posted by David Hall on July 12, 2007 at 06:58 PM CEST #