25 |
25 |
26 package javax.tools; |
26 package javax.tools; |
27 |
27 |
28 import java.lang.reflect.InvocationTargetException; |
28 import java.lang.reflect.InvocationTargetException; |
29 import java.lang.reflect.Method; |
29 import java.lang.reflect.Method; |
|
30 import java.security.AccessController; |
|
31 import java.security.PrivilegedAction; |
30 import java.util.Iterator; |
32 import java.util.Iterator; |
31 import java.util.ServiceConfigurationError; |
33 import java.util.ServiceConfigurationError; |
32 import java.util.ServiceLoader; |
34 import java.util.ServiceLoader; |
33 |
35 |
34 /** |
36 /** |
143 } |
145 } |
144 return null; |
146 return null; |
145 } |
147 } |
146 |
148 |
147 /** |
149 /** |
148 * Determine if this is tho desired tool instance. |
150 * Determine if this is the desired tool instance. |
149 * @param <T> the interface of the tool |
151 * @param <T> the interface of the tool |
150 * @param tool the instance of the tool |
152 * @param tool the instance of the tool |
151 * @param moduleName the name of the module containing the desired implementation |
153 * @param moduleName the name of the module containing the desired implementation |
152 * @return true if and only if the tool matches the specified criteria |
154 * @return true if and only if the tool matches the specified criteria |
153 */ |
155 */ |
154 private static <T> boolean matches(T tool, String moduleName) { |
156 private static <T> boolean matches(T tool, String moduleName) { |
155 // for now, use reflection to implement |
157 PrivilegedAction<Boolean> pa = () -> { |
156 // return moduleName.equals(tool.getClass().getModule().getName()); |
158 // for now, use reflection to implement |
157 try { |
159 // return moduleName.equals(tool.getClass().getModule().getName()); |
158 Method getModuleMethod = Class.class.getDeclaredMethod("getModule"); |
160 try { |
159 Object toolModule = getModuleMethod.invoke(tool.getClass()); |
161 Method getModuleMethod = Class.class.getDeclaredMethod("getModule"); |
160 Method getNameMethod = toolModule.getClass().getDeclaredMethod("getName"); |
162 Object toolModule = getModuleMethod.invoke(tool.getClass()); |
161 String toolModuleName = (String) getNameMethod.invoke(toolModule); |
163 Method getNameMethod = toolModule.getClass().getDeclaredMethod("getName"); |
162 return moduleName.equals(toolModuleName); |
164 String toolModuleName = (String) getNameMethod.invoke(toolModule); |
163 } catch (InvocationTargetException | NoSuchMethodException | IllegalAccessException e) { |
165 return moduleName.equals(toolModuleName); |
164 return false; |
166 } catch (InvocationTargetException | NoSuchMethodException | IllegalAccessException e) { |
165 } |
167 return false; |
|
168 } |
|
169 }; |
|
170 return AccessController.doPrivileged(pa); |
166 } |
171 } |
167 } |
172 } |