当前位置: 首页 > 知识库问答 >
问题:

不存在类型变量U的实例,因此Foo符合CompletionStage

姜森
2023-03-14

我已经研究了几个小时,但似乎无法解决这个问题。错误与这段代码有关:

(问题底部的完整代码。我用截图直观地显示问题。)

错误本身是:

不存在类型变量U的实例,因此GetUsersFaradacCountResponse符合CompletionStage

和composes链的最开始,我删除了返回,并使用IntelliJ的“引入局部变量…”查看整个链(直至并包括第1118行)返回的类型的功能:

结果是一个

final CompletionStage<U> uCompletionStage = ...

但是您可以看到,包含方法的返回类型是

public CompletionStage<GetUsersForAdAccountResponse> ...

是什么阻止编译器推断GetUsersForAdAccount tResponse?(同样,通常这里有一个返回。)

在此过程中,我还尝试在每个thenCompose处引入一个局部变量,它们似乎都是正确的。每个都产生一个CompletionStage

我不知道它是否会有帮助,但这是整个方法:

  @Override
  public CompletionStage<GetUsersForAdAccountResponse> getUsersForAdAccount(
      RequestContext context, GetUsersForAdAccountRequest request) {

    Uuid adAccountId = request.getAdAccountId();

    return verifyAuthorization(context,
        PortcullisTemplates.Action.GET_USERS_FOR_AD_ACCOUNT.getName(),
        portcullisTemplates.topOrganizationResource())
        .thenCompose(auditLogPrincipal -> jdbiExecutor.executeInTransaction(handler -> {

              // We purposely safeguard the account lookup as well behind Portcullis.
              AdAccountDao adAccountDao = handler.attach(AdAccountDao.class);
              if (adAccountDao.getAdAccountById(adAccountId) == null) {
                throw new ValidationException(SERVICE_NAME,
                    "Ad account not found: " + UuidUtils.toString(adAccountId));
              }

              AdAccountRoleUserMappingDao roleDao = handler.attach(AdAccountRoleUserMappingDao.class);

              List<String> roleNames = request.getRoleNamesList();
              return roleNames.isEmpty() ?
                     roleDao.getAdAccountRoleUserMappingsByAdAccount(adAccountId) :
                     roleDao.getAdAccountRoleUserMappingsByAdAccountAndRoles(adAccountId, roleNames);

        })).thenCompose(adAccountRoleUserMappings -> jdbiExecutor.execute(UserDao.class, userDao -> {
          return userDao
              .getUsersBy]UserIds(
                  adAccountRoleUserMappings.stream()
                      .map(AdAccountRoleUserMapping::userId)
                      .collect(Collectors.toList())
              ).stream()
                  .collect(Collectors.toMap(
                      User::userId,
                      user -> new EncryptedFieldsBuilder()
                          .firstName(user.encryptedFirstName())
                          .lastName(user.encryptedLastName())
                          .email(user.encryptedEmail())
                          .build()
              ));
        }).thenCompose(
            userEncryptedFields -> padlockService.decryptUserAccounts(userEncryptedFields)
        ).thenCompose(decryptedUsers -> GetUsersForAdAccountResponse.newBuilder()
            .addAllUserWithRole(
                adAccountRoleUserMappings.stream()
                    .filter(mapping -> decryptedUsers.containsKey(mapping.userId()))
                    .map(mapping -> UserWithRole.newBuilder()
                        .setAccount(decryptedUsers.get(mapping.userId()))
                        .setRoleName(mapping.roleName())
                        .build())
                    .collect(Collectors.toSet())
            ).build()
        ));
  }

共有1个答案

华煜祺
2023-03-14

链中最后一个然后是compose,应该是然后是apply。传递给它的函数正在返回一个GetUsersForAdAccountResponse,它不是CompletionStage

thencoseCompletableFutureflatMap。它使用一个函数,返回一个CompletionStage,并将结果展平。

 类似资料: