Skip to content

Latest commit

 

History

History
126 lines (101 loc) · 3.43 KB

README.md

File metadata and controls

126 lines (101 loc) · 3.43 KB

JOps - Operator Overloading for Java

This library allows to add operator overloading to your Java projects. This feature is normally not supported, however by creating an annotation processor it is possible to access the internals of the javac compiler (which is itself written in Java) and modify it to do our bidding. Doing so requires to access private code not part of the public Java API. As such, you must consider that it is experimental and that it may break unexpectedly at any point if Oracle decides to rewrite parts of the compiler implementation. Therefore it is more a fun experiment than a serious tool to build systems on.

Usage

You can add operators to your own classes :

public class Complex {
    public double re, im;

    public Complex() {}

    public Complex( double re, double im ) {
        this.re = re; this.im = im;
    }

    public void add( Complex other ) {
        re += other.re; im += other.im;
    }

    @Override
    public String toString() {
        return re + " + " + im + "i";
    }

    @Operator( "+" )
    public static Complex add( Complex a, Complex b ) {
        return new Complex( a.re + b.re, a.im + b.im );
    }

    @Operator( "*" )
    public static Complex mul( Complex a, Complex b ) {
        return new Complex( a.re * b.re - a.im * b.im, a.re * b.im + a.im * b.re );
    }
    
    public static void main( String[] args ) {
        Complex a = new Complex( 1, 2 );
        Complex b = new Complex( 3, 4 );
        Complex c = new Complex( 2, 0 );
        System.out.println( a + b * c );
    }
    
}

Result : 7 + 10i

You can also extend existing classes :

    @Operator( "+" )
    public static List concat( List a, List b ) {
        List res = new ArrayList( a.size() + b.size() );
        res.addAll( a );
        res.addAll( b );
        return res;
    }

    public static void main( String[] args ) {
        List<String> cola = Arrays.asList( "hello", "," );
        List<String> colb = Arrays.asList( "world", "!" );
        System.out.println( String.join( "", cola + colb ) );
    }

Result : hello,world!

It is possible to mix object and primitive types :

    @Operator( "*" )
    public static String mulString( String a, int b ) {
        String res = "";
        for( int i = 0; i < b; i++ ) {
            res += a;
        }
        return res;
    }

    public static void main( String[] args ) {
        System.out.println( "ab" * 3 );
    }

Result : ababab

You can find some more samples in the test directory.

Compatibility

This project is a javac hack and consequently it only works when compiling your code with javac. It will not work with ecj, Eclipse's built-in compiler. The library has been tested to work with the following versions of the Oracle JDK :

JOps JDK
jops-0.1 1.8.0_45-b14

Downloads

With Maven

In your pom.xml :

<repositories>
  <repository>
    <id>bidouille.org</id>
    <url>http://archiva.bidouille.org/archiva/repository/releases/</url>
  </repository>
</repositories>

Then in your <dependencies> section :

<dependency>
  <groupId>org.bidouille.jops</groupId>
  <artifactId>jops</artifactId>
  <version>0.1</version>
</dependency>

Without Maven

Grab the jar from the repository at this location : http://archiva.bidouille.org/archiva/repository/releases/org/bidouille/jops/jops/0.1/ and put it in your classpath.