Skip to content

Commit

Permalink
revamp/rearrange the autoimpl code, add delegate generator.
Browse files Browse the repository at this point in the history
  • Loading branch information
bdleitner committed Mar 18, 2017
1 parent ff2a2b6 commit 205c650
Show file tree
Hide file tree
Showing 59 changed files with 704 additions and 419 deletions.
105 changes: 12 additions & 93 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Auto Impl
# Auto
*Automatic generation of implementation classes for Java*
**Benjamin Leitner**
Inspired by the [Google Auto](https://github.com/google/auto) project.
Expand All @@ -7,97 +7,16 @@ Inspired by the [Google Auto](https://github.com/google/auto) project.
Writing default implementation classes is annoying and tedious. To quote
[Google Auto](https://github.com/google/auto), sounds like a job for robots.

## Usage
If you have an interface or abstract class for which you'd like to create a default implementation,
simply add the `@AutoImpl` annotation and let the annotation processor do the rest.
This project makes available two annotations, with annotation processors for
generating default implementations of Java interfaces.

An class named `Auto_[ClassName]_Impl` will be created in which all methods are implemented.
If your annotated interface/abstract class is an inner class, the name of the generated class will
be `Auto_[OuterClass]_[InnerClass]_Impl`, with as many segments as your class has nesting layers.

If you annotate an abstract class that already has implementations for some methods, those
implementations are kept.

If you annotate an abstract class that has non-default constructors, matching constructors are
created on the implementation class which simply delegate to your abstract class.

The options for behavior of the implemented methods are reflected in the
`ImplOption` enum.

* `THROW_EXCEPTION` - the default. The implemented method throws an`UnsupportedOperationException`.
* `RETURN_DEFAULT_VALUE` - the method returns a default value.
* 0 for all numeric types.
* false for booleans.
* "" for Strings
* null for everything else.
* `USE_PARENT` - defers to the next higher level.

The top level control is `AutoImpl.value()`, which defaults to `THROW_EXCEPTION`
Finer-grained controls are supported on `AutoImpl` itself
for each category of return type:

* numeric - `AutoImpl.numericImpl()`
* boolean - `AutoImpl.booleanImpl()`
* String - `AutoImpl.stringImpl()`
* void - `AutoImpl.voidImpl()`
* Everything else - `AutoImpl.objectImpl()`

These can all be set independently. Any that are not set (or that are set to the default `USE_PARENT`
will defer to `AutoImpl.value()`.

Finally, individual methods can be annotated with `MethodImpl`.
`MethodImpl` has a single `value` parameter that, if present, overrides the default settings
from `AutoImpl`.

## Examples / Use Cases
#### Optional Methods
You've written an interface for which you expect some methods to be frequently implemented with a
no-op (e.g. various listeners).

@AutoImpl(ImplOptions.RETURN_DEFAULT_VALUE)
public interface SomeInterface {
void firstMethod(...);
void secondMethod(...);
void thirdMethod(...);
...
void umpteenthMethod(...);
}

Thanks to the `@AutoImpl` annotation, you can write partial implementations:

public class PartialImpl extends Auto_SomeInterface_Impl { // implements SomeInterface
void thirdMethod(...) {
... // implementation
}

void seventhMethod(...) {
... // implementation
}
}

and all other methods will simply do nothing when called.

#### Testing
Suppose you're testing your class's interaction with a library interface for which generating an
instance is nontrivial. One approach to solve this is to mock the interface with something like
*EasyMock* or *Mockito*. But if you want a partial fake or want the implemented methods to actually
perform some logic, a default implementation class may be simpler:

public class SomeTestClass {
@AutoImpl
abstract static class MyTestImplementation implements LibraryInterface {
void doSomething(...) {
// implementation code
}
}
@Test
public void testDoesSomething() {
LibraryInterface impl = new Auto_SomeTestClass_MyTestImplementation_Impl();
... // use this instance for your test.
}
}
Note: this will throw exceptions if any other method is called. To avoid that,
use `AutoImpl(ImplOptions.RETURN_DEFAULT_VALUE)` or otherwise override the behavior as described
above.
## [@AutoImpl](impl.md)
Provides a default implementation that throws exception or returns default
values for each method. Can be applied to an abstract class so as to only
provide default implementations for abstract methods.

## [@AutoDelegate](delegate.md)
Provides an implementation of an abstract class that inherits from a single interface
or nontrivial superclass. The implementation takes an instance of that
interface on construction and implements all abstract methods to delegate
to that instance. Methods implemented on the abstract class are not overridden.
6 changes: 6 additions & 0 deletions auto/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
dependencies {
testCompile 'org.mockito:mockito-all:1.10.19'
testCompile project(':delegate_annotation_processor')
testApt project(':impl_annotation_processor')
testApt project(':delegate_annotation_processor')
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
com.bdl.auto.impl.processor.AutoImplProcessor
74 changes: 74 additions & 0 deletions auto/src/test/java/com/bdl/auto/AutoDelegateTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package com.bdl.auto;

import com.bdl.annotation.processing.model.ClassMetadata;
import com.bdl.auto.delegate.AutoDelegate;
import com.bdl.auto.delegate.processor.AutoDelegateWriter;
import com.google.testing.compile.CompilationRule;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

import javax.lang.model.element.TypeElement;
import javax.lang.model.util.Elements;
import java.io.PrintWriter;

import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;

@RunWith(JUnit4.class)
public class AutoDelegateTest {

@Rule
public final CompilationRule compilation = new CompilationRule();

private Elements elements;

@Before
public void before() {
elements = compilation.getElements();
}

@Test
public void testAutoDelegate() throws Exception {
TypeElement element = elements.getTypeElement("com.bdl.auto.AutoDelegateTest.DelegatingTestInterface");
ClassMetadata metadata = ClassMetadata.fromElement(element);

AutoDelegateWriter writer = new AutoDelegateWriter(
s-> new PrintWriter(System.out),
System.out::println);

writer.write(metadata);
TestInterface mock = mock(TestInterface.class);
TestInterface impl = new Auto_AutoDelegateTest_DelegatingTestInterface_Delegate(mock);

assertThat(impl.bar(2)).isEqualTo(3);
impl.foo();
verify(mock, never()).bar(anyInt());
verify(mock).foo();
}

interface TestInterface {
void foo();

int bar(int baz);
}

@AutoDelegate
abstract static class DelegatingTestInterface implements TestInterface {
protected final TestInterface delegate;

protected DelegatingTestInterface(TestInterface delegate) {
this.delegate = delegate;
}

@Override
public int bar(int baz) {
return baz + 1;
}
}
}
37 changes: 37 additions & 0 deletions auto/src/test/java/com/bdl/auto/AutoImplTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.bdl.auto;

import com.bdl.auto.impl.AutoImpl;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

import static org.junit.Assert.fail;

@RunWith(JUnit4.class)
public class AutoImplTest {

@Test
public void testAutoImpl() {
TestInterface impl = new Auto_AutoImplTest_TestInterface_Impl();
try {
impl.foo();
fail();
} catch (UnsupportedOperationException ex) {
// expected
}

try {
impl.bar(15);
fail();
} catch (UnsupportedOperationException ex) {
// expected
}
}

@AutoImpl
interface TestInterface {
int foo();

int bar(int baz);
}
}
92 changes: 48 additions & 44 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,59 +9,63 @@ buildscript {
}
}

group 'com.bdl.auto'
version '1.0-SNAPSHOT'

repositories {
mavenLocal()
mavenCentral()
jcenter()
maven { url "https://jitpack.io" }
allprojects {
group 'com.bdl.auto'
version '1.0-SNAPSHOT'
}

apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'maven'
apply plugin: 'net.ltgt.apt'
subprojects {

sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories {
mavenLocal()
mavenCentral()
jcenter()
maven { url "https://jitpack.io" }
}

ext {
JUNIT_VERSION = '4.2'
GOOGLE_TRUTH_VERSION = '0.28'
}
apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'maven'
apply plugin: 'net.ltgt.apt'

dependencies {
compile 'com.google.auto.value:auto-value:1.3'
compile 'com.google.code.findbugs:jsr305:latest.release'
compile 'com.google.guava:guava:19.0'
compile 'com.bdl.annotation.processing:common-annotation-processing:1.0-SNAPSHOT'
sourceCompatibility = 1.8
targetCompatibility = 1.8

testCompile "com.google.truth:truth:${GOOGLE_TRUTH_VERSION}"
testCompile "junit:junit:${JUNIT_VERSION}"
testCompile 'com.google.testing.compile:compile-testing:0.5'
ext {
JUNIT_VERSION = '4.2'
GOOGLE_TRUTH_VERSION = '0.32'
}

apt 'com.google.auto.value:auto-value:1.3'
testApt 'com.google.auto.value:auto-value:1.3'
testApt 'com.google.guava:guava:19.0'
}
dependencies {
compile 'com.google.auto.value:auto-value:1.4'
compile 'com.google.code.findbugs:jsr305:latest.release'
compile 'com.google.guava:guava:19.0'

javadoc {
failOnError false
}
testCompile "com.google.truth:truth:${GOOGLE_TRUTH_VERSION}"
testCompile "junit:junit:${JUNIT_VERSION}"
testCompile 'com.google.testing.compile:compile-testing:0.5'

task sourcesJar(type: Jar, dependsOn: classes) {
classifier = 'sources'
from sourceSets.main.allSource
}
apt 'com.google.auto.value:auto-value:1.4'
testApt 'com.google.auto.value:auto-value:1.4'
testApt 'com.google.guava:guava:19.0'
}

task javadocJar(type: Jar, dependsOn: javadoc) {
classifier = 'javadoc'
from javadoc.destinationDir
}
javadoc {
failOnError false
}

task sourcesJar(type: Jar, dependsOn: classes) {
classifier = 'sources'
from sourceSets.main.allSource
}

task javadocJar(type: Jar, dependsOn: javadoc) {
classifier = 'javadoc'
from javadoc.destinationDir
}

artifacts {
archives sourcesJar
archives javadocJar
artifacts {
archives sourcesJar
archives javadocJar
}
}
45 changes: 45 additions & 0 deletions delegate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# @AutoDelegate

## Usage
If you have an interface (or abstract class) for which you mostly want to delegate
to an existing instance, with a few overrides, you can use the `@AutoDelegate` annotation.

The requirements to mark a class as `@AutoDelegate` are:
* The class must be abstract and must either implement a single interface or
inherit from an abstract class.
* The class must contain a `protected final` variable of the same type as the
inherited interface and named `delegate`.
* All constructors on the class must take an instance of the inherited interface as
the first parameter.

For example:

@AutoDelegate
public abstract class DelegatingFoo implements Foo {
protected final Foo delegate;
public DelegatingFoo(Foo delegate) {
this.delegate = delegate;
}
/** Assume this is a method on Foo. */
@Override
public int fooMethod1(String arg) {
// ... implementation
}
}

An implementation class named `Auto_[ClassName]_Delegate` will be created in which all remaining
methods are implemented. If your annotated interface/abstract class is an inner class, the name of
the generated class will be `Auto_[OuterClass]_[InnerClass]_Delegate`, with as many segments as your
class has nesting layers.

If you annotate an abstract class that already has implementations for some methods, those
implementations are kept.

If you annotate an abstract class that has non-default constructors, matching constructors are
created on the implementation class which simply delegate to your abstract class.

The resulting functionality is similar to a *Spy* from a mocking environment like
*EasyMock* or *Mockito*, but if you want your overrides to have some state, or just
prefer concrete classes to mocks, this may come in handy
4 changes: 4 additions & 0 deletions delegate_annotation_processor/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
dependencies {
compile 'com.github.bdleitner:common-annotation-processing:0.4.1'
compile(project(':auto')) { transitive = false }
}
Loading

0 comments on commit 205c650

Please sign in to comment.