langtools/src/java.compiler/share/classes/javax/tools/ToolProvider.java
changeset 37853 b4ea8806ad1a
parent 37759 f0b5daef41b6
child 38918 bf1ed1a40f5b
equal deleted inserted replaced
37852:4dd95b3cb2e8 37853:b4ea8806ad1a
    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 }