Skip to content
Eric Bodden edited this page Mar 15, 2015 · 1 revision
package java.awt;
AWTKeyStroke.class
    private static synchronized AWTKeyStroke getCachedStroke
        (char keyChar, int keyCode, int modifiers, boolean onKeyRelease)
    {
	if (cache == null) {
	    cache = new HashMap();
	}
 
	if (cacheKey == null) {
	    try {
		cacheKey = (AWTKeyStroke)ctor.newInstance((Object[]) null);
	    } catch (InstantiationException e) {
                assert(false); 
            } catch (IllegalAccessException e) {
                assert(false); 
            } catch (InvocationTargetException e) {
                assert(false); 
	    }
	}
	cacheKey.keyChar = keyChar;
	cacheKey.keyCode = keyCode;
	cacheKey.modifiers = mapNewModifiers(mapOldModifiers(modifiers));
	cacheKey.onKeyRelease = onKeyRelease;
 
	AWTKeyStroke stroke = (AWTKeyStroke)cache.get(cacheKey);
	if (stroke == null) {
	    stroke = cacheKey;
	    cache.put(stroke, stroke);
	    cacheKey = null;
	}
	
	return stroke;
    }

package java.awt;
AWTKeyStroke.class 
   protected static void registerSubclass(Class<?> subclass) {
        if (subclass == null) {
	    throw new IllegalArgumentException("subclass cannot be null");
	}
	if (AWTKeyStroke.ctor.getDeclaringClass().equals(subclass)) {
	    // Already registered
	    return;
	}
	if (!AWTKeyStroke.class.isAssignableFrom(subclass)) {
	    throw new ClassCastException("subclass is not derived from AWTKeyStroke");
	}

        Constructor ctor = getCtor(subclass);

	String couldNotInstantiate = "subclass could not be instantiated";

        if (ctor == null) {
	    throw new IllegalArgumentException(couldNotInstantiate);
	}
	try {
	    AWTKeyStroke stroke = (AWTKeyStroke)ctor.newInstance((Object[]) null);
	    if (stroke == null) {
	        throw new IllegalArgumentException(couldNotInstantiate);
	    }
	} catch (NoSuchMethodError e) {
	    throw new IllegalArgumentException(couldNotInstantiate);
	} catch (ExceptionInInitializerError e) {
	    throw new IllegalArgumentException(couldNotInstantiate);
	} catch (InstantiationException e) {
	    throw new IllegalArgumentException(couldNotInstantiate);
	} catch (IllegalAccessException e) {
	    throw new IllegalArgumentException(couldNotInstantiate);
	} catch (InvocationTargetException e) {
	    throw new IllegalArgumentException(couldNotInstantiate);
	}

	synchronized (AWTKeyStroke.class) {
	    AWTKeyStroke.ctor = ctor;
	    cache = null;
	    cacheKey = null;
	}
    }
Clone this wiki locally