@Test public void nestedClasses() { TypeElement outer = elementUtils.getTypeElement(Outer.class.getCanonicalName()); TypeElement inner = elementUtils.getTypeElement(Outer.Inner.class.getCanonicalName()); List<ExecutableElement> outerMethods = ElementFilter.methodsIn(outer.getEnclosedElements()); Map<ExecutableElement, TypeMirror> types = TypeVariables.rewriteReturnTypes(elementUtils, typeUtils, outerMethods, outer, inner); List<ExecutableElement> innerMethods = ElementFilter.methodsIn(inner.getEnclosedElements()); ExecutableElement getFoo = methodNamed(outerMethods, "getFoo"); ExecutableElement getBar = methodNamed(outerMethods, "getBar"); ExecutableElement setFoo = methodNamed(innerMethods, "setFoo"); ExecutableElement setBar = methodNamed(innerMethods, "setBar"); TypeMirror setFooParameter = setFoo.getParameters().get(0).asType(); TypeMirror originalGetFooReturn = getFoo.getReturnType(); TypeMirror rewrittenGetFooReturn = types.get(getFoo); assertThat(typeUtils.isAssignable(setFooParameter, originalGetFooReturn)).isFalse(); assertThat(typeUtils.isAssignable(setFooParameter, rewrittenGetFooReturn)).isTrue(); TypeMirror setBarParameter = setBar.getParameters().get(0).asType(); TypeMirror originalGetBarReturn = getBar.getReturnType(); TypeMirror rewrittenGetBarReturn = types.get(getBar); assertThat(typeUtils.isAssignable(setBarParameter, originalGetBarReturn)).isFalse(); assertThat(typeUtils.isAssignable(setBarParameter, rewrittenGetBarReturn)).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();   }