< prev index next >

langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/util/JDK9Wrappers.java

Print this page

        

*** 29,283 **** 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; } } } --- 29,256 ---- 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, ClassLoader.getSystemClassLoader()); ! 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, ClassLoader.getSystemClassLoader()); ! 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, ClassLoader.getSystemClassLoader()); ! 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); } } } } }
< prev index next >