-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
revamp/rearrange the autoimpl code, add delegate generator.
- Loading branch information
Showing
59 changed files
with
704 additions
and
419 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions
1
auto/src/main/resources/META-INF/services/javax.annotation.processing.Processor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
com.bdl.auto.impl.processor.AutoImplProcessor |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } | ||
} |
Oops, something went wrong.