1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.filter.internal

File FilterProxy.java

 

Coverage histogram

../../../../img/srcFileCovDistChart4.png
78% of files have more coverage

Code metrics

16
24
4
1
124
63
13
0.54
6
4
3.25

Classes

Class Line # Actions
FilterProxy 40 24 0% 13 29
0.340909134.1%
 

Contributing tests

This file is covered by 56 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.filter.internal;
21   
22    import java.lang.reflect.InvocationHandler;
23    import java.lang.reflect.InvocationTargetException;
24    import java.lang.reflect.Method;
25   
26    import org.xwiki.filter.FilterDescriptor;
27    import org.xwiki.filter.FilterElementDescriptor;
28    import org.xwiki.filter.FilterElementParameterDescriptor;
29    import org.xwiki.filter.FilterEventParameters;
30    import org.xwiki.filter.FilterException;
31    import org.xwiki.filter.UnknownFilter;
32   
33    /**
34    * Helper for input module taking care of calling the right event when it exist, fallback on {@link UnknownFilter} or
35    * simply ignores it when the filter does not support it.
36    *
37    * @version $Id: 824f94860427ce42a524d7b98f951c66788e2a1f $
38    * @since 5.2M1
39    */
 
40    public final class FilterProxy implements InvocationHandler
41    {
42    /**
43    * The descriptor of the reference filter.
44    */
45    private FilterDescriptor descriptor;
46   
47    private Object targetFilter;
48   
49    /**
50    * @param filter the actual filter to send events to
51    * @param descriptor the reference filter descriptor
52    */
 
53  41602 toggle public FilterProxy(Object filter, FilterDescriptor descriptor)
54    {
55  41602 this.targetFilter = filter;
56  41602 this.descriptor = descriptor;
57    }
58   
 
59  1262585 toggle @Override
60    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable
61    {
62  1262586 try {
63  1262588 invoke(this.targetFilter, this.descriptor, method, args);
64    } catch (InvocationTargetException e) {
65  2 throw e.getCause();
66    }
67   
68  1262557 return null;
69    }
70   
71    /**
72    * @param filter the filter to send event to
73    * @param descriptor the descriptor of the filter
74    * @param method the event method called
75    * @param args the arguments of the called method
76    * @exception IllegalAccessException if this <code>Method</code> object enforces Java language access control and
77    * the underlying method is inaccessible.
78    * @exception IllegalArgumentException if the method is an instance method and the specified object argument is not
79    * an instance of the class or interface declaring the underlying method (or of a subclass or
80    * implementor thereof); if the number of actual and formal parameters differ; if an unwrapping
81    * conversion for primitive arguments fails; or if, after possible unwrapping, a parameter value
82    * cannot be converted to the corresponding formal parameter type by a method invocation conversion.
83    * @exception InvocationTargetException if the underlying method throws an exception.
84    * @throws FilterException if the execution of the event failed
85    */
 
86  1265593 toggle public static void invoke(Object filter, FilterDescriptor descriptor, Method method, Object[] args)
87    throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, FilterException
88    {
89  1265601 if (method.getDeclaringClass().isInstance(filter)) {
90  1264211 method.invoke(filter, args);
91  1388 } else if (filter instanceof UnknownFilter) {
92  0 invokeUnkown(filter, descriptor, method, args);
93    }
94    }
95   
 
96  0 toggle private static void invokeUnkown(Object filter, FilterDescriptor descriptor, Method method, Object[] args)
97    throws FilterException
98    {
99  0 String id = DefaultFilterDescriptorManager.getElementName(method);
100   
101  0 if (id != null) {
102  0 FilterElementDescriptor element = descriptor.getElement(id);
103   
104  0 if (element != null) {
105  0 FilterEventParameters metadata = new FilterEventParameters();
106   
107  0 for (FilterElementParameterDescriptor<?> parameter : element.getParameters()) {
108  0 metadata.put(
109  0 parameter.getName() != null ? parameter.getName() : String.valueOf(parameter.getIndex()),
110    args[parameter.getIndex()]);
111    }
112   
113  0 UnknownFilter unknownFilter = (UnknownFilter) filter;
114  0 if (method.getName().startsWith(DefaultFilterDescriptorManager.PREFIX_BEGIN)) {
115  0 unknownFilter.beginUnknwon(id, metadata);
116  0 } else if (method.getName().startsWith(DefaultFilterDescriptorManager.PREFIX_END)) {
117  0 unknownFilter.endUnknwon(id, metadata);
118  0 } else if (method.getName().startsWith(DefaultFilterDescriptorManager.PREFIX_ON)) {
119  0 unknownFilter.onUnknwon(id, metadata);
120    }
121    }
122    }
123    }
124    }