From 689c8aa27516c1cccffda8530147dafbeda61b1d Mon Sep 17 00:00:00 2001 From: Uwe Schindler Date: Sun, 29 Mar 2015 19:20:38 +0200 Subject: [PATCH] Issue #39: Add initial support for Java 9 modules --- .../de/thetaphi/forbiddenapis/Checker.java | 54 ++++++++++--------- 1 file changed, 30 insertions(+), 24 deletions(-) diff --git a/src/main/java/de/thetaphi/forbiddenapis/Checker.java b/src/main/java/de/thetaphi/forbiddenapis/Checker.java index 671e8f8e..91d1049f 100644 --- a/src/main/java/de/thetaphi/forbiddenapis/Checker.java +++ b/src/main/java/de/thetaphi/forbiddenapis/Checker.java @@ -99,25 +99,30 @@ public Checker(ClassLoader loader, boolean internalRuntimeForbidden, boolean fai final Set bootClassPathJars = new LinkedHashSet(); final Set bootClassPathDirs = new LinkedHashSet(); try { - final RuntimeMXBean rb = ManagementFactory.getRuntimeMXBean(); - if (rb.isBootClassPathSupported()) { - final String cp = rb.getBootClassPath(); - final StringTokenizer st = new StringTokenizer(cp, File.pathSeparator); - while (st.hasMoreTokens()) { - final File f = new File(st.nextToken()); - if (f.isFile()) { - bootClassPathJars.add(f.getCanonicalFile()); - } else if (f.isDirectory()) { - String fp = f.getCanonicalPath(); - if (!fp.endsWith(File.separator)) { - fp += File.separator; + final boolean isJava9Modules = "jrt".equalsIgnoreCase(loader.getResource(Object.class.getName().replace('.','/') + ".class").getProtocol()); + if (isJava9Modules) { + isSupportedJDK = true; + } else { + final RuntimeMXBean rb = ManagementFactory.getRuntimeMXBean(); + if (rb.isBootClassPathSupported()) { + final String cp = rb.getBootClassPath(); + final StringTokenizer st = new StringTokenizer(cp, File.pathSeparator); + while (st.hasMoreTokens()) { + final File f = new File(st.nextToken()); + if (f.isFile()) { + bootClassPathJars.add(f.getCanonicalFile()); + } else if (f.isDirectory()) { + String fp = f.getCanonicalPath(); + if (!fp.endsWith(File.separator)) { + fp += File.separator; + } + bootClassPathDirs.add(fp); } - bootClassPathDirs.add(fp); } } + isSupportedJDK = !(bootClassPathJars.isEmpty() && bootClassPathDirs.isEmpty()); + // logInfo("JARs in boot-classpath: " + bootClassPathJars + "; dirs in boot-classpath: " + bootClassPathDirs); } - isSupportedJDK = !(bootClassPathJars.isEmpty() && bootClassPathDirs.isEmpty()); - // logInfo("JARs in boot-classpath: " + bootClassPathJars + "; dirs in boot-classpath: " + bootClassPathDirs); } catch (IOException ioe) { isSupportedJDK = false; bootClassPathJars.clear(); @@ -179,16 +184,17 @@ private ClassSignature getClassFromClassLoader(final String clazz) throws ClassN } catch (URISyntaxException use) { // ignore (should not happen, but if it's happening, it's definitely not a runtime class) } - } else { - if (conn instanceof JarURLConnection) { - final URL jarUrl = ((JarURLConnection) conn).getJarFileURL(); - if ("file".equalsIgnoreCase(jarUrl.getProtocol())) try { - final File jarFile = new File(jarUrl.toURI()).getCanonicalFile(); - isRuntimeClass = bootClassPathJars.contains(jarFile); - } catch (URISyntaxException use) { - // ignore (should not happen, but if it's happening, it's definitely not a runtime class) - } + } else if ("jar".equalsIgnoreCase(url.getProtocol()) && conn instanceof JarURLConnection) { + final URL jarUrl = ((JarURLConnection) conn).getJarFileURL(); + if ("file".equalsIgnoreCase(jarUrl.getProtocol())) try { + final File jarFile = new File(jarUrl.toURI()).getCanonicalFile(); + isRuntimeClass = bootClassPathJars.contains(jarFile); + } catch (URISyntaxException use) { + // ignore (should not happen, but if it's happening, it's definitely not a runtime class) } + } else if ("jrt".equalsIgnoreCase(url.getProtocol())) { + // all 'jrt:' URLs refer to a module in the Java 9+ runtime (see http://openjdk.java.net/jeps/220): + isRuntimeClass = true; } final InputStream in = conn.getInputStream(); try {