jdk/src/share/classes/java/awt/AWTKeyStroke.java
changeset 20107 18e644411f0b
parent 11502 9d4e3b71c122
child 20172 f48935a247ec
equal deleted inserted replaced
20106:5d5f2c72f262 20107:18e644411f0b
    65  * @since 1.4
    65  * @since 1.4
    66  */
    66  */
    67 public class AWTKeyStroke implements Serializable {
    67 public class AWTKeyStroke implements Serializable {
    68     static final long serialVersionUID = -6430539691155161871L;
    68     static final long serialVersionUID = -6430539691155161871L;
    69 
    69 
    70     private static Map modifierKeywords;
    70     private static Map<String, Integer> modifierKeywords;
    71     /**
    71     /**
    72      * Associates VK_XXX (as a String) with code (as Integer). This is
    72      * Associates VK_XXX (as a String) with code (as Integer). This is
    73      * done to avoid the overhead of the reflective call to find the
    73      * done to avoid the overhead of the reflective call to find the
    74      * constant.
    74      * constant.
    75      */
    75      */
    83     /*
    83     /*
    84      * Reads keystroke class from AppContext and if null, puts there the
    84      * Reads keystroke class from AppContext and if null, puts there the
    85      * AWTKeyStroke class.
    85      * AWTKeyStroke class.
    86      * Must be called under locked AWTKeyStro
    86      * Must be called under locked AWTKeyStro
    87      */
    87      */
    88     private static Class getAWTKeyStrokeClass() {
    88     private static Class<AWTKeyStroke> getAWTKeyStrokeClass() {
    89         Class clazz = (Class)AppContext.getAppContext().get(AWTKeyStroke.class);
    89         Class<AWTKeyStroke> clazz = (Class)AppContext.getAppContext().get(AWTKeyStroke.class);
    90         if (clazz == null) {
    90         if (clazz == null) {
    91             clazz = AWTKeyStroke.class;
    91             clazz = AWTKeyStroke.class;
    92             AppContext.getAppContext().put(AWTKeyStroke.class, AWTKeyStroke.class);
    92             AppContext.getAppContext().put(AWTKeyStroke.class, AWTKeyStroke.class);
    93         }
    93         }
    94         return clazz;
    94         return clazz;
   180     protected static void registerSubclass(Class<?> subclass) {
   180     protected static void registerSubclass(Class<?> subclass) {
   181         if (subclass == null) {
   181         if (subclass == null) {
   182             throw new IllegalArgumentException("subclass cannot be null");
   182             throw new IllegalArgumentException("subclass cannot be null");
   183         }
   183         }
   184         synchronized (AWTKeyStroke.class) {
   184         synchronized (AWTKeyStroke.class) {
   185             Class keyStrokeClass = (Class)AppContext.getAppContext().get(AWTKeyStroke.class);
   185             Class<AWTKeyStroke> keyStrokeClass = (Class)AppContext.getAppContext().get(AWTKeyStroke.class);
   186             if (keyStrokeClass != null && keyStrokeClass.equals(subclass)){
   186             if (keyStrokeClass != null && keyStrokeClass.equals(subclass)){
   187                 // Already registered
   187                 // Already registered
   188                 return;
   188                 return;
   189             }
   189             }
   190         }
   190         }
   227        threat as accessible flag is set only for this Constructor object,
   227        threat as accessible flag is set only for this Constructor object,
   228        not for Class constructor.
   228        not for Class constructor.
   229      */
   229      */
   230     private static Constructor getCtor(final Class clazz)
   230     private static Constructor getCtor(final Class clazz)
   231     {
   231     {
   232         Object ctor = AccessController.doPrivileged(new PrivilegedAction() {
   232         Constructor ctor = AccessController.doPrivileged(new PrivilegedAction<Constructor>() {
   233             public Object run() {
   233             public Constructor run() {
   234                 try {
   234                 try {
   235                     Constructor ctor = clazz.getDeclaredConstructor((Class[]) null);
   235                     Constructor ctor = clazz.getDeclaredConstructor((Class[]) null);
   236                     if (ctor != null) {
   236                     if (ctor != null) {
   237                         ctor.setAccessible(true);
   237                         ctor.setAccessible(true);
   238                     }
   238                     }
   247     }
   247     }
   248 
   248 
   249     private static synchronized AWTKeyStroke getCachedStroke
   249     private static synchronized AWTKeyStroke getCachedStroke
   250         (char keyChar, int keyCode, int modifiers, boolean onKeyRelease)
   250         (char keyChar, int keyCode, int modifiers, boolean onKeyRelease)
   251     {
   251     {
   252         Map cache = (Map)AppContext.getAppContext().get(APP_CONTEXT_CACHE_KEY);
   252         Map<AWTKeyStroke, AWTKeyStroke> cache = (Map)AppContext.getAppContext().get(APP_CONTEXT_CACHE_KEY);
   253         AWTKeyStroke cacheKey = (AWTKeyStroke)AppContext.getAppContext().get(APP_CONTEXT_KEYSTROKE_KEY);
   253         AWTKeyStroke cacheKey = (AWTKeyStroke)AppContext.getAppContext().get(APP_CONTEXT_KEYSTROKE_KEY);
   254 
   254 
   255         if (cache == null) {
   255         if (cache == null) {
   256             cache = new HashMap();
   256             cache = new HashMap<>();
   257             AppContext.getAppContext().put(APP_CONTEXT_CACHE_KEY, cache);
   257             AppContext.getAppContext().put(APP_CONTEXT_CACHE_KEY, cache);
   258         }
   258         }
   259 
   259 
   260         if (cacheKey == null) {
   260         if (cacheKey == null) {
   261             try {
   261             try {
   262                 Class clazz = getAWTKeyStrokeClass();
   262                 Class<AWTKeyStroke> clazz = getAWTKeyStrokeClass();
   263                 cacheKey = (AWTKeyStroke)getCtor(clazz).newInstance((Object[]) null);
   263                 cacheKey = (AWTKeyStroke)getCtor(clazz).newInstance((Object[]) null);
   264                 AppContext.getAppContext().put(APP_CONTEXT_KEYSTROKE_KEY, cacheKey);
   264                 AppContext.getAppContext().put(APP_CONTEXT_KEYSTROKE_KEY, cacheKey);
   265             } catch (InstantiationException e) {
   265             } catch (InstantiationException e) {
   266                 assert(false);
   266                 assert(false);
   267             } catch (IllegalAccessException e) {
   267             } catch (IllegalAccessException e) {
   511         boolean typed = false;
   511         boolean typed = false;
   512         boolean pressed = false;
   512         boolean pressed = false;
   513 
   513 
   514         synchronized (AWTKeyStroke.class) {
   514         synchronized (AWTKeyStroke.class) {
   515             if (modifierKeywords == null) {
   515             if (modifierKeywords == null) {
   516                 Map uninitializedMap = new HashMap(8, 1.0f);
   516                 Map<String, Integer> uninitializedMap = new HashMap<>(8, 1.0f);
   517                 uninitializedMap.put("shift",
   517                 uninitializedMap.put("shift",
   518                                      Integer.valueOf(InputEvent.SHIFT_DOWN_MASK
   518                                      Integer.valueOf(InputEvent.SHIFT_DOWN_MASK
   519                                                      |InputEvent.SHIFT_MASK));
   519                                                      |InputEvent.SHIFT_MASK));
   520                 uninitializedMap.put("control",
   520                 uninitializedMap.put("control",
   521                                      Integer.valueOf(InputEvent.CTRL_DOWN_MASK
   521                                      Integer.valueOf(InputEvent.CTRL_DOWN_MASK
   859     }
   859     }
   860 
   860 
   861 }
   861 }
   862 
   862 
   863 class VKCollection {
   863 class VKCollection {
   864     Map code2name;
   864     Map<Integer, String> code2name;
   865     Map name2code;
   865     Map<String, Integer> name2code;
   866 
   866 
   867     public VKCollection() {
   867     public VKCollection() {
   868         code2name = new HashMap();
   868         code2name = new HashMap<>();
   869         name2code = new HashMap();
   869         name2code = new HashMap<>();
   870     }
   870     }
   871 
   871 
   872     public synchronized void put(String name, Integer code) {
   872     public synchronized void put(String name, Integer code) {
   873         assert((name != null) && (code != null));
   873         assert((name != null) && (code != null));
   874         assert(findName(code) == null);
   874         assert(findName(code) == null);