How are URLs written once?

I went through the java.net package and read this :

URLs are once-only objects. When you created the URL object, you cannot change any of its attributes (protocol, host name, file name, or port number).

But if we look at java.net.URL, we find this:

protected void set(String protocol, String host, int port, String file, String ref) 

and

 protected void set(String protocol, String host, int port, String authority, String userInfo, String path, String query, String ref) 

So, I know that these are protected methods, but they can be accessed through

 public static void setURLStreamHandlerFactory(URLStreamHandlerFactory fac) 

So my question is: if this statement I quoted above is vague, or did I just misunderstand it?

+5
source share
3 answers

URLs are write-once objects using you (which just use URL objects). You cannot change them.

URL must be created and initialized internally, sometime, so someone needs to modify them. Optimally, this would have to be done in your constructor, but this would lose flexibility. URL can indicate a variation of objects, and an implementation for accessing and processing them is not limited to the standard library; you can write implementations for processing user protocols.

Btw, in Java 8 they are not protected , but private, but even more restrictive (but not so important, since the URL class is declared final anyway, so you cannot subclass it).

Edit: Example

The URL class has many constructors: some allow you to specify different parts of the URL, such as protocol, host, port, etc., and there are some that allow you to specify the URL as one String : spec .

In the latter case (if the URL is specified as one String ), various parts of the URL (protocol, host, port, etc.) must be parsed from String . This parsing is done using the URLStreamHandler , which when analyzing String in the implementation of URLStreamHandler.parseURL() must use the URL.set() method to set the different parts back to the URL class, because the fields containing these parts are private . and there are no installation methods for them.

Because parsing is done in another class ( URLStreamHandler ), the URL provides a non-public method for retrieving the result of the parsing. This provides flexibility as you can replace / extend the URLStreamHandler , but still allow the URL declare final and be "write-once".

+4
source

Quote from the official documentation (net / URL.html) for the "set" methods

Sets the URL fields. This is not a public method, so only URLStreamHandlers can change URL fields. URLs are otherwise persistent .

Otherwise, it makes me think that only 2 "installed" methods can change the URL object, and you cannot change it using any other operation than the "set" method. They are an exception;)

+1
source

If you look at the documentation for the class URL, there are public Getters (e.g. getPort ), but not Setter methods. The set method is protected and cannot be accessed from outside the package.

You can reassign the object to a URL, although provided that your object is not final.

eg.

 URL url1 = new URL ("http://www.example1"); URL url2 = new URL ("http://www.example2"); url1 = url2; 

I think the expression you provided from the documentation is very vague

+1
source

Source: https://habr.com/ru/post/1206321/


All Articles