Last active
April 9, 2016 13:00
-
-
Save nipafx/730e89860c45fad077c53ec54b3cea52 to your computer and use it in GitHub Desktop.
Accessing Package Scoped Methods In Java 9
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.codefx.lab; | |
import java.lang.reflect.Method; | |
import java.net.URL; | |
import java.net.URLStreamHandler; | |
public class GetURLStreamHandler { | |
public static void main(String[] args) throws Exception { | |
printDefaultPortForProtocol("http"); | |
printDefaultPortForProtocol("https"); | |
} | |
private static void printDefaultPortForProtocol(String protocol) throws Exception { | |
URLStreamHandler handler = getURLStreamHandler(protocol); | |
int port = callGetDefaultPortOn(handler); | |
System.out.printf( | |
"default %s port: %d (delivered by %s; %s)%n", | |
protocol, | |
port, | |
handler.getClass().getName(), | |
determineJigsawPresenceByInstantiating(handler)); | |
} | |
private static URLStreamHandler getURLStreamHandler(String protocol) throws Exception { | |
Method method = URL.class.getDeclaredMethod("getURLStreamHandler", String.class); | |
method.setAccessible(true); | |
return (URLStreamHandler) method.invoke(null, protocol); | |
} | |
private static int callGetDefaultPortOn(URLStreamHandler handler) throws Exception { | |
Method method = URLStreamHandler.class.getDeclaredMethod("getDefaultPort"); | |
method.setAccessible(true); | |
return (int) method.invoke(handler); | |
} | |
private static String determineJigsawPresenceByInstantiating(URLStreamHandler handler) throws Exception { | |
try { | |
handler.getClass().newInstance(); | |
return "pre Jigsaw"; | |
} catch (IllegalAccessException ex) { | |
return "Jigsaw"; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.codefx.lab; | |
import java.lang.reflect.Method; | |
import java.net.URL; | |
import java.net.URLStreamHandler; | |
public class GetURLStreamHandler { | |
public static void main(String[] args) throws Exception { | |
printDefaultPortForProtocol("http"); | |
printDefaultPortForProtocol("https"); | |
} | |
private static void printDefaultPortForProtocol(String protocol) throws Exception { | |
URLStreamHandler handler = getURLStreamHandler(protocol); | |
int port = callGetDefaultPortOn(handler); | |
System.out.printf("default %s port: %d (delivered by %s)%n", protocol, port, handler.getClass().getName()); | |
} | |
private static URLStreamHandler getURLStreamHandler(String protocol) throws Exception { | |
Method method = URL.class.getDeclaredMethod("getURLStreamHandler", String.class); | |
method.setAccessible(true); | |
return (URLStreamHandler) method.invoke(null, protocol); | |
} | |
private static int callGetDefaultPortOn(URLStreamHandler handler) throws Exception { | |
Method method = URLStreamHandler.class.getDeclaredMethod("getDefaultPort"); | |
method.setAccessible(true); | |
return (int) method.invoke(handler); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
My experimentation for answering this question on Stack Overflow:
Execution
To execute it, a JDK 9 early access build (I tried it with build 113) should be installed and its
bin
folder be available under$JIGSAW_BIN
. The classGetURLStreamHandler
must be insrc/org/codefx/lab
, relative tocompile.sh
.Result
Java 8
Compiling and running with Java 8 (e.g. with IDE) yields ...
... because the internal classes can be instantiated.
Java 9
Compiling and running with Java 9 (with the script) yields ...
... because the internal classes can not be instantiated, due to the module system.