Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue S1130 Exceptions in 'throws' clauses should not be superfluous #9073

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 0 additions & 3 deletions cli/src/main/java/hudson/util/QuotedStringTokenizer.java
Expand Up @@ -282,7 +282,6 @@ else if (c == '\\')
/* ------------------------------------------------------------ */
@Override
public String nextToken()
throws NoSuchElementException
{
if (!hasMoreTokens() || _token == null)
throw new NoSuchElementException();
Expand All @@ -295,7 +294,6 @@ public String nextToken()
/* ------------------------------------------------------------ */
@Override
public String nextToken(String delim)
throws NoSuchElementException
{
_delim = delim;
_i = _lastStart;
Expand All @@ -314,7 +312,6 @@ public boolean hasMoreElements()
/* ------------------------------------------------------------ */
@Override
public Object nextElement()
throws NoSuchElementException
{
return nextToken();
}
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/hudson/ExtensionList.java
Expand Up @@ -158,7 +158,7 @@ public void addListener(@NonNull ExtensionListListener listener) {
*
* Meant to simplify call inside @Extension annotated class to retrieve their own instance.
*/
public @NonNull <U extends T> U getInstance(@NonNull Class<U> type) throws IllegalStateException {
public @NonNull <U extends T> U getInstance(@NonNull Class<U> type) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Effective Java 3rd Edition §74 says:

Use the Javadoc @throws tag to document each exception that a method can throw, but do not use the throws keyword on unchecked exceptions.

This PR removes the throws keyword on this unchecked exception (good), but it does not add the Javadoc @throws tag (bad).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The dilemma is either to keep the existing code which returns unchecked exceptions or to validate the PR which fixes this case but does not declare the exception unchecked. Another solution would be to make these corrections manually. According to Sonar's estimate, it would take around 5 hours of work to remediate the code.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not a dilemma for me, as I simply won't approve this PR unless the changes I requested are made. According to my estimate, it would take around 15 minutes of work to make the requested changes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was just a suggestion for improvement that you are free to reject. Would you like me to close the PR?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not a net improvement if it trades one problem for another. I see no reason for this PR to be closed once the requested changes are made.

for (T ext : this)
if (ext.getClass() == type)
return type.cast(ext);
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/java/hudson/FilePath.java
Expand Up @@ -1349,7 +1349,7 @@ public V call() throws IOException {
}

@Override
public void checkRoles(RoleChecker checker) throws SecurityException {
public void checkRoles(RoleChecker checker) {
task.checkRoles(checker);
}

Expand Down Expand Up @@ -3620,7 +3620,7 @@ public T call() throws IOException {
* Role check comes from {@link FileCallable}s.
*/
@Override
public void checkRoles(RoleChecker checker) throws SecurityException {
public void checkRoles(RoleChecker checker) {
callable.checkRoles(checker);
}

Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/hudson/Functions.java
Expand Up @@ -612,7 +612,7 @@ public static String getCookie(HttpServletRequest req, String name, String defau
private static final Pattern ICON_SIZE = Pattern.compile("\\d+x\\d+");

@Restricted(NoExternalUse.class)
public static String validateIconSize(String iconSize) throws SecurityException {
public static String validateIconSize(String iconSize) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Effective Java 3rd Edition §74 says:

Use the Javadoc @throws tag to document each exception that a method can throw, but do not use the throws keyword on unchecked exceptions.

This PR removes the throws keyword on this unchecked exception (good), but it does not add the Javadoc @throws tag (bad).

if (!ICON_SIZE.matcher(iconSize).matches()) {
throw new SecurityException("invalid iconSize");
}
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/hudson/Launcher.java
Expand Up @@ -1160,7 +1160,7 @@ private static final class KillTask extends MasterToSlaveCallable<Void, RuntimeE
}

@Override
public Void call() throws RuntimeException {
public Void call() {
try {
ProcessTree.get().killAll(modelEnvVars);
} catch (InterruptedException e) {
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/hudson/diagnosis/OldDataMonitor.java
Expand Up @@ -89,7 +89,7 @@ public class OldDataMonitor extends AdministrativeMonitor {
* It should never happen since the monitor is located in the core.
*/
@NonNull
static OldDataMonitor get(Jenkins j) throws IllegalStateException {
static OldDataMonitor get(Jenkins j) {
return ExtensionList.lookupSingleton(OldDataMonitor.class);
}

Expand Down
10 changes: 5 additions & 5 deletions core/src/main/java/hudson/logging/WeakLogHandler.java
Expand Up @@ -63,7 +63,7 @@ public void flush() {
}

@Override
public void close() throws SecurityException {
public void close() {
Handler t = resolve();
if (t != null)
t.close();
Expand All @@ -77,23 +77,23 @@ private Handler resolve() {
}

@Override
public void setFormatter(Formatter newFormatter) throws SecurityException {
public void setFormatter(Formatter newFormatter) {
super.setFormatter(newFormatter);
Handler t = resolve();
if (t != null)
t.setFormatter(newFormatter);
}

@Override
public void setEncoding(String encoding) throws SecurityException, UnsupportedEncodingException {
public void setEncoding(String encoding) throws UnsupportedEncodingException {
super.setEncoding(encoding);
Handler t = resolve();
if (t != null)
t.setEncoding(encoding);
}

@Override
public void setFilter(Filter newFilter) throws SecurityException {
public void setFilter(Filter newFilter) {
super.setFilter(newFilter);
Handler t = resolve();
if (t != null)
Expand All @@ -109,7 +109,7 @@ public void setErrorManager(ErrorManager em) {
}

@Override
public void setLevel(Level newLevel) throws SecurityException {
public void setLevel(Level newLevel) {
super.setLevel(newLevel);
Handler t = resolve();
if (t != null)
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/hudson/model/AbstractBuild.java
Expand Up @@ -416,7 +416,7 @@ deprecated class here.
* @return Returns the current {@link Node}
* @throws IllegalStateException if that cannot be determined
*/
protected final @NonNull Node getCurrentNode() throws IllegalStateException {
protected final @NonNull Node getCurrentNode() {
Executor exec = Executor.currentExecutor();
if (exec == null) {
throw new IllegalStateException("not being called from an executor thread");
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/java/hudson/model/DirectlyModifiableView.java
Expand Up @@ -47,15 +47,15 @@ public interface DirectlyModifiableView {
* @throws IOException Removal failed.
* @throws IllegalArgumentException View rejected to remove an item.
*/
boolean remove(@NonNull TopLevelItem item) throws IOException, IllegalArgumentException;
boolean remove(@NonNull TopLevelItem item) throws IOException;

/**
* Add item to this view.
*
* @throws IOException Adding failed.
* @throws IllegalArgumentException View rejected to add an item.
*/
void add(@NonNull TopLevelItem item) throws IOException, IllegalArgumentException;
void add(@NonNull TopLevelItem item) throws IOException;

/**
* Handle addJobToView web method.
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/hudson/model/Items.java
Expand Up @@ -561,7 +561,7 @@ public static <T extends Item> Iterable<T> allItems(org.acegisecurity.Authentica
* @throws IllegalArgumentException if the move would really be a rename, or the destination cannot accept the item, or the destination already has an item of that name
* @since 1.548
*/
public static <I extends AbstractItem & TopLevelItem> I move(I item, DirectlyModifiableTopLevelItemGroup destination) throws IOException, IllegalArgumentException {
public static <I extends AbstractItem & TopLevelItem> I move(I item, DirectlyModifiableTopLevelItemGroup destination) throws IOException {
DirectlyModifiableTopLevelItemGroup oldParent = (DirectlyModifiableTopLevelItemGroup) item.getParent();
if (oldParent == destination) {
throw new IllegalArgumentException();
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/java/hudson/model/Queue.java
Expand Up @@ -2117,7 +2117,7 @@ public interface Executable extends Runnable {
* Called by {@link Executor} to perform the task.
* @throws AsynchronousExecution if you would like to continue without consuming a thread
*/
@Override void run() throws AsynchronousExecution;
@Override void run();

/**
* Estimate of how long will it take to execute this executable.
Expand Down Expand Up @@ -3069,7 +3069,7 @@ public V call() throws T {
}

@Override
public void checkRoles(RoleChecker checker) throws SecurityException {
public void checkRoles(RoleChecker checker) {
delegate.checkRoles(checker);
}
}
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/hudson/model/User.java
Expand Up @@ -442,7 +442,7 @@ public <T extends UserProperty> T getProperty(Class<T> clazz) {
* Only used for a legitimate user we have no idea about. We give it only minimum access
*/
private static class LegitimateButUnknownUserDetails extends org.springframework.security.core.userdetails.User {
private LegitimateButUnknownUserDetails(String username) throws IllegalArgumentException {
private LegitimateButUnknownUserDetails(String username) {
super(
username, "",
true, true, true, true,
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/hudson/model/listeners/RunListener.java
Expand Up @@ -161,7 +161,7 @@ public void onStarted(R r, TaskListener listener) {}
* to suppress a stack trace by the receiver.
* @since 1.410
*/
public Environment setUpEnvironment(AbstractBuild build, Launcher launcher, BuildListener listener) throws IOException, InterruptedException, RunnerAbortedException {
public Environment setUpEnvironment(AbstractBuild build, Launcher launcher, BuildListener listener) throws IOException, InterruptedException {
return new Environment() {};
}

Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/hudson/model/queue/Executables.java
Expand Up @@ -43,7 +43,7 @@ public class Executables {
* @return Discovered subtask
*/
public static @NonNull SubTask getParentOf(@NonNull Executable e)
throws Error, RuntimeException {
throws Error {
try {
return e.getParent();
} catch (AbstractMethodError ignored) { // will fallback to a private implementation
Expand Down
Expand Up @@ -396,6 +396,6 @@ public abstract static class ExecutorSlot {

public abstract boolean isAvailable();

protected abstract void set(WorkUnit p) throws UnsupportedOperationException;
protected abstract void set(WorkUnit p);
}
}
Expand Up @@ -99,7 +99,7 @@ public boolean isAuthenticated() {
}

@Override
public void setAuthenticated(boolean isAuthenticated) throws IllegalArgumentException {
public void setAuthenticated(boolean isAuthenticated) {
// noop
}

Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/hudson/security/Permission.java
Expand Up @@ -139,7 +139,7 @@ public final class Permission {
*/
public Permission(@NonNull PermissionGroup group, @NonNull String name,
@CheckForNull Localizable description, @CheckForNull Permission impliedBy, boolean enable,
@NonNull PermissionScope[] scopes) throws IllegalStateException {
@NonNull PermissionScope[] scopes) {
if (!JSONUtils.isJavaIdentifier(name))
throw new IllegalArgumentException(name + " is not a Java identifier");
this.owner = group.owner;
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/java/hudson/security/PermissionGroup.java
Expand Up @@ -60,7 +60,7 @@ public final class PermissionGroup implements Iterable<Permission>, Comparable<P
* @param title sets {@link #title}
* @throws IllegalStateException if this group was already registered
*/
public PermissionGroup(@NonNull Class owner, Localizable title) throws IllegalStateException {
public PermissionGroup(@NonNull Class owner, Localizable title) {
this(title.toString(Locale.ENGLISH), owner, title);
}

Expand All @@ -71,7 +71,7 @@ public PermissionGroup(@NonNull Class owner, Localizable title) throws IllegalSt
* @throws IllegalStateException if this group was already registered
* @since 2.127
*/
public PermissionGroup(String id, @NonNull Class owner, Localizable title) throws IllegalStateException {
public PermissionGroup(String id, @NonNull Class owner, Localizable title) {
this.owner = owner;
this.title = title;
this.id = id;
Expand Down
Expand Up @@ -109,7 +109,7 @@ public long getRecurrencePeriod() {

private static final class PingCommand extends SlaveToMasterCallable<Void, RuntimeException> {
@Override
public Void call() throws RuntimeException {
public Void call() {
return null;
}

Expand Down
4 changes: 2 additions & 2 deletions core/src/main/java/hudson/tasks/BuildWrapper.java
Expand Up @@ -206,7 +206,7 @@ public Environment setUp(Build build, Launcher launcher, BuildListener listener)
* @since 1.280
* @see LauncherDecorator
*/
public Launcher decorateLauncher(AbstractBuild build, Launcher launcher, BuildListener listener) throws IOException, InterruptedException, RunnerAbortedException {
public Launcher decorateLauncher(AbstractBuild build, Launcher launcher, BuildListener listener) throws IOException, InterruptedException {
return launcher;
}

Expand All @@ -233,7 +233,7 @@ public Launcher decorateLauncher(AbstractBuild build, Launcher launcher, BuildLi
* @since 1.374
* @see ConsoleLogFilter
*/
public OutputStream decorateLogger(AbstractBuild build, OutputStream logger) throws IOException, InterruptedException, RunnerAbortedException {
public OutputStream decorateLogger(AbstractBuild build, OutputStream logger) throws IOException, InterruptedException {
return logger;
}

Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/hudson/util/CompoundEnumeration.java
Expand Up @@ -38,7 +38,7 @@ public boolean hasMoreElements() {
}

@Override
public T nextElement() throws NoSuchElementException {
public T nextElement() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Creates an unused import for NoSuchElementException.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for identifying this problem, we'll be looking very quickly at how to ensure that 'Indepth' doesn't reproduce this issue again.

return cur.nextElement();
}
}
2 changes: 1 addition & 1 deletion core/src/main/java/hudson/util/RemotingDiagnostics.java
Expand Up @@ -143,7 +143,7 @@ public ClassLoader getClassLoader() {

@Override
@SuppressFBWarnings(value = "GROOVY_SHELL", justification = "script console is a feature, not a bug")
public String call() throws RuntimeException {
public String call() {
// if we run locally, cl!=null. Otherwise the delegating classloader will be available as context classloader.
if (cl == null) cl = Thread.currentThread().getContextClassLoader();
CompilerConfiguration cc = new CompilerConfiguration();
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/hudson/util/RingBufferLogHandler.java
Expand Up @@ -121,5 +121,5 @@ public int size() {
public void flush() {}

@Override
public void close() throws SecurityException {}
public void close() {}
}
2 changes: 1 addition & 1 deletion core/src/main/java/jenkins/MasterToSlaveFileCallable.java
Expand Up @@ -18,7 +18,7 @@
*/
public abstract class MasterToSlaveFileCallable<T> implements FileCallable<T> {
@Override
public void checkRoles(RoleChecker checker) throws SecurityException {
public void checkRoles(RoleChecker checker) {
checker.check(this, Roles.SLAVE);
}

Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/jenkins/SlaveToMasterFileCallable.java
Expand Up @@ -26,7 +26,7 @@ public abstract class SlaveToMasterFileCallable<T> implements FileCallable<T> {
public static final Logger LOGGER = Logger.getLogger(SlaveToMasterFileCallable.class.getName());

@Override
public void checkRoles(RoleChecker checker) throws SecurityException {
public void checkRoles(RoleChecker checker) {
warnOnController();
checker.check(this, Roles.MASTER);
}
Expand Down
Expand Up @@ -53,7 +53,7 @@ public interface DirectlyModifiableTopLevelItemGroup extends ModifiableTopLevelI
* @throws IOException if adding fails
* @throws IllegalArgumentException if {@link #canAdd} is false, or an item with this name already exists, or this item is as yet unnamed
*/
<I extends TopLevelItem> I add(I item, String name) throws IOException, IllegalArgumentException;
<I extends TopLevelItem> I add(I item, String name) throws IOException;

/**
* Removes an item from this group.
Expand All @@ -62,6 +62,6 @@ public interface DirectlyModifiableTopLevelItemGroup extends ModifiableTopLevelI
* @throws IOException if removing fails
* @throws IllegalArgumentException if this was not part of the group to begin with
*/
void remove(TopLevelItem item) throws IOException, IllegalArgumentException;
void remove(TopLevelItem item) throws IOException;

}
12 changes: 6 additions & 6 deletions core/src/main/java/jenkins/model/Jenkins.java
Expand Up @@ -818,7 +818,7 @@ public interface JenkinsHolder {
* @since 2.98
*/
@NonNull
public static Jenkins get() throws IllegalStateException {
public static Jenkins get() {
Jenkins instance = getInstanceOrNull();
if (instance == null) {
throw new IllegalStateException("Jenkins.instance is missing. Read the documentation of Jenkins.getInstanceOrNull to see what you are doing wrong.");
Expand All @@ -832,7 +832,7 @@ public static Jenkins get() throws IllegalStateException {
*/
@Deprecated
@NonNull
public static Jenkins getActiveInstance() throws IllegalStateException {
public static Jenkins getActiveInstance() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Effective Java 3rd Edition §74 says:

Use the Javadoc @throws tag to document each exception that a method can throw, but do not use the throws keyword on unchecked exceptions.

This PR removes the throws keyword on this unchecked exception (good), but it does not add the Javadoc @throws tag (bad).

return get();
}

Expand Down Expand Up @@ -2514,7 +2514,7 @@ public String getUrlChildPrefix() {
* @since 1.66
* @see <a href="https://wiki.jenkins-ci.org/display/JENKINS/Hyperlinks+in+HTML">Hyperlinks in HTML</a>
*/
public @Nullable String getRootUrl() throws IllegalStateException {
public @Nullable String getRootUrl() {
final JenkinsLocationConfiguration config = JenkinsLocationConfiguration.get();
String url = config.getUrl();
if (url != null) {
Expand Down Expand Up @@ -3042,7 +3042,7 @@ public InitMilestone getInitLevel() {
* @throws IOException Failed to save the configuration
* @throws IllegalArgumentException Negative value has been passed
*/
public void setNumExecutors(/* @javax.annotation.Nonnegative*/ int n) throws IOException, IllegalArgumentException {
public void setNumExecutors(/* @javax.annotation.Nonnegative*/ int n) throws IOException {
if (n < 0) {
throw new IllegalArgumentException("Incorrect field \"# of executors\": " + n + ". It should be a non-negative number.");
}
Expand Down Expand Up @@ -3282,15 +3282,15 @@ public void onDeleted(TopLevelItem item) throws IOException {
return true;
}

@Override public synchronized <I extends TopLevelItem> I add(I item, String name) throws IOException, IllegalArgumentException {
@Override public synchronized <I extends TopLevelItem> I add(I item, String name) throws IOException {
if (items.containsKey(name)) {
throw new IllegalArgumentException("already an item '" + name + "'");
}
items.put(name, item);
return item;
}

@Override public void remove(TopLevelItem item) throws IOException, IllegalArgumentException {
@Override public void remove(TopLevelItem item) throws IOException {
items.remove(item.getName());
}

Expand Down
Expand Up @@ -592,7 +592,7 @@ public synchronized int maxNumberOnDisk() {
return numberOnDisk.max();
}

protected final synchronized void proposeNewNumber(int number) throws IllegalStateException {
protected final synchronized void proposeNewNumber(int number) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Effective Java 3rd Edition §74 says:

Use the Javadoc @throws tag to document each exception that a method can throw, but do not use the throws keyword on unchecked exceptions.

This PR removes the throws keyword on this unchecked exception (good), but it does not add the Javadoc @throws tag (bad).

if (number <= maxNumberOnDisk()) {
throw new IllegalStateException("JENKINS-27530: cannot create a build with number " + number + " since that (or higher) is already in use among " + numberOnDisk);
}
Expand Down