1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements.  See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership.  The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License.  You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied.  See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */
19 module hunt.shiro.event.support.AnnotationEventListenerResolver;
20 
21 import hunt.shiro.event.support.EventListener;
22 import hunt.shiro.event.support.EventListenerResolver;
23 
24 import hunt.shiro.event.Subscribe;
25 
26 import hunt.collection.Collections;
27 import hunt.collection.List;
28 import hunt.Exceptions;
29 // import hunt.shiro.util.ClassUtils;
30 
31 // import java.lang.annotation.Annotation;
32 // import java.lang.reflect.Method;
33 // import java.util.ArrayList;
34 // import java.util.Collections;
35 // import java.util.List;
36 
37 /**
38  * Inspects an object for annotated methods of interest and creates an {@link EventListener} instance for each method
39  * discovered.  An event bus will call the resulting listeners as relevant events arrive.
40  * <p/>
41  * The default {@link #setAnnotationClass(Class) annotationClass} is {@link Subscribe}, indicating each
42  * {@link Subscribe}-annotated method will be represented as an EventListener.
43  *
44  * @see SingleArgumentMethodEventListener
45  * @since 1.3
46  */
47 class AnnotationEventListenerResolver : EventListenerResolver {
48 
49     private TypeInfo_Class annotationClass;
50 
51     this() {
52         this.annotationClass = Subscribe.classinfo;
53     }
54 
55     /**
56      * Returns a new collection of {@link EventListener} instances, each instance corresponding to an annotated
57      * method discovered on the specified {@code instance} argument.
58      *
59      * @param instance the instance to inspect for annotated event handler methods.
60      * @return a new collection of {@link EventListener} instances, each instance corresponding to an annotated
61      *         method discovered on the specified {@code instance} argument.
62      */
63     EventListener[] getEventListeners(Object instance) {
64         if (instance is null) {
65             return [];
66         }
67 
68         // List<Method> methods = ClassUtils.getAnnotatedMethods(instance.getClass(), getAnnotationClass());
69         // if (methods is null || methods.isEmpty()) {
70         //     return Collections.emptyList();
71         // }
72 
73         // List!EventListener listeners = new ArrayList!EventListener(methods.size());
74 
75         // for (Method m : methods) {
76         //     listeners.add(new SingleArgumentMethodEventListener(instance, m));
77         // }
78 
79         // return listeners;
80         implementationMissing(false);
81         return null;
82     }
83 
84     /**
85      * Returns the type of annotation that indicates a method that should be represented as an {@link EventListener},
86      * defaults to {@link Subscribe}.
87      *
88      * @return the type of annotation that indicates a method that should be represented as an {@link EventListener},
89      *         defaults to {@link Subscribe}.
90      */
91     TypeInfo_Class getAnnotationClass() {
92         return annotationClass;
93     }
94 
95     /**
96      * Sets the type of annotation that indicates a method that should be represented as an {@link EventListener}.
97      * The default value is {@link Subscribe}.
98      *
99      * @param annotationClass the type of annotation that indicates a method that should be represented as an
100      *                        {@link EventListener}.  The default value is {@link Subscribe}.
101      */
102     void setAnnotationClass(TypeInfo_Class annotationClass) {
103         this.annotationClass = annotationClass;
104     }
105 }