Skip to content
Permalink
Browse files

[JENKINS-37566] - Annotate URLish methods and handle NPE in RemoteCla…

…ssLoader. (#170)

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

The NPE seems to be  non-realistic (why would RemoteClassloader construct URL from File?), but it’s better to fix it just in case.

* Make URLish#from() non-null as suggested by @jglick + some Javadoc
  • Loading branch information...
oleg-nenashev committed Jun 25, 2017
1 parent 35065e7 commit 5e1e8883ca94889d8f5598945965f00c4a908912
@@ -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) {
@@ -485,7 +490,12 @@ public URL findResource(String name) {
}
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();
}

/**
@@ -4,6 +4,7 @@
import java.net.URL;
import java.net.URLClassLoader;
import java.util.concurrent.ExecutionException;
import javax.annotation.Nonnull;

/**
* {@link ResourceImageRef} that points to a resource inside a jar file.
@@ -65,6 +66,7 @@ protected URLish adapt(URL jar) throws ExecutionException {
};
}

@Nonnull
private URL toResourceURL(URL jar, String resourcePath) throws IOException {
if (path!=null)
resourcePath = path;
@@ -3,6 +3,8 @@
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;

/**
* Something that's effectively URL.
@@ -16,20 +18,28 @@
abstract class URLish {
private URLish() {}

/**
* Converts URLish to the standard {@link URL} type.
* @return URL or {@code null} if the target destination is known to be non-existent.
* @throws MalformedURLException URL cannot be constructed
*/
@CheckForNull
abstract URL toURL() throws MalformedURLException;

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

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

static URLish from(final File f) {
if (f==null) return null;
@Nonnull
static URLish from(@Nonnull final File f) {
return new URLish() {
@Override
URL toURL() throws MalformedURLException {
@@ -54,6 +54,7 @@ public static void copy(InputStream in, OutputStream out) throws IOException {
}
}

@Nonnull
static File makeResource(String name, byte[] image) throws IOException {
File tmpFile = createTempDir();
File resource = new File(tmpFile, name);

0 comments on commit 5e1e888

Please sign in to comment.
You can’t perform that action at this time.