GroovyAwareDefaultEventRouter.java
01 /*
02  * Copyright 2008-2016 the original author or authors.
03  *
04  * Licensed under the Apache License, Version 2.0 (the "License");
05  * you may not use this file except in compliance with the License.
06  * You may obtain a copy of the License at
07  *
08  *     http://www.apache.org/licenses/LICENSE-2.0
09  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.codehaus.griffon.runtime.groovy.event;
17 
18 import groovy.lang.Closure;
19 import org.codehaus.griffon.runtime.core.event.DefaultEventRouter;
20 
21 import javax.annotation.Nonnull;
22 import java.lang.reflect.Proxy;
23 
24 import static griffon.util.GriffonClassUtils.getFieldValue;
25 import static java.lang.reflect.Proxy.getInvocationHandler;
26 
27 /**
28  @author Andres Almiray
29  @since 2.5.0
30  */
31 public class GroovyAwareDefaultEventRouter extends DefaultEventRouter {
32     @Override
33     protected boolean isNestedListener(@Nonnull Object listener, @Nonnull Object owner) {
34         if (listener instanceof Proxy) {
35             try {
36                 Object delegate = getFieldValue(getInvocationHandler(listener)"delegate");
37                 if (delegate instanceof Closure) {
38                     return owner.equals(((Closuredelegate).getOwner());
39                 }
40             catch (Exception e) {
41                 // ignore
42             }
43         }
44         return super.isNestedListener(listener, owner);
45     }
46 }