View on GitHub

Java-OO

Java Operator Overloading

Download this project as a .zip file Download this project as a tar.gz file

Java Operator Overloading

Implementation of (Scala-like) Operator Overloading for Java language. Works with standard JavaC compiler, Netbeans IDE, Eclipse IDE, IntelliJ IDEA IDE and any build tools.

Example (see other examples at examples/ dir):

import java.math.*;
import java.util.*;
public class Test {
    public static void main(String[] args) {
        BigInteger  a = BigInteger.valueOf(1), // without OO
                b = 2, // with OO

                c1 = a.negate().add(b.multiply(b)).add(b.divide(a)), // without OO
                c2 = -a + b*b + b/a; // with OO

        if (c1.compareTo(c2)<0 || c1.compareTo(c2)>0) System.out.println("impossible"); // without OO
        if (c1<c2 || c1>c2) System.out.println("impossible"); // with OO

        HashMap<String, String> map = new HashMap<>();
        if (!map.containsKey("qwe")) map.put("qwe", "asd"); // without OO
        if (map["qwe"]==null) map["qwe"] = "asd"; // with OO
    }
}

News

14 May 2013. IntelliJ IDEA IDE plugin v0.2.1 with IDEA Ultimate Edition support.

17 Apr 2013. IntelliJ IDEA IDE plugin v0.2.

26 Nov 2012. Version 0.2 released. New feature: Implicit type conversion via static #valueOf method.

Installation

Eclipse IDE update site

Click in menu: Help - Install New Software. Enter in "Work with" field:

http://amelentev.github.io/eclipse.jdt-oo-site/

Tested on 4.2.1

Netbeans IDE

  1. Add javac-oo-plugin.jar as compile or processor library to Netbeans.
  2. Enable "Annotation Processing in Editor" (Project Properties -> Build -> Compiling).

Tested on 7.2.1

IntelliJ IDEA IDE

  1. Install Java Operator Overloading support plugin: File -> Settings -> Plugins -> Browse repositories. Mirror: idea-oo-plugin.jar)
    For Maven projects installation is done. IDEA should setup everything according to pom.xml.
    For other project types:
  2. Add javac-oo-plugin.jar as compile or processor library.
  3. Enable Annotation Processing: Menu File -> Settings -> Compiler -> Annotation Processing -> Enable annotation processing
  4. Make sure you use javac compiler in Settings -> Compiler -> Use compiler.
    Tested on IDEA 12.1.3 Community and Ultimate Editions.

Android project in IDEA 12

Add javac-oo-plugin.jar to File - Settings - Compiler - Annotation Processors - Processor path

Android Studio (IDEA 13) / Gradle

add to build.gradle:

repositories {
    maven { url 'http://amelentev.github.io/mvnrepo/' }
}
dependencies {
    compile 'java-oo:javac-oo-plugin:0.2'
}

javac, ant, etc

Just add javac-oo-plugin.jar to classpath:

javac -cp javac-oo-plugin.jar <sources>

Demo at examples/compile.sh

Maven

Look at javac-oo-mvndemo/pom.xml

Details

Supported operators (operator to method map):

binary:

| OPERATOR | METHOD    |
------------------------
| +        | add       |
| -        | subtract  |
| *        | multiply  |
| /        | divide    |
| %        | remainder |
| &        | and       |
| |        | or        |
| ^        | xor       |
| <<       | shiftLeft |
| >>       | shiftRight|

unary:

| - | negate |
| ~ | not    |

comparison:

| <, <=, >, >= | compareTo  | example: `a < b` <=> `a.compareTo(b)<0`
`==` and `!=` is not overloadable because it will break things

index:

| []  | get       | `v = lst[i]` <=> `v = lst.get(i)`
| []= | set, put  | `map[s] = v` <=> `map.put(s,v)`,  `lst[i] = v` <=> `lst.set(i,v)`

Implicit type conversion:

if expression has type ExpressionType and there are static method RequredType RequredType#valueOf(ExpressionType)
then expression can be assigned to RequredType. example:
BigInteger a = 1 translates to BigInteger a = BigInteger.valueOf(1)

These methods exists in many java classes (example: BigInteger, BigDecimal) so you can use operators on them "out of the box". Or you can add these methods to your classes to use OO (see examples/Vector.java).