com.mycila.event.internal.Subscribers Maven / Gradle / Ivy
The newest version!
/**
* Copyright (C) 2010 Mycila ([email protected])
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.mycila.event.internal;
import com.mycila.event.Event;
import com.mycila.event.EventRequest;
import com.mycila.event.Reachability;
import com.mycila.event.Referencable;
import com.mycila.event.Subscriber;
import com.mycila.event.SubscriberExecutionException;
import com.mycila.event.annotation.Reference;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import static com.mycila.event.internal.Ensure.hasSomeArgs;
import static com.mycila.event.internal.Ensure.notNull;
/**
* @author Mathieu Carbou ([email protected])
*/
public final class Subscribers {
private Subscribers() {
}
public static Subscriber> createSubscriber(Object instance, Method method) {
return new MethodSubscriber(instance, method);
}
public static Subscriber extends EventRequest>> createResponder(Object instance, Method method) {
return new MethodResponder(instance, method);
}
private static class ReferencableMethod implements Referencable {
final Reachability reachability;
final Object target;
final MethodInvoker invoker;
final Class>[] argTypes;
ReferencableMethod(Object target, final Method method) {
notNull(target, "Target object");
notNull(method, "Method");
this.argTypes = method.getParameterTypes();
this.target = target;
this.invoker = Proxy.invoker(method);
this.reachability = method.isAnnotationPresent(Reference.class) ?
method.getAnnotation(Reference.class).value() :
Reachability.of(target.getClass());
}
@Override
public final Reachability getReachability() {
return reachability;
}
}
private static final class MethodSubscriber extends ReferencableMethod implements Subscriber