Convert CurrentUser to IdentifiedUser without casting
Add an asIdentifiedUser() method to CurrentUser that throws UnsupportedOperationException if the user is not an IdentifiedUser. This has a number of minor benefits: - Fewer extraneous parens required for writing ((IdentifiedUser) u).getAccount(). - Slightly more descriptive error message "FooUser is not IdentifiedUser" rather than a ClassCastException when an assumed precondition does not hold. - Implementation may be slightly more efficient, by overriding the method to "return this" rather than an extra instruction for the cast. (Not benchmarked.) By far the most common use of ((IdentifiedUser u) is to immediately call getAccountId(), so add a similar method getAccountId() to CurrentUser. These methods still throw unchecked exceptions, which matches the existing behavior. Some calls are guarded by isIdentifiedUser() and take different behavior if false; this ranges from throwing a checked exception, typically but not always AuthException, to substituting GerritPersonIdent. Other calls just continue to assume that the user is always an IdentifiedUser. Change-Id: I2ab2028a7cdc0f703c4a4cdc2b5797d87ab7024c
This commit is contained in:
@@ -326,8 +326,7 @@ class InProcessProtocol extends TestProtocol<Context> {
|
||||
throw new ServiceNotAuthorizedException();
|
||||
}
|
||||
|
||||
IdentifiedUser user = (IdentifiedUser) ctl.getUser();
|
||||
rp.setRefLogIdent(user.newRefLogIdent());
|
||||
rp.setRefLogIdent(ctl.getUser().asIdentifiedUser().newRefLogIdent());
|
||||
rp.setTimeout(config.getTimeout());
|
||||
rp.setMaxObjectSizeLimit(config.getMaxObjectSizeLimit());
|
||||
|
||||
|
Reference in New Issue
Block a user