| <?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>SingleArgumentMethodEventListener.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 :: Test Coverage</a> > <a href="../index.html" class="el_bundle">shiro-event</a> > <a href="index.source.html" class="el_package">org.apache.shiro.event.support</a> > <span class="el_source">SingleArgumentMethodEventListener.java</span></div><h1>SingleArgumentMethodEventListener.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 |
| * "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 org.apache.shiro.event.support; |
| |
| import java.lang.reflect.Method; |
| import java.lang.reflect.Modifier; |
| |
| /** |
| * A event listener that invokes a target object's method that accepts a single event argument. |
| * |
| * @since 1.3 |
| */ |
| public class SingleArgumentMethodEventListener implements TypedEventListener { |
| |
| private final Object target; |
| private final Method method; |
| |
| <span class="fc" id="L34"> public SingleArgumentMethodEventListener(Object target, Method method) {</span> |
| <span class="fc" id="L35"> this.target = target;</span> |
| <span class="fc" id="L36"> this.method = method;</span> |
| //assert that the method is defined as expected: |
| <span class="fc" id="L38"> getMethodArgumentType(method);</span> |
| |
| <span class="fc" id="L40"> assertPublicMethod(method);</span> |
| <span class="fc" id="L41"> }</span> |
| |
| public Object getTarget() { |
| <span class="fc" id="L44"> return this.target;</span> |
| } |
| |
| public Method getMethod() { |
| <span class="fc" id="L48"> return this.method;</span> |
| } |
| |
| private void assertPublicMethod(Method method) { |
| <span class="fc" id="L52"> int modifiers = method.getModifiers();</span> |
| <span class="fc bfc" id="L53" title="All 2 branches covered."> if (!Modifier.isPublic(modifiers)) {</span> |
| <span class="fc" id="L54"> throw new IllegalArgumentException("Event handler method [" + method + "] must be public.");</span> |
| } |
| <span class="fc" id="L56"> }</span> |
| |
| public boolean accepts(Object event) { |
| <span class="pc bpc" id="L59" title="1 of 4 branches missed."> return event != null && getEventType().isInstance(event);</span> |
| } |
| |
| public Class getEventType() { |
| <span class="fc" id="L63"> return getMethodArgumentType(getMethod());</span> |
| } |
| |
| public void onEvent(Object event) { |
| <span class="fc" id="L67"> Method method = getMethod();</span> |
| try { |
| <span class="fc" id="L69"> method.invoke(getTarget(), event);</span> |
| <span class="fc" id="L70"> } catch (Exception e) {</span> |
| <span class="fc" id="L71"> throw new IllegalStateException("Unable to invoke event handler method [" + method + "].", e);</span> |
| <span class="fc" id="L72"> }</span> |
| <span class="fc" id="L73"> }</span> |
| |
| protected Class getMethodArgumentType(Method method) { |
| <span class="fc" id="L76"> Class[] paramTypes = method.getParameterTypes();</span> |
| <span class="fc bfc" id="L77" title="All 2 branches covered."> if (paramTypes.length != 1) {</span> |
| //the default implementation expects a single typed argument and nothing more: |
| <span class="fc" id="L79"> String msg = "Event handler methods must accept a single argument.";</span> |
| <span class="fc" id="L80"> throw new IllegalArgumentException(msg);</span> |
| } |
| <span class="fc" id="L82"> return paramTypes[0];</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> |