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

[JENKINS-37566] - Annotate URLish methods and handle NPE in RemoteClassLoader. #170

Merged
merged 2 commits into from
Jun 25, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/main/java/hudson/remoting/RemoteClassLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,11 @@ public URL findResource(String name) {
}
}

/**
* @return {@code null} if one of the URLs cannot be converted.
* E.g. when the referenced file does not exist.
*/
@CheckForNull
private static Vector<URL> toURLs(Vector<URLish> src) throws MalformedURLException {
Vector<URL> r = new Vector<URL>(src.size());
for (URLish s : src) {
Expand Down Expand Up @@ -485,7 +490,12 @@ public Enumeration<URL> findResources(String name) throws IOException {
}
resourcesMap.put(name,v);

return toURLs(v).elements();
Vector<URL> resURLs = toURLs(v);
if (resURLs == null) {
// TODO: Better than NPE, but ideally needs correct error propagation from URLish
throw new IOException("One of the URLish objects cannot be converted to URL");
}
return resURLs.elements();
}

/**
Expand Down
12 changes: 10 additions & 2 deletions src/main/java/hudson/remoting/URLish.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;

/**
* Something that's effectively URL.
Expand All @@ -16,19 +19,24 @@
abstract class URLish {
private URLish() {}

@CheckForNull
abstract URL toURL() throws MalformedURLException;

static URLish from(final URL url) {
@Nullable
static URLish from(@CheckForNull final URL url) {
if (url==null) return null;

return new URLish() {
@Override
@Nonnull
URL toURL() {
return url;
}
};
}

static URLish from(final File f) {
@Nullable
static URLish from(@CheckForNull final File f) {
Copy link
Member

Choose a reason for hiding this comment

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

This is internal, so why not make it @Nonnull? Then toURL could also be @Nonnull, as could toURLs. If you want to signal an error, throw an exception.

Copy link
Member Author

Choose a reason for hiding this comment

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

makes sense

if (f==null) return null;
return new URLish() {
@Override
Expand Down