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;
}

View File

@@ -18,6 +18,7 @@ java_library(
"//java/com/google/gerrit/reviewdb:server",
"//java/com/google/gerrit/server",
"//java/com/google/gerrit/server/ioutil",
"//java/com/google/gerrit/util/cli",
"//java/org/eclipse/jgit:server",
"//lib:args4j",
"//lib:blame-cache",

View File

@@ -15,6 +15,7 @@
package com.google.gerrit.server.restapi.change;
import static com.google.common.base.Preconditions.checkState;
import static com.google.gerrit.util.cli.Localizable.localizable;
import com.google.common.base.MoreObjects;
import com.google.common.collect.ImmutableList;
@@ -440,9 +441,9 @@ public class GetDiff implements RestReadView<FileResource> {
} catch (NumberFormatException e) {
throw new CmdLineException(
owner,
String.format(
"\"%s\" is not a valid value for \"%s\"",
value, ((NamedOptionDef) option).name()));
localizable("\"%s\" is not a valid value for \"%s\""),
value,
((NamedOptionDef) option).name());
}
}
setter.addValue(context);

View File

@@ -14,6 +14,8 @@
package com.google.gerrit.sshd.commands;
import static com.google.gerrit.util.cli.Localizable.localizable;
import com.google.gerrit.common.data.LabelType;
import com.google.gerrit.common.data.LabelValue;
import java.lang.annotation.Annotation;
@@ -160,7 +162,7 @@ final class ApproveOption implements Option, Setter<Short> {
+ " for \""
+ name
+ "\"";
throw new CmdLineException(owner, e);
throw new CmdLineException(owner, localizable(e));
}
return value;
}

View File

@@ -34,6 +34,8 @@
package com.google.gerrit.util.cli;
import static com.google.gerrit.util.cli.Localizable.localizable;
import com.google.common.base.Strings;
import com.google.common.collect.ListMultimap;
import com.google.common.collect.Lists;
@@ -320,7 +322,7 @@ public class CmdLineParser {
return false;
}
throw new CmdLineException(parser, String.format("invalid boolean \"%s=%s\"", name, value));
throw new CmdLineException(parser, localizable("invalid boolean \"%s=%s\""), name, value);
}
private static class PrefixedOption implements Option {
@@ -586,6 +588,6 @@ public class CmdLineParser {
}
public CmdLineException reject(String message) {
return new CmdLineException(parser, message);
return new CmdLineException(parser, localizable(message));
}
}

View File

@@ -0,0 +1,39 @@
// Copyright (C) 2018 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.gerrit.util.cli;
import java.util.Locale;
public class Localizable implements org.kohsuke.args4j.Localizable {
private final String format;
@Override
public String formatWithLocale(Locale locale, Object... args) {
return String.format(locale, format, args);
}
@Override
public String format(Object... args) {
return String.format(format, args);
}
private Localizable(String format) {
this.format = format;
}
public static Localizable localizable(String format) {
return new Localizable(format);
}
}