108 } 109 useLegacy = (c == null); 110 } 111 112 /** 113 * Get an instance of a system tool using the service loader. 114 * @implNote By default, this returns the implementation in the specified module. 115 * For limited backward compatibility, if this code is run on an older version 116 * of the Java platform that does not support modules, this method will 117 * try and create an instance of the named class. Note that implies the 118 * class must be available on the system class path. 119 * @param <T> the interface of the tool 120 * @param clazz the interface of the tool 121 * @param moduleName the name of the module containing the desired implementation 122 * @param className the class name of the desired implementation 123 * @return the specified implementation of the tool 124 */ 125 private static <T> T getSystemTool(Class<T> clazz, String moduleName, String className) { 126 if (useLegacy) { 127 try { 128 @SuppressWarnings("deprecation") 129 T result = Class.forName(className, true, ClassLoader.getSystemClassLoader()).asSubclass(clazz).newInstance(); 130 return result; 131 } catch (ReflectiveOperationException e) { 132 throw new Error(e); 133 } 134 } 135 136 try { 137 ServiceLoader<T> sl = ServiceLoader.load(clazz, ClassLoader.getSystemClassLoader()); 138 for (Iterator<T> iter = sl.iterator(); iter.hasNext(); ) { 139 T tool = iter.next(); 140 if (matches(tool, moduleName)) 141 return tool; 142 } 143 } catch (ServiceConfigurationError e) { 144 throw new Error(e); 145 } 146 return null; 147 } 148 149 /** 150 * Determine if this is the desired tool instance. | 108 } 109 useLegacy = (c == null); 110 } 111 112 /** 113 * Get an instance of a system tool using the service loader. 114 * @implNote By default, this returns the implementation in the specified module. 115 * For limited backward compatibility, if this code is run on an older version 116 * of the Java platform that does not support modules, this method will 117 * try and create an instance of the named class. Note that implies the 118 * class must be available on the system class path. 119 * @param <T> the interface of the tool 120 * @param clazz the interface of the tool 121 * @param moduleName the name of the module containing the desired implementation 122 * @param className the class name of the desired implementation 123 * @return the specified implementation of the tool 124 */ 125 private static <T> T getSystemTool(Class<T> clazz, String moduleName, String className) { 126 if (useLegacy) { 127 try { 128 return Class.forName(className, 129 true, 130 ClassLoader.getSystemClassLoader()).asSubclass(clazz). 131 getConstructor(new Class<?>[0]).newInstance((Object[])null); 132 } catch (ReflectiveOperationException e) { 133 throw new Error(e); 134 } 135 } 136 137 try { 138 ServiceLoader<T> sl = ServiceLoader.load(clazz, ClassLoader.getSystemClassLoader()); 139 for (Iterator<T> iter = sl.iterator(); iter.hasNext(); ) { 140 T tool = iter.next(); 141 if (matches(tool, moduleName)) 142 return tool; 143 } 144 } catch (ServiceConfigurationError e) { 145 throw new Error(e); 146 } 147 return null; 148 } 149 150 /** 151 * Determine if this is the desired tool instance. |