Turn on many more Eclipse warnings, and fix them

- Warn on empty statements, e.g. "for (;;);". These may be
   typos and are easily replaced by "for (;;) {}" which is more
   explicit.
 - Warn on field hiding. This allows cleanup of many acceptance test
   members, at the cost of a couple of renames and the occasional
   suppression (when the field is in a public nested enum that shadows
   a public constant).
 - Warn on unnecessary casts.
 - Warn on unused declared thrown exceptions. In addition to reducing
   method signature length and number of imports, this also eliminated
   some impossible catch blocks.
 - Warn on missing @Override annotations.
 - Warn on unused parameters. This is likely the most controversial,
   as a few relatively common patterns require unused parameters in a
   way that Eclipse can't ignore. However, it also resulted in cleanup
   of a lot of unnecessary injections and method parameters, so I
   think the cost was worth it.

Change-Id: I7224be8b1c798613a127c88507e8cce400679e5d
This commit is contained in:
Dave Borowitz
2014-10-28 12:09:55 -07:00
parent 2e82f2f8a2
commit 8b42ec5bd5
305 changed files with 932 additions and 699 deletions

View File

@@ -31,6 +31,7 @@ class AdvertisedObjectsCacheKey {
return account.hashCode();
}
@Override
public boolean equals(Object other) {
if (other instanceof AdvertisedObjectsCacheKey) {
AdvertisedObjectsCacheKey o = (AdvertisedObjectsCacheKey) other;

View File

@@ -21,7 +21,6 @@ import com.google.gerrit.common.Nullable;
import com.google.gerrit.common.TimeUtil;
import com.google.gerrit.extensions.registration.DynamicItem;
import com.google.gerrit.server.CurrentUser;
import com.google.gerrit.server.account.AccountManager;
import com.google.gerrit.server.config.AuthConfig;
import com.google.gerrit.server.config.CanonicalWebUrl;
import com.google.inject.Inject;
@@ -47,7 +46,6 @@ class HttpLogoutServlet extends HttpServlet {
HttpLogoutServlet(final AuthConfig authConfig,
final DynamicItem<WebSession> webSession,
@CanonicalWebUrl @Nullable final Provider<String> urlProvider,
final AccountManager accountManager,
final AuditService audit) {
this.webSession = webSession;
this.urlProvider = urlProvider;

View File

@@ -48,7 +48,6 @@ import java.io.Writer;
import java.util.List;
import java.util.UUID;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
@@ -68,7 +67,6 @@ class BecomeAnyAccountLoginServlet extends HttpServlet {
BecomeAnyAccountLoginServlet(final DynamicItem<WebSession> ws,
final SchemaFactory<ReviewDb> sf,
final AccountManager am,
final ServletContext servletContext,
SiteHeaderFooter shf) {
webSession = ws;
schema = sf;
@@ -92,13 +90,13 @@ class BecomeAnyAccountLoginServlet extends HttpServlet {
res = create();
} else if (req.getParameter("user_name") != null) {
res = byUserName(rsp, req.getParameter("user_name"));
res = byUserName(req.getParameter("user_name"));
} else if (req.getParameter("preferred_email") != null) {
res = byPreferredEmail(rsp, req.getParameter("preferred_email"));
res = byPreferredEmail(req.getParameter("preferred_email"));
} else if (req.getParameter("account_id") != null) {
res = byAccountId(rsp, req.getParameter("account_id"));
res = byAccountId(req.getParameter("account_id"));
} else {
byte[] raw;
@@ -210,8 +208,7 @@ class BecomeAnyAccountLoginServlet extends HttpServlet {
return null;
}
private AuthResult byUserName(final HttpServletResponse rsp,
final String userName) {
private AuthResult byUserName(final String userName) {
try {
final ReviewDb db = schema.open();
try {
@@ -227,8 +224,7 @@ class BecomeAnyAccountLoginServlet extends HttpServlet {
}
}
private AuthResult byPreferredEmail(final HttpServletResponse rsp,
final String email) {
private AuthResult byPreferredEmail(final String email) {
try {
final ReviewDb db = schema.open();
try {
@@ -243,8 +239,7 @@ class BecomeAnyAccountLoginServlet extends HttpServlet {
}
}
private AuthResult byAccountId(final HttpServletResponse rsp,
final String idStr) {
private AuthResult byAccountId(final String idStr) {
final Account.Id id;
try {
id = Account.Id.parse(idStr);

View File

@@ -36,8 +36,8 @@ abstract class GitWebCssServlet extends HttpServlet {
@Singleton
static class Site extends GitWebCssServlet {
@Inject
Site(SitePaths paths, GitWebConfig gwc) throws IOException {
super(paths.site_css, gwc);
Site(SitePaths paths) throws IOException {
super(paths.site_css);
}
}
@@ -45,7 +45,7 @@ abstract class GitWebCssServlet extends HttpServlet {
static class Default extends GitWebCssServlet {
@Inject
Default(GitWebConfig gwc) throws IOException {
super(gwc.getGitwebCSS(), gwc);
super(gwc.getGitwebCSS());
}
}
@@ -55,7 +55,7 @@ abstract class GitWebCssServlet extends HttpServlet {
private final byte[] raw_css;
private final byte[] gz_css;
GitWebCssServlet(final File src, final GitWebConfig gitWebConfig)
GitWebCssServlet(final File src)
throws IOException {
if (src != null) {
final File dir = src.getParentFile();

View File

@@ -402,7 +402,7 @@ class GitWebServlet extends HttpServlet {
}
try {
CacheHeaders.setNotCacheable(rsp);
exec(req, rsp, project, repo);
exec(req, rsp, project);
} finally {
repo.close();
}
@@ -435,8 +435,7 @@ class GitWebServlet extends HttpServlet {
}
private void exec(final HttpServletRequest req,
final HttpServletResponse rsp, final ProjectControl project,
final Repository repo) throws IOException {
final HttpServletResponse rsp, final ProjectControl project) throws IOException {
final Process proc =
Runtime.getRuntime().exec(new String[] {gitwebCgi.getAbsolutePath()},
makeEnv(req, project),
@@ -595,6 +594,7 @@ class GitWebServlet extends HttpServlet {
final int contentLength = req.getContentLength();
final InputStream src = req.getInputStream();
new Thread(new Runnable() {
@Override
public void run() {
try {
try {
@@ -621,6 +621,7 @@ class GitWebServlet extends HttpServlet {
private void copyStderrToLog(final InputStream in) {
new Thread(new Runnable() {
@Override
public void run() {
try {
final BufferedReader br =

View File

@@ -33,7 +33,6 @@ import com.google.gerrit.httpd.resources.SmallResource;
import com.google.gerrit.httpd.restapi.RestApiServlet;
import com.google.gerrit.server.MimeUtilFileTypeRegistry;
import com.google.gerrit.server.config.CanonicalWebUrl;
import com.google.gerrit.server.config.GerritServerConfig;
import com.google.gerrit.server.documentation.MarkdownFormatter;
import com.google.gerrit.server.plugins.Plugin;
import com.google.gerrit.server.plugins.Plugin.ApiType;
@@ -50,7 +49,6 @@ import com.google.inject.Singleton;
import com.google.inject.name.Named;
import com.google.inject.servlet.GuiceFilter;
import org.eclipse.jgit.lib.Config;
import org.eclipse.jgit.util.IO;
import org.eclipse.jgit.util.RawParseUtils;
import org.slf4j.Logger;
@@ -109,7 +107,6 @@ class HttpPluginServlet extends HttpServlet
MimeUtilFileTypeRegistry mimeUtil,
@CanonicalWebUrl Provider<String> webUrl,
@Named(HttpPluginModule.PLUGIN_RESOURCES) Cache<ResourceKey, Resource> cache,
@GerritServerConfig Config cfg,
SshInfo sshInfo,
RestApiServlet.Globals globals,
PluginsCollection plugins) {

View File

@@ -26,6 +26,7 @@ final class PluginResourceKey implements ResourceKey {
this.resource = r;
}
@Override
public int weigh() {
return resource.length() * 2;
}

View File

@@ -24,7 +24,6 @@ import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@@ -45,7 +44,7 @@ public class LegacyGerritServlet extends HttpServlet {
private final byte[] compressed;
@Inject
LegacyGerritServlet(final ServletContext servletContext) throws IOException {
LegacyGerritServlet() throws IOException {
final String pageName = "LegacyGerrit.html";
final String doc = HtmlDomUtil.readFile(getClass(), pageName);
if (doc == null) {

View File

@@ -61,7 +61,7 @@ public class ToolServlet extends HttpServlet {
switch (ent.getType()) {
case FILE:
doGetFile(ent, req, rsp);
doGetFile(ent, rsp);
break;
case DIR:
@@ -74,8 +74,7 @@ public class ToolServlet extends HttpServlet {
}
}
private void doGetFile(Entry ent, HttpServletRequest req,
HttpServletResponse rsp) throws IOException {
private void doGetFile(Entry ent, HttpServletResponse rsp) throws IOException {
byte[] tosend = ent.getBytes();
rsp.setDateHeader(HDR_EXPIRES, 0L);

View File

@@ -224,7 +224,7 @@ public class RestApiServlet extends HttpServlet {
try {
rsrc = rc.parse(rsrc, id);
if (path.isEmpty()) {
checkPreconditions(req, rsrc);
checkPreconditions(req);
}
} catch (ResourceNotFoundException e) {
if (rc instanceof AcceptsCreate
@@ -269,7 +269,7 @@ public class RestApiServlet extends HttpServlet {
IdString id = path.remove(0);
try {
rsrc = c.parse(rsrc, id);
checkPreconditions(req, rsrc);
checkPreconditions(req);
viewData = new ViewData(null, null);
} catch (ResourceNotFoundException e) {
if (c instanceof AcceptsCreate
@@ -439,7 +439,7 @@ public class RestApiServlet extends HttpServlet {
}
}
private void checkPreconditions(HttpServletRequest req, RestResource rsrc)
private void checkPreconditions(HttpServletRequest req)
throws PreconditionFailedException {
if ("*".equals(req.getHeader("If-None-Match"))) {
throw new PreconditionFailedException("Resource already exists");

View File

@@ -28,6 +28,7 @@ class AuditedHttpServletResponse
super(response);
}
@Override
public int getStatus() {
return status;
}

View File

@@ -131,7 +131,7 @@ final class GerritJsonServlet extends JsonServlet<GerritJsonServlet.GerritCall>
if (method == null) {
return;
}
Audit note = (Audit) method.getAnnotation(Audit.class);
Audit note = method.getAnnotation(Audit.class);
if (note != null) {
final String sid = call.getWebSession().getSessionId();
final CurrentUser username = call.getWebSession().getCurrentUser();

View File

@@ -103,5 +103,6 @@ public abstract class Handler<T> implements Callable<T> {
* @throws Exception the operation failed. The caller will log the exception
* and the stack trace, if it is worth logging on the server side.
*/
@Override
public abstract T call() throws Exception;
}

View File

@@ -110,9 +110,11 @@ class SuggestServiceImpl extends BaseServiceImplementation implements
boolean isVisible(Account account) throws OrmException;
}
@Override
public void suggestAccount(final String query, final Boolean active,
final int limit, final AsyncCallback<List<AccountInfo>> callback) {
run(callback, new Action<List<AccountInfo>>() {
@Override
public List<AccountInfo> run(final ReviewDb db) throws OrmException {
return suggestAccount(db, query, active, limit, new VisibilityControl() {
@Override
@@ -175,15 +177,18 @@ class SuggestServiceImpl extends BaseServiceImplementation implements
}
}
@Override
public void suggestAccountGroup(final String query, final int limit,
final AsyncCallback<List<GroupReference>> callback) {
suggestAccountGroupForProject(null, query, limit, callback);
}
@Override
public void suggestAccountGroupForProject(final Project.NameKey project,
final String query, final int limit,
final AsyncCallback<List<GroupReference>> callback) {
run(callback, new Action<List<GroupReference>>() {
@Override
public List<GroupReference> run(final ReviewDb db) {
ProjectControl projectControl = null;
if (project != null) {
@@ -217,6 +222,7 @@ class SuggestServiceImpl extends BaseServiceImplementation implements
final String query, final int limit,
final AsyncCallback<List<ReviewerInfo>> callback) {
run(callback, new Action<List<ReviewerInfo>>() {
@Override
public List<ReviewerInfo> run(final ReviewDb db)
throws OrmException, Failure {
final ChangeControl changeControl;

View File

@@ -59,6 +59,7 @@ class SystemInfoServiceImpl implements SystemInfoService {
projectCache = pc;
}
@Override
public void contributorAgreements(
final AsyncCallback<List<ContributorAgreement>> callback) {
Collection<ContributorAgreement> agreements =
@@ -71,6 +72,7 @@ class SystemInfoServiceImpl implements SystemInfoService {
callback.onSuccess(cas);
}
@Override
public void daemonHostKeys(final AsyncCallback<List<SshHostKey>> callback) {
final ArrayList<SshHostKey> r = new ArrayList<>(hostKeys.size());
for (final HostKey hk : hostKeys) {

View File

@@ -120,18 +120,22 @@ class AccountSecurityImpl extends BaseServiceImplementation implements
}
}
@Override
public void myExternalIds(AsyncCallback<List<AccountExternalId>> callback) {
externalIdDetailFactory.create().to(callback);
}
@Override
public void deleteExternalIds(final Set<AccountExternalId.Key> keys,
final AsyncCallback<Set<AccountExternalId.Key>> callback) {
deleteExternalIdsFactory.create(keys).to(callback);
}
@Override
public void updateContact(final String name, final String emailAddr,
final ContactInformation info, final AsyncCallback<Account> callback) {
run(callback, new Action<Account>() {
@Override
public Account run(ReviewDb db) throws OrmException, Failure {
IdentifiedUser self = user.get();
final Account me = db.accounts().get(self.getAccountId());
@@ -175,9 +179,11 @@ class AccountSecurityImpl extends BaseServiceImplementation implements
return a != null && a.equals(b);
}
@Override
public void enterAgreement(final String agreementName,
final AsyncCallback<VoidResult> callback) {
run(callback, new Action<VoidResult>() {
@Override
public VoidResult run(final ReviewDb db) throws OrmException, Failure {
ContributorAgreement ca = projectCache.getAllProjects().getConfig()
.getContributorAgreement(agreementName);
@@ -216,6 +222,7 @@ class AccountSecurityImpl extends BaseServiceImplementation implements
});
}
@Override
public void validateEmail(final String tokenString,
final AsyncCallback<VoidResult> callback) {
try {

View File

@@ -68,6 +68,7 @@ class AccountServiceImpl extends BaseServiceImplementation implements
this.queryBuilder = queryBuilder;
}
@Override
public void myAccount(final AsyncCallback<Account> callback) {
run(callback, new Action<Account>() {
@Override
@@ -77,9 +78,11 @@ class AccountServiceImpl extends BaseServiceImplementation implements
});
}
@Override
public void changePreferences(final AccountGeneralPreferences pref,
final AsyncCallback<VoidResult> callback) {
run(callback, new Action<VoidResult>() {
@Override
public VoidResult run(final ReviewDb db) throws OrmException, Failure {
final Account a = db.accounts().get(getAccountId());
if (a == null) {
@@ -97,6 +100,7 @@ class AccountServiceImpl extends BaseServiceImplementation implements
public void changeDiffPreferences(final AccountDiffPreference diffPref,
AsyncCallback<VoidResult> callback) {
run(callback, new Action<VoidResult>(){
@Override
public VoidResult run(ReviewDb db) throws OrmException {
if (!diffPref.getAccountId().equals(getAccountId())) {
throw new IllegalArgumentException("diffPref.getAccountId() "
@@ -109,9 +113,11 @@ class AccountServiceImpl extends BaseServiceImplementation implements
});
}
@Override
public void myProjectWatch(
final AsyncCallback<List<AccountProjectWatchInfo>> callback) {
run(callback, new Action<List<AccountProjectWatchInfo>>() {
@Override
public List<AccountProjectWatchInfo> run(ReviewDb db) throws OrmException {
List<AccountProjectWatchInfo> r = new ArrayList<>();
@@ -127,6 +133,7 @@ class AccountServiceImpl extends BaseServiceImplementation implements
r.add(new AccountProjectWatchInfo(w, ctl.getProject()));
}
Collections.sort(r, new Comparator<AccountProjectWatchInfo>() {
@Override
public int compare(final AccountProjectWatchInfo a,
final AccountProjectWatchInfo b) {
return a.getProject().getName().compareTo(b.getProject().getName());
@@ -137,9 +144,11 @@ class AccountServiceImpl extends BaseServiceImplementation implements
});
}
@Override
public void addProjectWatch(final String projectName, final String filter,
final AsyncCallback<AccountProjectWatchInfo> callback) {
run(callback, new Action<AccountProjectWatchInfo>() {
@Override
public AccountProjectWatchInfo run(ReviewDb db) throws OrmException,
NoSuchProjectException, InvalidQueryException {
final Project.NameKey nameKey = new Project.NameKey(projectName);
@@ -167,6 +176,7 @@ class AccountServiceImpl extends BaseServiceImplementation implements
});
}
@Override
public void updateProjectWatch(final AccountProjectWatch watch,
final AsyncCallback<VoidResult> callback) {
if (!getAccountId().equals(watch.getAccountId())) {
@@ -175,6 +185,7 @@ class AccountServiceImpl extends BaseServiceImplementation implements
}
run(callback, new Action<VoidResult>() {
@Override
public VoidResult run(ReviewDb db) throws OrmException {
db.accountProjectWatches().update(Collections.singleton(watch));
return VoidResult.INSTANCE;
@@ -182,9 +193,11 @@ class AccountServiceImpl extends BaseServiceImplementation implements
});
}
@Override
public void deleteProjectWatches(final Set<AccountProjectWatch.Key> keys,
final AsyncCallback<VoidResult> callback) {
run(callback, new Action<VoidResult>() {
@Override
public VoidResult run(final ReviewDb db) throws OrmException, Failure {
final Account.Id me = getAccountId();
for (final AccountProjectWatch.Key keyId : keys) {
@@ -198,6 +211,7 @@ class AccountServiceImpl extends BaseServiceImplementation implements
});
}
@Override
public void myAgreements(final AsyncCallback<AgreementInfo> callback) {
agreementInfoFactory.create().to(callback);
}

View File

@@ -30,11 +30,13 @@ class ChangeDetailServiceImpl implements ChangeDetailService {
this.patchSetDetail = patchSetDetail;
}
@Override
public void patchSetDetail(PatchSet.Id id,
AsyncCallback<PatchSetDetail> callback) {
patchSetDetail2(null, id, null, callback);
}
@Override
public void patchSetDetail2(PatchSet.Id baseId, PatchSet.Id id,
AccountDiffPreference diffPrefs, AsyncCallback<PatchSetDetail> callback) {
patchSetDetail.create(baseId, id, diffPrefs).to(callback);

View File

@@ -47,6 +47,7 @@ class PatchDetailServiceImpl extends BaseServiceImplementation implements
this.changeControlFactory = changeControlFactory;
}
@Override
public void patchScript(final Patch.Key patchKey, final PatchSet.Id psa,
final PatchSet.Id psb, final AccountDiffPreference dp,
final AsyncCallback<PatchScript> callback) {