diff --git a/javapoet/src/main/java/com/palantir/javapoet/MethodSpec.java b/javapoet/src/main/java/com/palantir/javapoet/MethodSpec.java index 21306d5..5485cca 100644 --- a/javapoet/src/main/java/com/palantir/javapoet/MethodSpec.java +++ b/javapoet/src/main/java/com/palantir/javapoet/MethodSpec.java @@ -35,6 +35,7 @@ import javax.lang.model.element.TypeParameterElement; import javax.lang.model.type.DeclaredType; import javax.lang.model.type.ExecutableType; +import javax.lang.model.type.TypeKind; import javax.lang.model.type.TypeMirror; import javax.lang.model.type.TypeVariable; import javax.lang.model.util.Types; @@ -295,6 +296,7 @@ public static Builder overriding(ExecutableElement method) { */ public static Builder overriding(ExecutableElement method, DeclaredType enclosing, Types types) { ExecutableType executableType = (ExecutableType) types.asMemberOf(enclosing, method); + List resolvedTypeVariables = executableType.getTypeVariables(); List resolvedParameterTypes = executableType.getParameterTypes(); List resolvedThrownTypes = executableType.getThrownTypes(); TypeMirror resolvedReturnType = executableType.getReturnType(); @@ -307,6 +309,18 @@ public static Builder overriding(ExecutableElement method, DeclaredType enclosin builder.parameters.set( i, parameter.toBuilder(type, parameter.name()).build()); } + for (int i = 0, size = builder.typeVariables.size(); i < size; i++) { + String name = + resolvedTypeVariables.get(i).asElement().getSimpleName().toString(); + List bounds = new ArrayList<>(); + if (resolvedTypeVariables.get(i).getUpperBound().getKind() != TypeKind.NULL) { + bounds.add(TypeName.get(resolvedTypeVariables.get(i).getUpperBound())); + } + if (resolvedTypeVariables.get(i).getLowerBound().getKind() != TypeKind.NULL) { + bounds.add(TypeName.get(resolvedTypeVariables.get(i).getLowerBound())); + } + builder.typeVariables.set(i, TypeVariableName.get(name, bounds.toArray(new TypeName[0]))); + } builder.exceptions.clear(); for (TypeMirror resolvedThrownType : resolvedThrownTypes) { builder.addException(TypeName.get(resolvedThrownType)); diff --git a/javapoet/src/test/java/com/palantir/javapoet/MethodSpecTest.java b/javapoet/src/test/java/com/palantir/javapoet/MethodSpecTest.java index b7d2200..87f21c9 100644 --- a/javapoet/src/test/java/com/palantir/javapoet/MethodSpecTest.java +++ b/javapoet/src/test/java/com/palantir/javapoet/MethodSpecTest.java @@ -117,7 +117,15 @@ interface Throws { void fail() throws R; } - interface ExtendsOthers extends Callable, Comparable, Throws {} + interface CustomExtensible { + void doStuff(S input); + } + + interface ExtendsOthers + extends Callable, + Comparable, + Throws, + CustomExtensible {} interface ExtendsIterableWithDefaultMethods extends Iterable {} @@ -227,6 +235,15 @@ public int compareTo(com.palantir.javapoet.MethodSpecTest.ExtendsOthers arg0) { public void fail() throws java.lang.IllegalStateException { } """); + exec = findFirst(methods, "doStuff"); + method = MethodSpec.overriding(exec, classType, types).build(); + assertThat(method.toString()) + .isEqualTo( + """ + @java.lang.Override + public void doStuff(S input) { + } + """); } @Test