public static TypeMirror asMemberOf(
      Types types, DeclaredType container, VariableElement variable) {
    if (variable.getKind().equals(ElementKind.PARAMETER)) {
      ExecutableElement methodOrConstructor =
          MoreElements.asExecutable(variable.getEnclosingElement());
      ExecutableType resolvedMethodOrConstructor =
          MoreTypes.asExecutable(types.asMemberOf(container, methodOrConstructor));
      List<? extends VariableElement> parameters = methodOrConstructor.getParameters();
      List<? extends TypeMirror> parameterTypes = resolvedMethodOrConstructor.getParameterTypes();
      checkState(parameters.size() == parameterTypes.size());
      for (int i = 0; i < parameters.size(); i++) {
        // We need to capture the parameter type of the variable we're concerned about,
        // for later printing.  This is the only way to do it since we can't use
        // types.asMemberOf on variables of methods.
        if (parameters.get(i).equals(variable)) {
          return parameterTypes.get(i);
        }
      }
      throw new IllegalStateException("Could not find variable: " + variable);
    } else {
      return types.asMemberOf(container, variable);
    }
  }
Resolves a {@link VariableElement} parameter to a method or constructor based on the given
container, or a member of a class. For parameters to a method or constructor, the variable's
enclosing element must be a supertype of the container type. For example, given a {@code
container} of type {@code Set<String>}, and a variable corresponding to the {@code E e}
parameter in the {@code Set.add(E e)} method, this will return a TypeMirror for {@code String}.
common/src/main/java/com/google/auto/common/MoreTypes.java