8157474: clean up/simplify/rename ModuleWrappers class

Reviewed-by: mchung
This commit is contained in:
Jonathan Gibbons 2016-05-20 16:44:35 -07:00
parent 0bafc010b2
commit e568099980
5 changed files with 272 additions and 298 deletions

View File

@ -99,12 +99,14 @@ public class MutableFieldsAnalyzer extends AbstractCodingRulesAnalyzer {
ignoreFields("com.sun.tools.javac.code.Type", "moreInfo");
ignoreFields("com.sun.tools.javac.util.SharedNameTable", "freelist");
ignoreFields("com.sun.tools.javac.util.Log", "useRawMessages");
ignoreFields("com.sun.tools.javac.util.ModuleWrappers$ModuleFinderHelper",
"moduleFinderInterface", "ofMethod", "emptyMethod");
ignoreFields("com.sun.tools.javac.util.ModuleWrappers$ConfigurationHelper",
ignoreFields("com.sun.tools.javac.util.JDK9Wrappers$ModuleFinder",
"moduleFinderClass", "ofMethod");
ignoreFields("com.sun.tools.javac.util.JDK9Wrappers$Configuration",
"configurationClass", "resolveRequiresAndUsesMethod");
ignoreFields("com.sun.tools.javac.util.ModuleWrappers$LayerHelper",
ignoreFields("com.sun.tools.javac.util.JDK9Wrappers$Layer",
"layerClass", "bootMethod", "defineModulesWithOneLoaderMethod", "configurationMethod");
ignoreFields("com.sun.tools.javac.util.JDK9Wrappers$ServiceLoaderHelper",
"loadMethod");
ignoreFields("com.sun.tools.javac.util.ModuleHelper",
"addExportsMethod", "getUnnamedModuleMethod", "getModuleMethod");
}

View File

@ -74,10 +74,10 @@ import com.sun.tools.javac.util.DefinedBy;
import com.sun.tools.javac.util.DefinedBy.Api;
import com.sun.tools.javac.util.List;
import com.sun.tools.javac.util.ListBuffer;
import com.sun.tools.javac.util.ModuleWrappers.Configuration;
import com.sun.tools.javac.util.ModuleWrappers.Layer;
import com.sun.tools.javac.util.ModuleWrappers.ModuleFinder;
import com.sun.tools.javac.util.ModuleWrappers.ServiceLoaderHelper;
import com.sun.tools.javac.util.JDK9Wrappers.Configuration;
import com.sun.tools.javac.util.JDK9Wrappers.Layer;
import com.sun.tools.javac.util.JDK9Wrappers.ModuleFinder;
import com.sun.tools.javac.util.JDK9Wrappers.ServiceLoaderHelper;
import static java.nio.file.FileVisitOption.FOLLOW_LINKS;
@ -972,7 +972,7 @@ public class JavacFileManager extends BaseFileManager implements StandardJavaFil
Collection<Path> paths = locations.getLocation(location);
ModuleFinder finder = ModuleFinder.of(paths.toArray(new Path[paths.size()]));
Layer bootLayer = Layer.boot();
Configuration cf = bootLayer.configuration().resolveRequiresAndUses(ModuleFinder.empty(), finder, Collections.emptySet());
Configuration cf = bootLayer.configuration().resolveRequiresAndUses(ModuleFinder.of(), finder, Collections.emptySet());
Layer layer = bootLayer.defineModulesWithOneLoader(cf, ClassLoader.getSystemClassLoader());
return ServiceLoaderHelper.load(layer, service);
} else {

View File

@ -0,0 +1,255 @@
/*
* Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.sun.tools.javac.util;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.nio.file.Path;
import java.util.Collection;
import java.util.ServiceLoader;
/**
* This class provides wrappers for classes and methods that are new in JDK 9, and which are not
* available on older versions of the platform on which javac may be compiled and run.
* In future releases, when javac is always compiled on JDK 9 or later, the use of these wrappers
* can be replaced by use of the real underlying classes.
*
* <p>Wrapper classes provide a subset of the API of the wrapped classes, as needed for use
* in javac. Wrapper objects contain an {@code Object} reference to the underlying runtime object,
* and {@code Class} and {@code Method} objects for obtaining or using such instances via
* runtime reflection. The {@code Class} and {@code Method} objects are set up on a per-class
* basis, by an {@code init} method, which is called from static methods on the wrapper class,
* or in the constructor, when instances are created.
* <p>
*
* <p><b>This is NOT part of any supported API.
* If you write code that depends on this, you do so at your own risk.
* This code and its internal interfaces are subject to change or
* deletion without notice.</b>
*/
public class JDK9Wrappers {
/**
* Helper class for new method in java.util.ServiceLoader.
*/
public static final class ServiceLoaderHelper {
@SuppressWarnings("unchecked")
public static <S> ServiceLoader<S> load(Layer layer, Class<S> service) {
try {
init();
Object result = loadMethod.invoke(null, layer.theRealLayer, service);
return (ServiceLoader<S>)result;
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException
| SecurityException ex) {
throw new Abort(ex);
}
}
// -----------------------------------------------------------------------------------------
private static Method loadMethod = null;
private static void init() {
if (loadMethod == null) {
try {
Class<?> layerClass = Layer.layerClass;
loadMethod = ServiceLoader.class.getDeclaredMethod("load", layerClass, Class.class);
} catch (NoSuchMethodException | SecurityException ex) {
throw new Abort(ex);
}
}
}
}
/**
* Wrapper class for java.lang.module.ModuleFinder.
*/
public static class ModuleFinder {
private final Object theRealModuleFinder;
private ModuleFinder(Object moduleFinder) {
this.theRealModuleFinder = moduleFinder;
init();
}
public static ModuleFinder of(Path... dirs) {
try {
init();
Object result = ofMethod.invoke(null, (Object)dirs);
ModuleFinder mFinder = new ModuleFinder(result);
return mFinder;
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException
| SecurityException ex) {
throw new Abort(ex);
}
}
// -----------------------------------------------------------------------------------------
private static Class<?> moduleFinderClass = null;
private static Method ofMethod;
static final Class<?> getModuleFinderClass() {
init();
return moduleFinderClass;
}
private static void init() {
if (moduleFinderClass == null) {
try {
moduleFinderClass = Class.forName("java.lang.module.ModuleFinder", false, null);
ofMethod = moduleFinderClass.getDeclaredMethod("of", Path[].class);
} catch (ClassNotFoundException | NoSuchMethodException | SecurityException ex) {
throw new Abort(ex);
}
}
}
}
/**
* Wrapper class for java.lang.module.Configuration.
*/
public static final class Configuration {
private final Object theRealConfiguration;
private Configuration(Object configuration) {
this.theRealConfiguration = configuration;
init();
}
public Configuration resolveRequiresAndUses(
ModuleFinder beforeFinder,
ModuleFinder afterFinder,
Collection<String> roots) {
try {
Object result = resolveRequiresAndUsesMethod.invoke(theRealConfiguration,
beforeFinder.theRealModuleFinder,
afterFinder.theRealModuleFinder,
roots
);
Configuration configuration = new Configuration(result);
return configuration;
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException
| SecurityException ex) {
throw new Abort(ex);
}
}
// -----------------------------------------------------------------------------------------
private static Class<?> configurationClass = null;
private static Method resolveRequiresAndUsesMethod;
static final Class<?> getConfigurationClass() {
init();
return configurationClass;
}
private static void init() {
if (configurationClass == null) {
try {
configurationClass = Class.forName("java.lang.module.Configuration", false, null);
Class<?> moduleFinderInterface = ModuleFinder.getModuleFinderClass();
resolveRequiresAndUsesMethod = configurationClass.getDeclaredMethod("resolveRequiresAndUses",
moduleFinderInterface,
moduleFinderInterface,
Collection.class
);
} catch (ClassNotFoundException | NoSuchMethodException | SecurityException ex) {
throw new Abort(ex);
}
}
}
}
/**
* Wrapper class for java.lang.module.Layer.
*/
public static final class Layer {
private final Object theRealLayer;
private Layer(Object layer) {
this.theRealLayer = layer;
}
public static Layer boot() {
try {
init();
Object result = bootMethod.invoke(null);
Layer layer = new Layer(result);
return layer;
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException
| SecurityException ex) {
throw new Abort(ex);
}
}
public Configuration configuration() {
try {
Object result = configurationMethod.invoke(theRealLayer);
Configuration configuration = new Configuration(result);
return configuration;
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException
| SecurityException ex) {
throw new Abort(ex);
}
}
public Layer defineModulesWithOneLoader(Configuration configuration, ClassLoader parentClassLoader) {
try {
Object result = defineModulesWithOneLoaderMethod.invoke(
theRealLayer, configuration.theRealConfiguration, parentClassLoader);
Layer layer = new Layer(result);
return layer;
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException
| SecurityException ex) {
throw new Abort(ex);
}
}
// -----------------------------------------------------------------------------------------
private static Class<?> layerClass = null;
private static Method bootMethod;
private static Method defineModulesWithOneLoaderMethod;
private static Method configurationMethod;
private static void init() {
if (layerClass == null) {
try {
layerClass = Class.forName("java.lang.reflect.Layer", false, null);
bootMethod = layerClass.getDeclaredMethod("boot");
defineModulesWithOneLoaderMethod = layerClass.getDeclaredMethod("defineModulesWithOneLoader",
Configuration.getConfigurationClass(),
ClassLoader.class);
configurationMethod = layerClass.getDeclaredMethod("configuration");
} catch (ClassNotFoundException | NoSuchMethodException | SecurityException ex) {
throw new Abort(ex);
}
}
}
}
}

View File

@ -1,283 +0,0 @@
/*
* Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.sun.tools.javac.util;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.nio.file.Path;
import java.util.Collection;
import java.util.ServiceLoader;
/** This class provides wrappers for classes and methods that are new in JDK 9, and which are not
* available on older versions of the platform on which javac may be compiled and run.
* In future releases, when javac is always compiled on JDK 9 or later, the use of these wrappers
* can be replaced by use of the real underlying classes.
*/
public class ModuleWrappers {
public static final class ServiceLoaderHelper {
@SuppressWarnings("unchecked")
public static <S> ServiceLoader<S> load(Layer layer, Class<S> service) {
try {
Class<?> layerClass = LayerHelper.getLayerClass();
Method loadMethod = ServiceLoader.class
.getDeclaredMethod("load", layerClass, Class.class);
Object result = loadMethod.invoke(ServiceLoader.class, layer.theRealLayer, service);
return (ServiceLoader<S>)result;
} catch (NoSuchMethodException |
SecurityException |
IllegalArgumentException |
IllegalAccessException |
InvocationTargetException ex) {
throw new Abort(ex);
}
}
}
public static class ModuleFinder {
Object theRealModuleFinder;
private ModuleFinder(Object moduleFinder) {
this.theRealModuleFinder = moduleFinder;
}
public static ModuleFinder of(Path... dirs) {
try {
Object result = ModuleFinderHelper.getOfMethod()
.invoke(ModuleFinderHelper.moduleFinderInterface, (Object)dirs);
ModuleFinder mFinder = new ModuleFinder(result);
return mFinder;
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
throw new Abort(ex);
}
}
public static ModuleFinder empty() {
try {
Object result = ModuleFinderHelper.getEmptyMethod()
.invoke(ModuleFinderHelper.moduleFinderInterface);
ModuleFinder mFinder = new ModuleFinder(result);
return mFinder;
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
throw new Abort(ex);
}
}
}
private static class ModuleFinderHelper {
static Method ofMethod = null;
static Method emptyMethod = null;
static Class<?> moduleFinderInterface;
static Method getOfMethod() {
if (ModuleFinderHelper.ofMethod == null) {
try {
getModuleFinderInterface();
ofMethod = moduleFinderInterface.getDeclaredMethod("of", Path[].class);
} catch (NoSuchMethodException | SecurityException ex) {
throw new Abort(ex);
}
}
return ofMethod;
}
static Method getEmptyMethod() {
if (emptyMethod == null) {
try {
getModuleFinderInterface();
emptyMethod = moduleFinderInterface.getDeclaredMethod("empty");
} catch (NoSuchMethodException | SecurityException ex) {
throw new Abort(ex);
}
}
return emptyMethod;
}
static Class<?> getModuleFinderInterface() {
if (moduleFinderInterface == null) {
try {
moduleFinderInterface = Class.forName("java.lang.module.ModuleFinder", false, ClassLoader.getSystemClassLoader());
} catch (ClassNotFoundException ex) {
throw new Abort(ex);
}
}
return moduleFinderInterface;
}
}
public static final class Configuration {
Object theRealConfiguration;
private Configuration(Object configuration) {
this.theRealConfiguration = configuration;
}
public Configuration resolveRequiresAndUses(
ModuleFinder beforeFinder,
ModuleFinder afterFinder,
Collection<String> roots) {
try {
Object result = ConfigurationHelper.getResolveRequiresAndUses()
.invoke(theRealConfiguration,
beforeFinder.theRealModuleFinder,
afterFinder.theRealModuleFinder,
roots
);
Configuration configuration = new Configuration(result);
return configuration;
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
throw new Abort(ex);
}
}
}
private static class ConfigurationHelper {
static Method resolveRequiresAndUsesMethod;
static Class<?> configurationClass;
static Method getResolveRequiresAndUses() {
if (resolveRequiresAndUsesMethod == null) {
try {
getConfigurationClass();
Class<?> moduleFinderInterface = ModuleFinderHelper.getModuleFinderInterface();
Class<?> configurationClass = ConfigurationHelper.getConfigurationClass();
resolveRequiresAndUsesMethod = configurationClass.getDeclaredMethod("resolveRequiresAndUses",
moduleFinderInterface,
moduleFinderInterface,
Collection.class
);
} catch (NoSuchMethodException | SecurityException ex) {
throw new Abort(ex);
}
}
return resolveRequiresAndUsesMethod;
}
static Class<?> getConfigurationClass() {
if (configurationClass == null) {
try {
configurationClass = Class.forName("java.lang.module.Configuration", false, ClassLoader.getSystemClassLoader());
} catch (ClassNotFoundException ex) {
throw new Abort(ex);
}
}
return configurationClass;
}
}
public static final class Layer {
Object theRealLayer;
private Layer(Object layer) {
this.theRealLayer = layer;
}
public static Layer boot() {
try {
Object result = LayerHelper.getBootMethod().invoke(LayerHelper.getLayerClass());
Layer layer = new Layer(result);
return layer;
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
throw new Abort(ex);
}
}
public Configuration configuration() {
try {
Object result = LayerHelper.getConfigurationMethod().invoke(theRealLayer);
Layer layer = new Layer(result);
return new Configuration(result);
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
throw new Abort(ex);
}
}
public Layer defineModulesWithOneLoader(Configuration configuration, ClassLoader parentClassLoader) {
try {
Object result = LayerHelper.getDefineModulesWithOneLoaderMethod()
.invoke(theRealLayer, configuration.theRealConfiguration, parentClassLoader);
Layer layer = new Layer(result);
return layer;
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
throw new Abort(ex);
}
}
}
private static class LayerHelper {
static Class<?> layerClass;
static Method bootMethod;
static Method defineModulesWithOneLoaderMethod = null;
static Method configurationMethod;
static Class<?> getLayerClass() {
if (layerClass == null) {
try {
layerClass = Class.forName("java.lang.reflect.Layer", false, ClassLoader.getSystemClassLoader());
} catch (ClassNotFoundException ex) {
throw new Abort(ex);
}
}
return layerClass;
}
static Method getBootMethod() {
if (bootMethod == null) {
try {
bootMethod = getLayerClass().getDeclaredMethod("boot");
} catch (NoSuchMethodException | SecurityException ex) {
throw new Abort(ex);
}
}
return bootMethod;
}
static Method getDefineModulesWithOneLoaderMethod() {
if (defineModulesWithOneLoaderMethod == null) {
try {
defineModulesWithOneLoaderMethod = getLayerClass().getDeclaredMethod("defineModulesWithOneLoader",
ConfigurationHelper.getConfigurationClass(),
ClassLoader.class
);
} catch (NoSuchMethodException | SecurityException ex) {
throw new Abort(ex);
}
}
return defineModulesWithOneLoaderMethod;
}
static Method getConfigurationMethod() {
if (configurationMethod == null) {
try {
configurationMethod = getLayerClass().getDeclaredMethod("configuration");
} catch (NoSuchMethodException | SecurityException ex) {
throw new Abort(ex);
}
}
return configurationMethod;
}
}
}

View File

@ -108,14 +108,14 @@ public class DetectMutableStaticFields {
// when running javac on JDK 8.
ignore("com/sun/tools/javac/util/ModuleHelper",
"addExportsMethod", "getModuleMethod", "getUnnamedModuleMethod");
ignore("com/sun/tools/javac/util/ModuleWrappers$ConfigurationHelper",
ignore("com/sun/tools/javac/util/JDK9Wrappers$Configuration",
"resolveRequiresAndUsesMethod", "configurationClass");
ignore("com/sun/tools/javac/util/ModuleWrappers$LayerHelper",
ignore("com/sun/tools/javac/util/JDK9Wrappers$Layer",
"bootMethod", "defineModulesWithOneLoaderMethod", "configurationMethod", "layerClass");
ignore("com/sun/tools/javac/util/ModuleWrappers$ModuleFinderHelper",
"emptyMethod", "moduleFinderInterface", "ofMethod");
ignore("com/sun/tools/javac/util/JDK9Wrappers$ModuleFinder",
"moduleFinderClass", "ofMethod");
ignore("com/sun/tools/javac/util/JDK9Wrappers$ServiceLoaderHelper",
"loadMethod");
}
private final List<String> errors = new ArrayList<>();