Skip to content

A Java annotation processor that generates synchronized decorator classes

License

Notifications You must be signed in to change notification settings

egor-n/SyncGenerator

Repository files navigation

SyncGenerator

Build Status codecov

A Java annotation processor that generates synchronized decorator classes based on this blog post.

Warning

The project is not in any way production material and has been written solely for education purposes.

Usage

Applying the @GenerateSync to the following interface

@GenerateSync
interface Example {
  void method1(String s);
  Boolean method2(Integer i1, Integer i2) throws IOException;
}

will produce the following implementation:

class SyncExample implements Example {
  final Example wrapped;

  public SyncExample(Example wrapped) {
    this.wrapped = wrapped;
  }

  public synchronized void method1(String s) {
    wrapped.method1(s);
  }

  public synchronized Boolean method2(Integer i1, Integer i2) throws IOException {
    return wrapped.method2(i1, i2);
  }
}

@GenerateSync can also be applied to non-final and non-static classes. Given the following class

@GenerateSync
abstract class Example {
  abstract String s(Boolean b, Integer i);

  final void f() { }

  Double d() throws IOException {
        return null;
    }
}

it will generate the synchronized version of it:

class SyncExample extends Example {
  final Example wrapped;

  public SyncExample(Example wrapped) {
    this.wrapped = wrapped;
  }

  synchronized String s(Boolean b, Integer i) {
    return wrapped.s(b, i);
  }

  synchronized Double d() throws IOException {
    return wrapped.d();
  }
}

Download

To use SyncGenerator add these dependencies:

compileOnly 'com.github.egor-n:syncgenerator-library:1.0.2'
apt 'com.github.egor-n:syncgenerator-compiler:1.0.2'

Snapshots of the development version are available in Sonatype's snapshots repository.

TODO

  • add support for inner static classes with the same name
  • add support for inner static classes (#1)
  • figure out how to test this painlessly

About

A Java annotation processor that generates synchronized decorator classes

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published