/** * Guesses the clazz of a type. * * @param type * The type. * @return The clazz. */ public static Class<?> guessClazz(final Type type) { return GenericsUtil.guessClazz(type, 0); }
/** * Decide if we continue on guessing the class or we stick to Object. * * @param looped * The amount of times we looped. * @param type * The guessed type. * @return The guessed type. */ private static Class<?> decideGuessClazz(final int looped, final Type type) { Class<?> result; if (looped > GenericsUtil.MAX_SCAN_DEPTH) { result = Object.class; } else { result = GenericsUtil.guessClazz(type, looped); } return result; }
/** * Adds a converter. * * @param converter * The converter. */ public final void addConverter(final Converter<?> converter) { Validate.notNull(converter, AbstractDefaultConverterTool.CONVERTER_NULL); final Type converterType = GenericsUtil.getEntityGenericType(converter.getClass(), 0, Converter.class); this.converters.put(GenericsUtil.guessClazz(converterType), converter); }
/** * Handle an event. * * @param event * The event to handle. * @param <T> * The event type. * @throws EventException * Thrown if the event could not be processed. */ public <T> void handleEvent(final T event) throws EventException { final Class<?> type = event.getClass(); if (!this.cachedListeners.containsKey(event.getClass())) { this.cachedListeners.put(type, new LinkedList<ListenerInstancePair<?>>()); for (final ListenerInstancePair<?> listener : this.listeners) { final Type observableType = GenericsUtil.getEntityGenericType(listener.listener.getClass(), 0, Listener.class); final Class<?> observable = GenericsUtil.guessClazz(observableType); if (observable.isAssignableFrom(type)) { this.cachedListeners.get(type).add(listener); } } } @SuppressWarnings("unchecked") final List<ListenerInstancePair<T>> resolvedListeners = List.class.cast(this.cachedListeners.get(event.getClass())); for (final ListenerInstancePair<T> listener : resolvedListeners) { listener.handleEvent(event, null); } }
/** * Adds a listener. * * @param listener * The listener. * @param instance * The instance associated with this listener. * @param <T> * The listener event type. */ public <T> void addListener(final Listener<T> listener, final Object instance) { final ListenerInstancePair<T> pair = new ListenerInstancePair<T>(listener, instance); this.listeners.add(pair); final Type observableType = GenericsUtil.getEntityGenericType(listener.getClass(), 0, Listener.class); final Class<?> observable = GenericsUtil.guessClazz(observableType); if (!this.cachedListeners.containsKey(observable)) { this.cachedListeners.put(observable, new LinkedList<ListenerInstancePair<?>>()); } for (final Map.Entry<Class<?>, List<ListenerInstancePair<?>>> entry : this.cachedListeners.entrySet()) { final Class<?> type = entry.getKey(); if (observable.isAssignableFrom(type)) { entry.getValue().add(pair); } } }
/** * Adds a listener. * * @param listener * The listener. * @param instance * The instance associated with this listener. * @param <T> * The listener event type. */ public <T> void addListenerBefore(final Listener<T> listener, final Object instance) { final ListenerInstancePair<T> pair = new ListenerInstancePair<T>(listener, instance); this.listeners.add(pair); final Type observableType = GenericsUtil.getEntityGenericType(listener.getClass(), 0, Listener.class); final Class<?> observable = GenericsUtil.guessClazz(observableType); if (!this.cachedListeners.containsKey(observable)) { this.cachedListeners.put(observable, new LinkedList<ListenerInstancePair<?>>()); } for (final Map.Entry<Class<?>, List<ListenerInstancePair<?>>> entry : this.cachedListeners.entrySet()) { final Class<?> type = entry.getKey(); if (observable.isAssignableFrom(type)) { entry.getValue().add(0, pair); } } }
/** * Handle an event. * * @param event * The event to handle. * @param instance * The instance. * @param <T> * The event type. * @throws EventException * Thrown if the event could not be processed. */ public <T> void handleEvent(final T event, final Object instance) throws EventException { final Class<?> type = event.getClass(); if (!this.cachedListeners.containsKey(event.getClass())) { this.cachedListeners.put(type, new LinkedList<ListenerInstancePair<?>>()); for (final ListenerInstancePair<?> listener : this.listeners) { final Type observableType = GenericsUtil.getEntityGenericType(listener.listener.getClass(), 0, Listener.class); final Class<?> observable = GenericsUtil.guessClazz(observableType); if (observable.isAssignableFrom(type)) { this.cachedListeners.get(type).add(listener); } } } @SuppressWarnings("unchecked") final List<ListenerInstancePair<T>> resolvedListeners = List.class.cast(this.cachedListeners.get(event.getClass())); for (final ListenerInstancePair<T> listener : resolvedListeners) { listener.handleEvent(event, instance); } }
/** {@inheritDoc} */ @Override public void handleEvent(final UpdatedOperationReferenceEvent<E, B> event) throws EventException { SetResultListener.LOGGER.debug("Handling event {}", event); Validate.notNull(event, "Event may not be null."); final AnnotationOperationDescriptorBuilder<E, B> builder = event.getBuilder(); final Deque<Member> members = new LinkedList<Member>(); members.add(builder.getOperation()); members.addAll(builder.getBuilderContext().getAccessorContext().getDescribedProperties()); final java.lang.reflect.Type type = GenericsUtil.getRealType(members); final AnnotationResultDescriptorBuilder<R, E, B> resultDescriptor = builder.getResultBuilder(); if (type instanceof Class) { resultDescriptor.type((Class<?>) type); } else { final Class<?> guessedClazz = GenericsUtil.guessClazz(type); resultDescriptor.type(guessedClazz); } } }