@Test public void hairyTypeParameters() { TypeElement source3 = elementUtils.getTypeElement(Source3.class.getCanonicalName()); TypeElement target3 = elementUtils.getTypeElement(Target3.class.getCanonicalName()); List<ExecutableElement> sourceMethods = ElementFilter.methodsIn(source3.getEnclosedElements()); Map<ExecutableElement, TypeMirror> types = TypeVariables.rewriteReturnTypes(elementUtils, typeUtils, sourceMethods, source3, target3); List<ExecutableElement> targetMethods = ElementFilter.methodsIn(target3.getEnclosedElements()); TypeMirror setFooParameter = targetMethods.get(0).getParameters().get(0).asType(); ExecutableElement getFoo = sourceMethods.get(0); TypeMirror originalGetFooReturn = getFoo.getReturnType(); TypeMirror rewrittenGetFooReturn = types.get(getFoo); assertThat(typeUtils.isAssignable(setFooParameter, originalGetFooReturn)).isFalse(); assertThat(typeUtils.isAssignable(setFooParameter, rewrittenGetFooReturn)).isTrue(); }
static ImmutableMap<ExecutableElement, TypeMirror> rewriteReturnTypes( Elements elementUtils, Types typeUtils, Collection<ExecutableElement> methods, TypeElement sourceType, TypeElement targetType) { List<? extends TypeParameterElement> sourceTypeParameters = sourceType.getTypeParameters(); List<? extends TypeParameterElement> targetTypeParameters = targetType.getTypeParameters(); Preconditions.checkArgument( sourceTypeParameters.toString().equals(targetTypeParameters.toString()), "%s != %s", sourceTypeParameters, targetTypeParameters); EclipseHack eclipseHack = new EclipseHack(elementUtils, typeUtils); TypeMirror[] targetTypeParameterMirrors = new TypeMirror[targetTypeParameters.size()]; for (int i = 0; i < targetTypeParameters.size(); i++) { targetTypeParameterMirrors[i] = targetTypeParameters.get(i).asType(); } DeclaredType parallelSource = typeUtils.getDeclaredType(sourceType, targetTypeParameterMirrors); return methods.stream() .collect( ImmutableMap.toImmutableMap( m -> m, m -> eclipseHack.methodReturnType(m, parallelSource))); }
TypeVariables { static ImmutableMap<ExecutableElement, TypeMirror> rewriteReturnTypes( Elements elementUtils, Types typeUtils, Collection<ExecutableElement> methods, TypeElement sourceType, TypeElement targetType) { List<? extends TypeParameterElement> sourceTypeParameters = sourceType.getTypeParameters(); List<? extends TypeParameterElement> targetTypeParameters = targetType.getTypeParameters(); Preconditions.checkArgument( sourceTypeParameters.toString().equals(targetTypeParameters.toString()), "%s != %s", sourceTypeParameters, targetTypeParameters); EclipseHack eclipseHack = new EclipseHack(elementUtils, typeUtils); TypeMirror[] targetTypeParameterMirrors = new TypeMirror[targetTypeParameters.size()]; for (int i = 0; i < targetTypeParameters.size(); i++) { targetTypeParameterMirrors[i] = targetTypeParameters.get(i).asType(); } DeclaredType parallelSource = typeUtils.getDeclaredType(sourceType, targetTypeParameterMirrors); return methods.stream() .collect( ImmutableMap.toImmutableMap( m -> m, m -> eclipseHack.methodReturnType(m, parallelSource))); } }
TypeVariables { static ImmutableMap<ExecutableElement, TypeMirror> rewriteReturnTypes( Elements elementUtils, Types typeUtils, Collection<ExecutableElement> methods, TypeElement sourceType, TypeElement targetType) { List<? extends TypeParameterElement> sourceTypeParameters = sourceType.getTypeParameters(); List<? extends TypeParameterElement> targetTypeParameters = targetType.getTypeParameters(); Preconditions.checkArgument( sourceTypeParameters.toString().equals(targetTypeParameters.toString()), "%s != %s", sourceTypeParameters, targetTypeParameters); EclipseHack eclipseHack = new EclipseHack(elementUtils, typeUtils); TypeMirror[] targetTypeParameterMirrors = new TypeMirror[targetTypeParameters.size()]; for (int i = 0; i < targetTypeParameters.size(); i++) { targetTypeParameterMirrors[i] = targetTypeParameters.get(i).asType(); } DeclaredType parallelSource = typeUtils.getDeclaredType(sourceType, targetTypeParameterMirrors); return methods.stream() .collect( ImmutableMap.toImmutableMap( m -> m, m -> eclipseHack.methodReturnType(m, parallelSource))); } private  TypeVariables(); }
TypeVariables { static ImmutableMap<ExecutableElement, TypeMirror> rewriteReturnTypes( Elements elementUtils, Types typeUtils, Collection<ExecutableElement> methods, TypeElement sourceType, TypeElement targetType) { List<? extends TypeParameterElement> sourceTypeParameters = sourceType.getTypeParameters(); List<? extends TypeParameterElement> targetTypeParameters = targetType.getTypeParameters(); Preconditions.checkArgument( sourceTypeParameters.toString().equals(targetTypeParameters.toString()), "%s != %s", sourceTypeParameters, targetTypeParameters); EclipseHack eclipseHack = new EclipseHack(elementUtils, typeUtils); TypeMirror[] targetTypeParameterMirrors = new TypeMirror[targetTypeParameters.size()]; for (int i = 0; i < targetTypeParameters.size(); i++) { targetTypeParameterMirrors[i] = targetTypeParameters.get(i).asType(); } DeclaredType parallelSource = typeUtils.getDeclaredType(sourceType, targetTypeParameterMirrors); return methods.stream() .collect( ImmutableMap.toImmutableMap( m -> m, m -> eclipseHack.methodReturnType(m, parallelSource))); } private  TypeVariables();  }
TypeVariables { static ImmutableMap<ExecutableElement, TypeMirror> rewriteReturnTypes( Elements elementUtils, Types typeUtils, Collection<ExecutableElement> methods, TypeElement sourceType, TypeElement targetType) { List<? extends TypeParameterElement> sourceTypeParameters = sourceType.getTypeParameters(); List<? extends TypeParameterElement> targetTypeParameters = targetType.getTypeParameters(); Preconditions.checkArgument( sourceTypeParameters.toString().equals(targetTypeParameters.toString()), "%s != %s", sourceTypeParameters, targetTypeParameters); EclipseHack eclipseHack = new EclipseHack(elementUtils, typeUtils); TypeMirror[] targetTypeParameterMirrors = new TypeMirror[targetTypeParameters.size()]; for (int i = 0; i < targetTypeParameters.size(); i++) { targetTypeParameterMirrors[i] = targetTypeParameters.get(i).asType(); } DeclaredType parallelSource = typeUtils.getDeclaredType(sourceType, targetTypeParameterMirrors); return methods.stream() .collect( ImmutableMap.toImmutableMap( m -> m, m -> eclipseHack.methodReturnType(m, parallelSource))); } private  TypeVariables();   }