Skip to content

Instantly share code, notes, and snippets.

@nipafx
Last active April 9, 2016 13:00
Show Gist options
  • Save nipafx/730e89860c45fad077c53ec54b3cea52 to your computer and use it in GitHub Desktop.
Save nipafx/730e89860c45fad077c53ec54b3cea52 to your computer and use it in GitHub Desktop.
Accessing Package Scoped Methods In Java 9
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";
}
}
}
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);
}
}
@nipafx
Copy link
Author

nipafx commented Apr 9, 2016

My experimentation for answering this question on Stack Overflow:

Is it possible to access package scoped methods by reflection in Java 9 with Jigsaw?

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 class GetURLStreamHandler must be in src/org/codefx/lab, relative to compile.sh.

Result

Java 8

Compiling and running with Java 8 (e.g. with IDE) yields ...

default http port: 80 (delivered by sun.net.www.protocol.http.Handler; pre Jigsaw)
default https port: 443 (delivered by sun.net.www.protocol.https.Handler; pre Jigsaw)

... because the internal classes can be instantiated.

Java 9

Compiling and running with Java 9 (with the script) yields ...

default http port: 80 (delivered by sun.net.www.protocol.http.Handler; Jigsaw)
default https port: 443 (delivered by sun.net.www.protocol.https.Handler; Jigsaw)

... because the internal classes can not be instantiated, due to the module system.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment