Skip to content

Commit 9d0cfd1

Browse files
committed
8294948: Document IllegalArgumentException and NullPointerException thrown by URLStreamHandler::parseURL and URLStreamHandler::setURL
Reviewed-by: jpai, aefimov, alanb, michaelm
1 parent dcd4650 commit 9d0cfd1

File tree

2 files changed

+52
-7
lines changed

2 files changed

+52
-7
lines changed

src/java.base/share/classes/java/net/URL.java

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,18 @@
170170
* the recommendations advised in <a
171171
* href="https://tools.ietf.org/html/rfc3986#section-7">RFC3986,
172172
* Section 7, Security Considerations</a>.
173+
* <p>
174+
* All {@code URL} constructors may throw {@link MalformedURLException}.
175+
* In particular, if the underlying {@link URLStreamHandler}
176+
* implementation rejects, or is known to reject, any of the parameters,
177+
* {@link MalformedURLException} may be thrown.
178+
* Typically, a constructor that calls the stream handler's {@linkplain
179+
* URLStreamHandler#parseURL(URL, String, int, int) parseURL method} may
180+
* throw {@code MalformedURLException} if the underlying stream handler
181+
* implementation of that method throws {@code IllegalArgumentException}.
182+
* However, which checks are performed, or not, by the stream handlers
183+
* is implementation dependent, and callers should not rely on such
184+
* checks for full URL validation.
173185
*
174186
* @author James Gosling
175187
* @since 1.0
@@ -352,7 +364,9 @@ public final class URL implements java.io.Serializable {
352364
* @param port the port number on the host.
353365
* @param file the file on the host
354366
* @throws MalformedURLException if an unknown protocol or the port
355-
* is a negative number other than -1
367+
* is a negative number other than -1, or if the
368+
* underlying stream handler implementation rejects,
369+
* or is known to reject, the {@code URL}
356370
* @see java.lang.System#getProperty(java.lang.String)
357371
* @see java.net.URL#setURLStreamHandlerFactory(
358372
* java.net.URLStreamHandlerFactory)
@@ -380,7 +394,9 @@ public URL(String protocol, String host, int port, String file)
380394
* @param protocol the name of the protocol to use.
381395
* @param host the name of the host.
382396
* @param file the file on the host.
383-
* @throws MalformedURLException if an unknown protocol is specified.
397+
* @throws MalformedURLException if an unknown protocol is specified,
398+
* or if the underlying stream handler implementation
399+
* rejects, or is known to reject, the {@code URL}
384400
* @see java.net.URL#URL(java.lang.String, java.lang.String,
385401
* int, java.lang.String)
386402
*/
@@ -415,7 +431,9 @@ public URL(String protocol, String host, String file)
415431
* @param file the file on the host
416432
* @param handler the stream handler for the URL.
417433
* @throws MalformedURLException if an unknown protocol or the port
418-
* is a negative number other than -1
434+
* is a negative number other than -1,
435+
* or if the underlying stream handler implementation
436+
* rejects, or is known to reject, the {@code URL}
419437
* @throws SecurityException
420438
* if a security manager exists and its
421439
* {@code checkPermission} method doesn't allow
@@ -510,7 +528,10 @@ public URL(String protocol, String host, int port, String file,
510528
* @throws MalformedURLException if no protocol is specified, or an
511529
* unknown protocol is found, or {@code spec} is {@code null},
512530
* or the parsed URL fails to comply with the specific syntax
513-
* of the associated protocol.
531+
* of the associated protocol, or the
532+
* underlying stream handler's {@linkplain
533+
* URLStreamHandler#parseURL parseURL method} throws
534+
* {@code IllegalArgumentException}
514535
* @see java.net.URL#URL(java.net.URL, java.lang.String)
515536
*/
516537
public URL(String spec) throws MalformedURLException {
@@ -522,7 +543,7 @@ public URL(String spec) throws MalformedURLException {
522543
*
523544
* The new URL is created from the given context URL and the spec
524545
* argument as described in
525-
* RFC2396 &quot;Uniform Resource Identifiers : Generic * Syntax&quot; :
546+
* RFC2396 &quot;Uniform Resource Identifiers : Generic Syntax&quot; :
526547
* <blockquote><pre>
527548
* &lt;scheme&gt;://&lt;authority&gt;&lt;path&gt;?&lt;query&gt;#&lt;fragment&gt;
528549
* </pre></blockquote>
@@ -554,12 +575,19 @@ public URL(String spec) throws MalformedURLException {
554575
* <p>
555576
* For a more detailed description of URL parsing, refer to RFC2396.
556577
*
578+
* @implSpec Parsing the URL includes calling the {@link
579+
* URLStreamHandler#parseURL(URL, String, int, int) parseURL} method on the
580+
* selected handler.
581+
*
557582
* @param context the context in which to parse the specification.
558583
* @param spec the {@code String} to parse as a URL.
559584
* @throws MalformedURLException if no protocol is specified, or an
560585
* unknown protocol is found, or {@code spec} is {@code null},
561586
* or the parsed URL fails to comply with the specific syntax
562-
* of the associated protocol.
587+
* of the associated protocol, or the
588+
* underlying stream handler's {@linkplain
589+
* URLStreamHandler#parseURL parseURL method} throws
590+
* {@code IllegalArgumentException}
563591
* @see java.net.URL#URL(java.lang.String, java.lang.String,
564592
* int, java.lang.String)
565593
* @see java.net.URLStreamHandler
@@ -575,13 +603,20 @@ public URL(URL context, String spec) throws MalformedURLException {
575603
* within a specified context. If the handler is null, the parsing
576604
* occurs as with the two argument constructor.
577605
*
606+
* @implSpec Parsing the URL includes calling the {@link
607+
* URLStreamHandler#parseURL(URL, String, int, int) parseURL} method on the
608+
* selected handler.
609+
*
578610
* @param context the context in which to parse the specification.
579611
* @param spec the {@code String} to parse as a URL.
580612
* @param handler the stream handler for the URL.
581613
* @throws MalformedURLException if no protocol is specified, or an
582614
* unknown protocol is found, or {@code spec} is {@code null},
583615
* or the parsed URL fails to comply with the specific syntax
584-
* of the associated protocol.
616+
* of the associated protocol, or the
617+
* underlying stream handler's {@linkplain
618+
* URLStreamHandler#parseURL(URL, String, int, int)
619+
* parseURL method} throws {@code IllegalArgumentException}
585620
* @throws SecurityException
586621
* if a security manager exists and its
587622
* {@code checkPermission} method doesn't allow

src/java.base/share/classes/java/net/URLStreamHandler.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,10 @@ protected URLConnection openConnection(URL u, Proxy p) throws IOException {
129129
* end of the string or the position of the
130130
* "{@code #}" character, if present. All information
131131
* after the sharp sign indicates an anchor.
132+
* @throws IllegalArgumentException if the implementation of the protocol
133+
* handler rejects any of the given parameters
134+
* @throws NullPointerException if {@code u} is {@code null},
135+
* or if {@code start < limit} and {@code spec} is {@code null}
132136
*/
133137
protected void parseURL(URL u, String spec, int start, int limit) {
134138
// These fields may receive context content if this was relative URL
@@ -497,6 +501,9 @@ protected String toExternalForm(URL u) {
497501
* @param ref the reference.
498502
* @throws SecurityException if the protocol handler of the URL is
499503
* different from this one
504+
* @throws IllegalArgumentException if the implementation of the protocol
505+
* handler rejects any of the given parameters
506+
* @throws NullPointerException if {@code u} is {@code null}
500507
* @since 1.3
501508
*/
502509
protected void setURL(URL u, String protocol, String host, int port,
@@ -539,6 +546,9 @@ protected void setURL(URL u, String protocol, String host, int port,
539546
* @param ref the reference.
540547
* @throws SecurityException if the protocol handler of the URL is
541548
* different from this one
549+
* @throws IllegalArgumentException if the implementation of the protocol
550+
* handler rejects any of the given parameters
551+
* @throws NullPointerException if {@code u} is {@code null}
542552
* @deprecated Use setURL(URL, String, String, int, String, String, String,
543553
* String);
544554
*/

0 commit comments

Comments
 (0)