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

Conversation

oleg-nenashev
Copy link
Member

@oleg-nenashev oleg-nenashev commented Jun 20, 2017

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

https://issues.jenkins-ci.org/browse/JENKINS-37566

@reviewbybees

…ssLoader.

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

ghost commented Jun 20, 2017

This pull request originates from a CloudBees employee. At CloudBees, we require that all pull requests be reviewed by other CloudBees employees before we seek to have the change accepted. If you want to learn more about our process please see this explanation.

@recampbell recampbell requested a review from jglick June 20, 2017 15:44
@oleg-nenashev oleg-nenashev reopened this Jun 21, 2017
Copy link
Member

@jglick jglick left a comment

Choose a reason for hiding this comment

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

Probably not introducing a bug, but does not seem to be correctly solving it either.

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

@@ -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.
Copy link
Member

Choose a reason for hiding this comment

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

Why not throw, say, FileNotFoundException in such a case, so this can be @Nonnull?

Copy link
Member Author

Choose a reason for hiding this comment

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

We could, of course.
Can do it though now we are rather in the code refactoring area than in the NPE warning fix one

Copy link
Member Author

Choose a reason for hiding this comment

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

@jglick After some consideration I feel returning null is a valid behavior. E.g. this code uses abstract URLish, which may be both URL and File pointers. I could refactor all the code to do the same with exceptions, but I do not feel it improves the code much. WDYT?

public URL findResource(String name) {
        // first attempt to load from locally fetched jars
        URL url = super.findResource(name);
        final Channel channel = channel();
        if(url!=null || channel == null || !channel.isRemoteClassLoadingAllowed())   return url;

        try {
            if(resourceMap.containsKey(name)) {
                URLish f = resourceMap.get(name);
                if(f==null) return null;    // no such resource
                URL u = f.toURL();
                if (u!=null)    return u;
            }

Copy link
Member

Choose a reason for hiding this comment

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

IIUC it is the line

if (u!=null)    return u;

that is under discussion. I think it makes sense to have toURL throw an exception if it is definitely broken, and for the caller to decide what to do with that—log it, in this case (whatever the catch block does).

Or fix this method to be @Nonnull, which seems more natural, and leave it up to the consumer of the resulting URL to deal with missing resources—they would anyway need to be handling FileNotFoundException, so it seems pointless to be prechecking that condition.

IOW lines 46–50 seem stupid.

@recampbell recampbell requested a review from jglick June 23, 2017 15:43
Copy link
Member

@jglick jglick left a comment

Choose a reason for hiding this comment

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

Does not make things any worse, though it misses an opportunity to make the code clearer and more robust.

@@ -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.
Copy link
Member

Choose a reason for hiding this comment

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

IIUC it is the line

if (u!=null)    return u;

that is under discussion. I think it makes sense to have toURL throw an exception if it is definitely broken, and for the caller to decide what to do with that—log it, in this case (whatever the catch block does).

Or fix this method to be @Nonnull, which seems more natural, and leave it up to the consumer of the resulting URL to deal with missing resources—they would anyway need to be handling FileNotFoundException, so it seems pointless to be prechecking that condition.

IOW lines 46–50 seem stupid.

@oleg-nenashev
Copy link
Member Author

Does not make things any worse, though it misses an opportunity to make the code clearer and more robust.

I will consider it for the next refactoring phases. It is definitely not the biggest issue in Remoting

@oleg-nenashev oleg-nenashev merged commit 5e1e888 into jenkinsci:master Jun 25, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants