Add Localizable class to fix CmdLineException deprecation warnings

In the updated version of args4j the constructor:

  CmdLineException(CmdLineParser, String)

is deprecated in favor of:

  CmdLineException(CmdLineParser, Localizable, String...)

Add a new Localizable class that implements the expected interface,
and replace usage of the deprecated constructor across the Gerrit code
except in AsciiDoctor and DocIndexer. The latter don't really need to
use CmdLineException; this will be fixed in a separate commit.

Change-Id: I7033c0dd764780a163953ca9fc39da228ac20707
This commit is contained in:
David Pursehouse
2018-08-02 09:20:23 +02:00
parent 655214054c
commit dc1d42d2f6
12 changed files with 82 additions and 21 deletions

View File

@@ -14,6 +14,8 @@
package com.google.gerrit.server.args4j;
import static com.google.gerrit.util.cli.Localizable.localizable;
import com.google.gerrit.reviewdb.client.AccountGroup;
import com.google.gerrit.server.account.GroupCache;
import com.google.gerrit.server.group.InternalGroup;
@@ -45,7 +47,7 @@ public class AccountGroupIdHandler extends OptionHandler<AccountGroup.Id> {
final String n = params.getParameter(0);
Optional<InternalGroup> group = groupCache.get(new AccountGroup.NameKey(n));
if (!group.isPresent()) {
throw new CmdLineException(owner, "Group \"" + n + "\" does not exist");
throw new CmdLineException(owner, localizable("Group \"%s\" does not exist"), n);
}
setter.addValue(group.get().getId());
return 1;

View File

@@ -14,6 +14,8 @@
package com.google.gerrit.server.args4j;
import static com.google.gerrit.util.cli.Localizable.localizable;
import com.google.gerrit.common.data.GroupDescription;
import com.google.gerrit.common.data.GroupReference;
import com.google.gerrit.reviewdb.client.AccountGroup;
@@ -77,7 +79,7 @@ public class AccountGroupUUIDHandler extends OptionHandler<AccountGroup.UUID> {
GroupReference group = GroupBackends.findExactSuggestion(groupBackend, n);
if (group == null) {
throw new CmdLineException(owner, "Group \"" + n + "\" does not exist");
throw new CmdLineException(owner, localizable("Group \"%s\" does not exist"), n);
}
setter.addValue(group.getUUID());
return 1;

View File

@@ -14,6 +14,8 @@
package com.google.gerrit.server.args4j;
import static com.google.gerrit.util.cli.Localizable.localizable;
import com.google.gerrit.extensions.client.AuthType;
import com.google.gerrit.reviewdb.client.Account;
import com.google.gerrit.server.account.AccountException;
@@ -76,11 +78,11 @@ public class AccountIdHandler extends OptionHandler<Account.Id> {
case OPENID:
case OPENID_SSO:
default:
throw new CmdLineException(owner, "user \"" + token + "\" not found");
throw new CmdLineException(owner, localizable("user \"%s\" not found"), token);
}
}
} catch (OrmException e) {
throw new CmdLineException(owner, "database is down");
throw new CmdLineException(owner, localizable("database is down"));
} catch (IOException e) {
throw new CmdLineException(owner, "Failed to load account", e);
} catch (ConfigInvalidException e) {
@@ -92,7 +94,7 @@ public class AccountIdHandler extends OptionHandler<Account.Id> {
private Account.Id createAccountByLdap(String user) throws CmdLineException, IOException {
if (!ExternalId.isValidUsername(user)) {
throw new CmdLineException(owner, "user \"" + user + "\" not found");
throw new CmdLineException(owner, localizable("user \"%s\" not found"), user);
}
try {
@@ -100,7 +102,7 @@ public class AccountIdHandler extends OptionHandler<Account.Id> {
req.setSkipAuthentication(true);
return accountManager.authenticate(req).getAccountId();
} catch (AccountException e) {
throw new CmdLineException(owner, "user \"" + user + "\" not found");
throw new CmdLineException(owner, localizable("user \"%s\" not found"), user);
}
}

View File

@@ -14,6 +14,8 @@
package com.google.gerrit.server.args4j;
import static com.google.gerrit.util.cli.Localizable.localizable;
import com.google.common.base.Splitter;
import com.google.gerrit.reviewdb.client.Branch;
import com.google.gerrit.reviewdb.client.Change;
@@ -52,7 +54,7 @@ public class ChangeIdHandler extends OptionHandler<Change.Id> {
final List<String> tokens = Splitter.on(',').splitToList(token);
if (tokens.size() != 3) {
throw new CmdLineException(
owner, "change should be specified as <project>,<branch>,<change-id>");
owner, localizable("change should be specified as <project>,<branch>,<change-id>"));
}
try {
@@ -64,12 +66,12 @@ public class ChangeIdHandler extends OptionHandler<Change.Id> {
return 1;
}
} catch (IllegalArgumentException e) {
throw new CmdLineException(owner, "Change-Id is not valid");
throw new CmdLineException(owner, localizable("Change-Id is not valid"));
} catch (OrmException e) {
throw new CmdLineException(owner, "Database error: " + e.getMessage());
throw new CmdLineException(owner, localizable("Database error: %s"), e.getMessage());
}
throw new CmdLineException(owner, "\"" + token + "\": change not found");
throw new CmdLineException(owner, localizable("\"%s\": change not found"), token);
}
@Override

View File

@@ -14,6 +14,8 @@
package com.google.gerrit.server.args4j;
import static com.google.gerrit.util.cli.Localizable.localizable;
import com.google.gerrit.reviewdb.client.PatchSet;
import com.google.inject.Inject;
import com.google.inject.assistedinject.Assisted;
@@ -41,7 +43,7 @@ public class PatchSetIdHandler extends OptionHandler<PatchSet.Id> {
try {
id = PatchSet.Id.parse(token);
} catch (IllegalArgumentException e) {
throw new CmdLineException(owner, "\"" + token + "\" is not a valid patch set");
throw new CmdLineException(owner, localizable("\"%s\" is not a valid patch set"), token);
}
setter.addValue(id);

View File

@@ -14,6 +14,8 @@
package com.google.gerrit.server.args4j;
import static com.google.gerrit.util.cli.Localizable.localizable;
import com.google.common.flogger.FluentLogger;
import com.google.gerrit.common.ProjectUtil;
import com.google.gerrit.extensions.restapi.AuthException;
@@ -76,7 +78,7 @@ public class ProjectHandler extends OptionHandler<ProjectState> {
try {
state = projectCache.checkedGet(nameKey);
if (state == null) {
throw new CmdLineException(owner, String.format("project %s not found", nameWithoutSuffix));
throw new CmdLineException(owner, localizable("project %s not found"), nameWithoutSuffix);
}
// Hidden projects(permitsRead = false) should only be accessible by the project owners.
// READ_CONFIG is checked here because it's only allowed to project owners(ACCESS may also
@@ -86,10 +88,12 @@ public class ProjectHandler extends OptionHandler<ProjectState> {
state.statePermitsRead() ? ProjectPermission.ACCESS : ProjectPermission.READ_CONFIG;
permissionBackend.currentUser().project(nameKey).check(permissionToCheck);
} catch (AuthException e) {
throw new CmdLineException(owner, new NoSuchProjectException(nameKey).getMessage());
throw new CmdLineException(
owner, localizable(new NoSuchProjectException(nameKey).getMessage()));
} catch (PermissionBackendException | IOException e) {
logger.atWarning().withCause(e).log("Cannot load project %s", nameWithoutSuffix);
throw new CmdLineException(owner, new NoSuchProjectException(nameKey).getMessage());
throw new CmdLineException(
owner, localizable(new NoSuchProjectException(nameKey).getMessage()));
}
setter.addValue(state);

View File

@@ -14,6 +14,8 @@
package com.google.gerrit.server.args4j;
import static com.google.gerrit.util.cli.Localizable.localizable;
import com.google.gerrit.server.util.SocketUtil;
import com.google.inject.Inject;
import com.google.inject.assistedinject.Assisted;
@@ -41,7 +43,7 @@ public class SocketAddressHandler extends OptionHandler<SocketAddress> {
try {
setter.addValue(SocketUtil.parse(token, 0));
} catch (IllegalArgumentException e) {
throw new CmdLineException(owner, e.getMessage());
throw new CmdLineException(owner, localizable(e.getMessage()));
}
return 1;
}