我想知道我能做些什么来使它更具可读性和清洁性。可读性是指其他开发人员更容易阅读。
我真的不想两次使用相同的代码。我在想我可以做一些方法来缩短它,但我不确定......
@Override
public void dispatchEvent(Event event) {
checkNotNull(event);
CancellableEvent cancellableEvent = null;
boolean cancellable;
if (cancellable = event instanceof CancellableEvent) {
cancellableEvent = (CancellableEvent) event;
checkArgument(cancellableEvent.isCancelled());
}
// Ignore-cancellation event handlers will run
for (EventPriority priority : EventPriority.values()) {
Map<Method, EventListener> internalMapping = getRegistry().getMethodMap(event.getClass(), priority, true);
if (internalMapping != null) {
for (Entry<Method, EventListener> entry : internalMapping.entrySet()) {
try {
entry.getKey().invoke(entry.getValue(), event);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
/*
* Delegate any exceptions that occur from
* the method to a runtime exception.
*/
throw new RuntimeException(e);
}
}
}
}
// Event handlers that consider cancellation will run
for (EventPriority priority : EventPriority.values()) {
Map<Method, EventListener> internalMapping = getRegistry().getMethodMap(event.getClass(), priority, false);
if (internalMapping != null) {
for (Entry<Method, EventListener> entry : internalMapping.entrySet()) {
try {
entry.getKey().invoke(entry.getValue(), event);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
/*
* Delegate any exceptions that occur from
* the method to a runtime exception.
*/
throw new RuntimeException(e);
}
// Immediately return in the case of the event being cancelled.
if (cancellable && cancellableEvent.isCancelled()) {
return;
}
}
}
}
}