RFR JDK-4880778

John Zavgren john.zavgren at oracle.com
Sat Feb 2 06:39:01 PST 2013


Greetings:

Please consider the following modification of our classes for handling URLs.

http://cr.openjdk.java.net/~jzavgren/4880778/webrev.01/

---- background ----
See http://java.sun.com/j2se/1.4.2/docs/api/java/net/URL.html.
The URL class is declared final yet has two protected methods:

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

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


Since the final class can't be subclassed these methods can't be accessed.
They could be package protected instead. (i.e. no access specifier)
--------------------
Solution:
1.) I made the set method in the URL class "package protected"
2.) I edited the java doc documentation to eliminate reference to the set method in the URL class from the URLStreamHandler class

3.) I wrote a test program (URLTest.java) (at the end of this message) that attempts to call the set method from a URL object...
When there is no package statement: 

//package java.net;

the compilation fails
java/net/URLTest.java:28: error: set(String,String,int,String,String,String,String,String) is not public in URL; cannot be accessed from outside package
    myURL.set("ftp", "vpn.zavgren.com", 77, "lugnut.txt", "","","","");
         ^
1 error
4.) When there is a package statement in the source code:

package java.net;

the code compiles but it creates a run time error.
jzavgren at ubuntuVM:~/code/java/URL$ java java.net.URLTest
Exception in thread "main" java.lang.SecurityException: Prohibited package name: java.net
        at java.lang.ClassLoader.preDefineClass(ClassLoader.java:651)
        at java.lang.ClassLoader.defineClass(ClassLoader.java:785)
        at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
        at java.net.URLClassLoader.defineClass(URLClassLoader.java:442)
        at java.net.URLClassLoader.access$100(URLClassLoader.java:64)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:354)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:348)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:347)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
        at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:491)
----- URLTest.java -----
package java.net;
import java.net.*;
import java.io.*;
public class URLTest
{
  public static void main (String[]args)
  {
    URL myURL=null;
      try
    {
      myURL = new URL ("http", "www.zavgren.com", 80, "/index.html");
    }
    catch (MalformedURLException mfue)
    {
      System.err.println (mfue);
    }
    System.out.printf ("The URL is: %s\n", myURL.toString ());
    System.out.printf ("The protocol is: %s\n", myURL.getProtocol());
    System.out.printf ("The external form is: %s\n", myURL.toExternalForm());
    System.out.printf ("Opening connection... \n");
    try
    {
    URLConnection uc = myURL.openConnection();
    }
    catch (IOException io)
    {
    }
    myURL.set("ftp", "vpn.zavgren.com", 77, "lugnut.txt", "","","","");
  }
};

------------------------
Thanks!
John



More information about the net-dev mailing list