Search in sources :

Example 26 with WeakHashMap

use of java.util.WeakHashMap in project openmrs-core by openmrs.

the class HandlerUtil method getHandlersForType.

/**
 * Retrieves a List of all registered components from the Context that are of the passed
 * handlerType and one or more of the following is true:
 * <ul>
 * <li>The handlerType is annotated as a {@link Handler} that supports the passed type</li>
 * <li>The passed type is null - this effectively returns all components of the passed
 * handlerType</li>
 * </ul>
 * The returned handlers are ordered in the list based upon the order property.
 *
 * @param handlerType Indicates the type of class to return
 * @param type Indicates the type that the given handlerType must support (or null for any)
 * @return a List of all matching Handlers for the given parameters, ordered by Handler#order
 * @should return a list of all classes that can handle the passed type
 * @should return classes registered in a module
 * @should return an empty list if no classes can handle the passed type
 */
public static <H, T> List<H> getHandlersForType(Class<H> handlerType, Class<T> type) {
    List<?> list = cachedHandlers.get(new Key(handlerType, type));
    if (list != null) {
        return (List<H>) list;
    }
    List<H> handlers = new ArrayList<>();
    // First get all registered components of the passed class
    log.debug("Getting handlers of type " + handlerType + (type == null ? "" : " for class " + type.getName()));
    for (H handler : Context.getRegisteredComponents(handlerType)) {
        Handler handlerAnnotation = handler.getClass().getAnnotation(Handler.class);
        // Only consider those that have been annotated as Handlers
        if (handlerAnnotation != null) {
            // If no type is passed in return all handlers
            if (type == null) {
                log.debug("Found handler " + handler.getClass());
                handlers.add(handler);
            } else // Otherwise, return all handlers that support the passed type
            {
                for (int i = 0; i < handlerAnnotation.supports().length; i++) {
                    Class<?> clazz = handlerAnnotation.supports()[i];
                    if (clazz.isAssignableFrom(type)) {
                        log.debug("Found handler: " + handler.getClass());
                        handlers.add(handler);
                    }
                }
            }
        }
    }
    // Return the list of handlers based on the order specified in the Handler annotation
    handlers.sort(Comparator.comparing(o -> getOrderOfHandler(o.getClass())));
    Map<Key, List<?>> newCachedHandlers = new WeakHashMap<>(cachedHandlers);
    newCachedHandlers.put(new Key(handlerType, type), handlers);
    cachedHandlers = newCachedHandlers;
    return handlers;
}
Also used : Logger(org.slf4j.Logger) APIException(org.openmrs.api.APIException) LoggerFactory(org.slf4j.LoggerFactory) ApplicationListener(org.springframework.context.ApplicationListener) ArrayList(java.util.ArrayList) List(java.util.List) ContextRefreshedEvent(org.springframework.context.event.ContextRefreshedEvent) Component(org.springframework.stereotype.Component) Map(java.util.Map) Handler(org.openmrs.annotation.Handler) Comparator(java.util.Comparator) Context(org.openmrs.api.context.Context) WeakHashMap(java.util.WeakHashMap) ArrayList(java.util.ArrayList) Handler(org.openmrs.annotation.Handler) ArrayList(java.util.ArrayList) List(java.util.List) WeakHashMap(java.util.WeakHashMap)

Example 27 with WeakHashMap

use of java.util.WeakHashMap in project eclipse.platform.runtime by eclipse.

the class InjectorImpl method processMethods.

/**
 * Make the processor visit all declared methods on the given class.
 */
private boolean processMethods(final Object userObject, PrimaryObjectSupplier objectSupplier, PrimaryObjectSupplier tempSupplier, Class<?> objectsClass, ArrayList<Class<?>> classHierarchy, boolean track, List<Requestor<?>> requestors) {
    boolean injectedStatic = false;
    Method[] methods = getDeclaredMethods(objectsClass);
    for (Method method : methods) {
        Boolean isOverridden = null;
        Map<Method, Boolean> methodMap = null;
        Class<?> originalClass = userObject.getClass();
        if (isOverriddenCache.containsKey(originalClass)) {
            methodMap = isOverriddenCache.get(originalClass);
            if (methodMap.containsKey(method))
                isOverridden = methodMap.get(method);
        }
        if (isOverridden == null) {
            isOverridden = isOverridden(method, classHierarchy);
            if (methodMap == null) {
                methodMap = Collections.synchronizedMap(new WeakHashMap<>());
                isOverriddenCache.put(originalClass, methodMap);
            }
            methodMap.put(method, isOverridden);
        }
        if (isOverridden)
            // process in the subclass
            continue;
        if (Modifier.isStatic(method.getModifiers())) {
            if (hasInjectedStatic(objectsClass))
                continue;
            injectedStatic = true;
        }
        if (!isAnnotationPresent(method, Inject.class))
            continue;
        requestors.add(new MethodRequestor(method, this, objectSupplier, tempSupplier, userObject, track));
    }
    return injectedStatic;
}
Also used : Inject(javax.inject.Inject) Method(java.lang.reflect.Method) WeakHashMap(java.util.WeakHashMap)

