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;
}
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;
}
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);
}
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
}
}
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
}
}
Aggregations