Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate proxies for generics with upper or lower bounds #93

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/main/java/io/vlingo/xoom/actors/ProxyGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.lang.reflect.WildcardType;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
Expand Down Expand Up @@ -666,6 +667,11 @@ private static Stream<String> typeNameToTypeStream(final Type type) {
Arrays.stream(paramType.getActualTypeArguments()).flatMap(GenericParser::typeNameToTypeStream),
typeNameToTypeStream(paramType.getRawType())
);
} else if (type instanceof WildcardType) {
return Stream.concat(
Arrays.stream(((WildcardType) type).getUpperBounds()),
Arrays.stream(((WildcardType) type).getLowerBounds())
).flatMap(GenericParser::typeNameToTypeStream);
}

return Arrays.stream(type.getTypeName().replaceAll("[<>]", "==").split("=="));
Expand Down
34 changes: 34 additions & 0 deletions src/test/java/io/vlingo/xoom/actors/ProxyGeneratorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,30 @@ public void testThatImportsNestedGenerics() {
assertTrue("RuntimeException is not imported", result.source.contains("import java.lang.RuntimeException;"));
}

@Test
public void testThatGenericsWithUpperBoundsAreImported() {
ProxyGenerator.Result result = proxyGenerator.generateFor(ProtocolWithUpperBoundsInGenerics.class.getCanonicalName());

assertTrue("Completes is not imported", result.source.contains("import io.vlingo.xoom.common.Completes;"));
assertTrue("List is not imported", result.source.contains("import java.util.List;"));
assertTrue("RuntimeException is not imported", result.source.contains("import java.lang.RuntimeException;"));
assertTrue("Queue is not imported", result.source.contains("import java.util.Queue;"));
assertFalse("A generic type is imported", result.source.contains("import A;"));
assertFalse("B generic type is imported", result.source.contains("import B;"));
}

@Test
public void testThatGenericsWithLowerBoundsAreImported() {
ProxyGenerator.Result result = proxyGenerator.generateFor(ProtocolWithLowerBoundsInGenerics.class.getCanonicalName());

assertTrue("Completes is not imported", result.source.contains("import io.vlingo.xoom.common.Completes;"));
assertTrue("List is not imported", result.source.contains("import java.util.List;"));
assertTrue("RuntimeException is not imported", result.source.contains("import java.lang.RuntimeException;"));
assertTrue("Queue is not imported", result.source.contains("import java.util.Queue;"));
assertFalse("A generic type is imported", result.source.contains("import A;"));
assertFalse("B generic type is imported", result.source.contains("import B;"));
}

@Test
public void testThatMethodDefinitionIsValid() {
ProxyGenerator.Result result = proxyGenerator.generateFor(ProtocolWithGenericMethods.class.getCanonicalName());
Expand Down Expand Up @@ -139,6 +163,16 @@ interface ProtocolWithGenerics<A extends RuntimeException, B extends Queue<IOExc
Completes<Queue<List<B>>> otherMethod();
}

interface ProtocolWithUpperBoundsInGenerics<A extends RuntimeException, B extends Queue<IOException>> {
void someMethod(List<? extends Queue<A>> queues);
void otherMethod(List<? extends Queue<B>> queues);
}

interface ProtocolWithLowerBoundsInGenerics<A extends RuntimeException, B extends Queue<IOException>> {
void someMethod(List<? super Queue<A>> queues);
void otherMethod(List<? super Queue<B>> queues);
}

interface ProtocolWithWilcardGenerics<A extends RuntimeException, B extends Queue<?>> {
Completes<Queue<List<A>>> someMethod();
Completes<Queue<List<B>>> otherMethod();
Expand Down