Skip to content

Commit

Permalink
Properly resolve parent interface generics
Browse files Browse the repository at this point in the history
Closes #6
  • Loading branch information
victornoel committed Jul 23, 2018
1 parent e8d7c3b commit a7762e4
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package com.github.victornoel.eo.apt;

import com.google.auto.common.MoreElements;
import com.google.auto.common.MoreTypes;
import com.squareup.javapoet.CodeBlock;
import com.squareup.javapoet.CodeBlock.Builder;
import com.squareup.javapoet.FieldSpec;
Expand Down Expand Up @@ -115,16 +116,14 @@ public TypeSpec typeSpec() throws Exception {
.addStatement("this.$N = $N", field, parameter)
.build()
)
.addMethods(
new DelegatingMethods(this.source, field, this.procenv).get()
)
.addMethods(new DelegatingMethods(field).get())
.build();
}

/**
* Generated delegating methods.
*/
private static final class DelegatingMethods
private final class DelegatingMethods
implements Supplier<Iterable<MethodSpec>> {

/**
Expand All @@ -140,17 +139,14 @@ private static final class DelegatingMethods
/**
* Ctor.
*
* @param element The interface to delegate to
* @param wrapped The field to delegate to
* @param procenv The processing environment
*/
DelegatingMethods(final TypeElement element, final FieldSpec wrapped,
final ProcessingEnvironment procenv) {
DelegatingMethods(final FieldSpec wrapped) {
this(
MoreElements.getLocalAndInheritedMethods(
element,
procenv.getTypeUtils(),
procenv.getElementUtils()
GeneratedEnvelopeTypeSpec.this.source,
GeneratedEnvelopeTypeSpec.this.procenv.getTypeUtils(),
GeneratedEnvelopeTypeSpec.this.procenv.getElementUtils()
),
wrapped
);
Expand Down Expand Up @@ -179,8 +175,7 @@ public Iterable<MethodSpec> get() {
/**
* One generated delegating method.
*/
private static final class DelegatingMethod
implements Supplier<MethodSpec> {
private final class DelegatingMethod implements Supplier<MethodSpec> {

/**
* The method to delegate.
Expand All @@ -207,7 +202,13 @@ private static final class DelegatingMethod
@Override
public MethodSpec get() {
return MethodSpec
.overriding(this.method)
.overriding(
this.method,
MoreTypes.asDeclared(
GeneratedEnvelopeTypeSpec.this.source.asType()
),
GeneratedEnvelopeTypeSpec.this.procenv.getTypeUtils()
)
.addModifiers(Modifier.FINAL)
.addStatement(this.delegation())
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import com.google.testing.compile.Compiler;
import com.google.testing.compile.JavaFileObjects;
import org.assertj.core.api.Assertions;
import org.junit.Ignore;
import org.junit.Test;

/**
Expand Down Expand Up @@ -617,7 +616,6 @@ public void delegatesMethodsOfOverridedSuperInterfaces() {
);
}

@Ignore("https://github.com/victornoel/eo-envelopes/issues/6")
@Test
public void delegatesMethodsOfSuperInterfacesWithConcreteParameters() {
final Compilation compilation = Compiler.javac()
Expand All @@ -634,32 +632,81 @@ public void delegatesMethodsOfSuperInterfacesWithConcreteParameters() {
"import com.github.victornoel.eo.GenerateEnvelope;",
"@GenerateEnvelope",
// @checkstyle LineLengthCheck (1 line)
"public interface AnInterface extends ASuperInterface<String> {",
"public interface AnInterface<B> extends ASuperInterface<B> {",
" void test();",
"}"
)
);
CompilationSubject.assertThat(compilation).succeededWithoutWarnings();
CompilationSubject.assertThat(compilation)
.generatedSourceFile("AnInterfaceEnvelope")
.hasSourceEquivalentTo(
JavaFileObjects.forSourceLines(
"AnInterfaceEnvelope",
"import java.lang.Override;",
// @checkstyle LineLengthCheck (1 line)
"public abstract class AnInterfaceEnvelope<B> implements AnInterface<B> {",
" protected final AnInterface<B> wrapped;",
" public AnInterfaceEnvelope(AnInterface<B> wrapped) {",
" this.wrapped = wrapped;",
" }",
" @Override",
" public final void test(B a) {",
" wrapped.test(a);",
" }",
" @Override",
" public final void test() {",
" wrapped.test();",
" }",
"}"
)
);
}

@Test
public void delegatesMethodsOfInterfacesWithConcreteComplexParameters() {
final Compilation compilation = Compiler.javac()
.withProcessors(new GenerateEnvelopeProcessor())
.compile(
JavaFileObjects.forSourceLines(
"AnInterface",
"import com.github.victornoel.eo.GenerateEnvelope;",
"import java.lang.Iterable;",
"@GenerateEnvelope",
// @checkstyle LineLengthCheck (1 line)
"public interface AnInterface extends Iterable<String> {",
"}"
)
);
CompilationSubject.assertThat(compilation).succeededWithoutWarnings();
CompilationSubject.assertThat(compilation)
.generatedSourceFile("AnInterfaceEnvelope")
.hasSourceEquivalentTo(
JavaFileObjects.forSourceLines(
"AnInterfaceEnvelope",
"import java.lang.Override;",
"import java.lang.String;",
"import java.util.Iterator;",
"import java.util.Spliterator;",
"import java.util.function.Consumer;",
// @checkstyle LineLengthCheck (1 line)
"public abstract class AnInterfaceEnvelope implements AnInterface {",
" protected final AnInterface wrapped;",
" public AnInterfaceEnvelope(AnInterface wrapped) {",
" this.wrapped = wrapped;",
" }",
" @Override",
" public final void test(String a) {",
" wrapped.test(a);",
" public final Iterator<String> iterator() {",
" return wrapped.iterator();",
" }",
" @Override",
" public final void test() {",
" wrapped.test();",
// @checkstyle LineLengthCheck (1 line)
" public final void forEach(Consumer<? super String> arg0) {",
" wrapped.forEach(arg0);",
" }",
" @Override",
" public final Spliterator<String> spliterator() {",
" return wrapped.spliterator();",
" }",
"}"
)
Expand Down

0 comments on commit a7762e4

Please sign in to comment.