Example 28 with WeakHashMap

use of java.util.WeakHashMap in project openj9 by eclipse.

the class J9VMInternals method recordInitializationFailure.

/**
 * Record the cause (Throwable) of a failed initialization for a Class.
 * If the Throwable is an Error, throw that, otherwise, wrap the Throwable
 * in an ExceptionInInitializerError and throw that instead.
 */
private static void recordInitializationFailure(Class clazz, Throwable err) {
    if (initialized) {
        // if exceptions is null, we're initializing and running single threaded
        if (exceptions == null)
            exceptions = new WeakHashMap();
        synchronized (exceptions) {
            Throwable cause = err;
            if (err instanceof ExceptionInInitializerError) {
                cause = ((ExceptionInInitializerError) err).getException();
                if (cause == null) {
                    /* Use the original ExceptionInInitializerError */
                    cause = err;
                }
            }
            exceptions.put(clazz, new SoftReference(copyThrowable(cause)));
        }
    }
    ensureError(err);
}
Also used : SoftReference(java.lang.ref.SoftReference) WeakHashMap(java.util.WeakHashMap)

Example 29 with WeakHashMap

use of java.util.WeakHashMap in project j2objc by google.

the class AttributedStringTest method test_addAttributeLjava_text_AttributedCharacterIterator$AttributeLjava_lang_Object.

/**
 * @tests java.text.AttributedString.addAttribute(AttributedCharacterIterator, Object)
 */
public void test_addAttributeLjava_text_AttributedCharacterIterator$AttributeLjava_lang_Object() {
    // regression for Harmony-1244
    AttributedString as = new AttributedString("123", new WeakHashMap());
    try {
        as.addAttribute(null, new TreeSet());
        fail("should throw NullPointerException");
    } catch (NullPointerException e) {
    // expected
    }
    try {
        as.addAttribute(null, null);
        fail("should throw NullPointerException");
    } catch (NullPointerException e) {
    // expected
    }
}
Also used : AttributedString(java.text.AttributedString) TreeSet(java.util.TreeSet) WeakHashMap(java.util.WeakHashMap)

Example 30 with WeakHashMap

use of java.util.WeakHashMap in project j2objc by google.

the class AttributedStringTest method test_addAttributeLjava_text_AttributedCharacterIterator$AttributeLjava_lang_ObjectII.

public void test_addAttributeLjava_text_AttributedCharacterIterator$AttributeLjava_lang_ObjectII() {
    AttributedString as = new AttributedString("test");
    as.addAttribute(AttributedCharacterIterator.Attribute.LANGUAGE, "a", 2, 3);
    AttributedCharacterIterator it = as.getIterator();
    assertEquals("non-null value limit", 2, it.getRunLimit(AttributedCharacterIterator.Attribute.LANGUAGE));
    as = new AttributedString("test");
    as.addAttribute(AttributedCharacterIterator.Attribute.LANGUAGE, null, 2, 3);
    it = as.getIterator();
    assertEquals("null value limit", 4, it.getRunLimit(AttributedCharacterIterator.Attribute.LANGUAGE));
    try {
        as = new AttributedString("test");
        as.addAttribute(AttributedCharacterIterator.Attribute.LANGUAGE, null, -1, 3);
        fail("Expected IllegalArgumentException");
    } catch (IllegalArgumentException e) {
    // Expected
    }
    // regression for Harmony-1244
    as = new AttributedString("123", new WeakHashMap());
    try {
        as.addAttribute(null, new TreeSet(), 0, 1);
        fail("should throw NullPointerException");
    } catch (NullPointerException e) {
    // expected
    }
    try {
        as.addAttribute(null, new TreeSet(), -1, 1);
        fail("should throw NullPointerException");
    } catch (NullPointerException e) {
    // expected
    }
}
Also used : AttributedString(java.text.AttributedString) TreeSet(java.util.TreeSet) AttributedCharacterIterator(java.text.AttributedCharacterIterator) WeakHashMap(java.util.WeakHashMap)

Aggregations

WeakHashMap (java.util.WeakHashMap)65 Map (java.util.Map)19 HashMap (java.util.HashMap)18 TreeMap (java.util.TreeMap)15 IdentityHashMap (java.util.IdentityHashMap)13 AbstractMap (java.util.AbstractMap)12 LinkedHashMap (java.util.LinkedHashMap)12 ICC_ColorSpace (java.awt.color.ICC_ColorSpace)8 ColorTransform (sun.java2d.cmm.ColorTransform)8 PCMM (sun.java2d.cmm.PCMM)8 Hashtable (java.util.Hashtable)6 AttributedString (java.text.AttributedString)5 Properties (java.util.Properties)5 ArrayList (java.util.ArrayList)4 Comparator (java.util.Comparator)4 List (java.util.List)4 TreeSet (java.util.TreeSet)4 WeakReference (java.lang.ref.WeakReference)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 AttributedCharacterIterator (java.text.AttributedCharacterIterator)3