blob: 3a5bba10145b73229841f02d0544f10142a03844 [file] [log] [blame]
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="en"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><link rel="stylesheet" href="../../jacoco-resources/report.css" type="text/css"/><link rel="shortcut icon" href="../../jacoco-resources/report.gif" type="image/gif"/><title>AnnotationEventListenerResolver.java</title><link rel="stylesheet" href="../../jacoco-resources/prettify.css" type="text/css"/><script type="text/javascript" src="../../jacoco-resources/prettify.js"></script></head><body onload="window['PR_TAB_WIDTH']=4;prettyPrint()"><div class="breadcrumb" id="breadcrumb"><span class="info"><a href="../../jacoco-sessions.html" class="el_session">Sessions</a></span><a href="../../index.html" class="el_report">Apache Shiro :: All (aggregate jar)</a> &gt; <a href="../index.html" class="el_bundle">shiro-event</a> &gt; <a href="index.source.html" class="el_package">org.apache.shiro.event.support</a> &gt; <span class="el_source">AnnotationEventListenerResolver.java</span></div><h1>AnnotationEventListenerResolver.java</h1><pre class="source lang-java linenums">/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* &quot;License&quot;); 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
* &quot;AS IS&quot; 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 org.apache.shiro.event.support;
import org.apache.shiro.event.Subscribe;
import org.apache.shiro.util.ClassUtils;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* Inspects an object for annotated methods of interest and creates an {@link EventListener} instance for each method
* discovered. An event bus will call the resulting listeners as relevant events arrive.
* &lt;p/&gt;
* The default {@link #setAnnotationClass(Class) annotationClass} is {@link Subscribe}, indicating each
* {@link Subscribe}-annotated method will be represented as an EventListener.
*
* @see SingleArgumentMethodEventListener
* @since 1.3
*/
public class AnnotationEventListenerResolver implements EventListenerResolver {
private Class&lt;? extends Annotation&gt; annotationClass;
<span class="fc" id="L44"> public AnnotationEventListenerResolver() {</span>
<span class="fc" id="L45"> this.annotationClass = Subscribe.class;</span>
<span class="fc" id="L46"> }</span>
/**
* Returns a new collection of {@link EventListener} instances, each instance corresponding to an annotated
* method discovered on the specified {@code instance} argument.
*
* @param instance the instance to inspect for annotated event handler methods.
* @return a new collection of {@link EventListener} instances, each instance corresponding to an annotated
* method discovered on the specified {@code instance} argument.
*/
public List&lt;EventListener&gt; getEventListeners(Object instance) {
<span class="fc bfc" id="L57" title="All 2 branches covered."> if (instance == null) {</span>
<span class="fc" id="L58"> return Collections.emptyList();</span>
}
<span class="fc" id="L61"> List&lt;Method&gt; methods = ClassUtils.getAnnotatedMethods(instance.getClass(), getAnnotationClass());</span>
<span class="pc bpc" id="L62" title="1 of 4 branches missed."> if (methods == null || methods.isEmpty()) {</span>
<span class="fc" id="L63"> return Collections.emptyList();</span>
}
<span class="fc" id="L66"> List&lt;EventListener&gt; listeners = new ArrayList&lt;EventListener&gt;(methods.size());</span>
<span class="fc bfc" id="L68" title="All 2 branches covered."> for (Method m : methods) {</span>
<span class="fc" id="L69"> listeners.add(new SingleArgumentMethodEventListener(instance, m));</span>
<span class="fc" id="L70"> }</span>
<span class="fc" id="L72"> return listeners;</span>
}
/**
* Returns the type of annotation that indicates a method that should be represented as an {@link EventListener},
* defaults to {@link Subscribe}.
*
* @return the type of annotation that indicates a method that should be represented as an {@link EventListener},
* defaults to {@link Subscribe}.
*/
public Class&lt;? extends Annotation&gt; getAnnotationClass() {
<span class="fc" id="L83"> return annotationClass;</span>
}
/**
* Sets the type of annotation that indicates a method that should be represented as an {@link EventListener}.
* The default value is {@link Subscribe}.
*
* @param annotationClass the type of annotation that indicates a method that should be represented as an
* {@link EventListener}. The default value is {@link Subscribe}.
*/
public void setAnnotationClass(Class&lt;? extends Annotation&gt; annotationClass) {
<span class="fc" id="L94"> this.annotationClass = annotationClass;</span>
<span class="fc" id="L95"> }</span>
}
</pre><div class="footer"><span class="right">Created with <a href="http://www.eclemma.org/jacoco">JaCoCo</a> 0.7.7.201606060606</span></div></body></html>