8155765: javax.tools.ToolProvider::getSystemToolClassLoader returns app class loader even if no tool is available

Reviewed-by: mchung
This commit is contained in:
Jonathan Gibbons 2016-11-22 16:29:24 -08:00
parent fe1b93aeab
commit 2d7092c7ac
4 changed files with 21 additions and 15 deletions

View file

@ -57,7 +57,7 @@ public class ToolProvider {
* {@code null} if no compiler is provided * {@code null} if no compiler is provided
* @implNote This implementation returns the compiler provided * @implNote This implementation returns the compiler provided
* by the {@code jdk.compiler} module if that module is available, * by the {@code jdk.compiler} module if that module is available,
* and null otherwise. * and {@code null} otherwise.
*/ */
public static JavaCompiler getSystemJavaCompiler() { public static JavaCompiler getSystemJavaCompiler() {
return getSystemTool(JavaCompiler.class, return getSystemTool(JavaCompiler.class,
@ -78,7 +78,7 @@ public class ToolProvider {
* {@code null} if no documentation tool is provided * {@code null} if no documentation tool is provided
* @implNote This implementation returns the tool provided * @implNote This implementation returns the tool provided
* by the {@code jdk.javadoc} module if that module is available, * by the {@code jdk.javadoc} module if that module is available,
* and null otherwise. * and {@code null} otherwise.
*/ */
public static DocumentationTool getSystemDocumentationTool() { public static DocumentationTool getSystemDocumentationTool() {
return getSystemTool(DocumentationTool.class, return getSystemTool(DocumentationTool.class,
@ -86,16 +86,19 @@ public class ToolProvider {
} }
/** /**
* Returns the class loader for tools provided with this platform. * Returns a class loader that may be used to load system tools,
* This does not include user-installed tools. Use the * or {@code null} if no such special loader is provided.
* {@linkplain java.util.ServiceLoader service provider mechanism} * @implSpec This implementation always returns {@code null}.
* for locating user installed tools. * @deprecated This method is subject to removal in a future version of
* * Java SE.
* @return the class loader for tools provided with this platform * Use the {@link java.util.spi.ToolProvider system tool provider} or
* or {@code null} if no tools are provided * {@link java.util.ServiceLoader service loader} mechanisms to
* locate system tools as well as user-installed tools.
* @return a class loader, or {@code null}
*/ */
@Deprecated
public static ClassLoader getSystemToolClassLoader() { public static ClassLoader getSystemToolClassLoader() {
return ClassLoader.getSystemClassLoader(); return null;
} }
private static final boolean useLegacy; private static final boolean useLegacy;

View file

@ -31,6 +31,7 @@
*/ */
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.Module;
import java.io.File; import java.io.File;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import javax.tools.*; import javax.tools.*;
@ -39,9 +40,9 @@ public class T6410653 {
public static void main(String... args) throws Exception { public static void main(String... args) throws Exception {
File testSrc = new File(System.getProperty("test.src")); File testSrc = new File(System.getProperty("test.src"));
String source = new File(testSrc, "T6410653.java").getPath(); String source = new File(testSrc, "T6410653.java").getPath();
ClassLoader cl = ToolProvider.getSystemToolClassLoader();
Tool compiler = ToolProvider.getSystemJavaCompiler(); Tool compiler = ToolProvider.getSystemJavaCompiler();
Class<?> log = Class.forName("com.sun.tools.javac.util.Log", true, cl); Module compilerModule = compiler.getClass().getModule();
Class<?> log = Class.forName(compilerModule, "com.sun.tools.javac.util.Log");
Field useRawMessages = log.getDeclaredField("useRawMessages"); Field useRawMessages = log.getDeclaredField("useRawMessages");
useRawMessages.setAccessible(true); useRawMessages.setAccessible(true);
useRawMessages.setBoolean(null, true); useRawMessages.setBoolean(null, true);

View file

@ -55,6 +55,8 @@ public class ToolProviderTest {
Objects.requireNonNull(ToolProvider.getSystemDocumentationTool()); Objects.requireNonNull(ToolProvider.getSystemDocumentationTool());
Objects.requireNonNull(ToolProvider.getSystemJavaCompiler()); Objects.requireNonNull(ToolProvider.getSystemJavaCompiler());
Objects.requireNonNull(ToolProvider.getSystemToolClassLoader()); if (ToolProvider.getSystemToolClassLoader() != null) {
throw new AssertionError("unexpected value for getSystemToolClassLoader");
}
} }
} }

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -82,6 +82,6 @@ public class ReleaseOptionClashes {
compiler.run(null, null, System.out, options.toArray(new String[0])); compiler.run(null, null, System.out, options.toArray(new String[0]));
} }
ClassLoader cl = ToolProvider.getSystemToolClassLoader();
Tool compiler = ToolProvider.getSystemJavaCompiler(); Tool compiler = ToolProvider.getSystemJavaCompiler();
ClassLoader cl = compiler.getClass().getClassLoader();
} }