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

File DeprecatedCheckUberspector.java

 

Coverage histogram

../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

12
21
5
1
118
69
20
0.95
4.2
5
4

Classes

Class Line # Actions
DeprecatedCheckUberspector 43 21 0% 20 3
0.9210526392.1%
 

Contributing tests

This file is covered by 58 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.velocity.introspection;
21   
22    import java.lang.reflect.Method;
23   
24    import org.apache.velocity.util.introspection.AbstractChainableUberspector;
25    import org.apache.velocity.util.introspection.Info;
26    import org.apache.velocity.util.introspection.Introspector;
27    import org.apache.velocity.util.introspection.Uberspect;
28    import org.apache.velocity.util.introspection.UberspectLoggable;
29    import org.apache.velocity.util.introspection.VelMethod;
30    import org.apache.velocity.util.introspection.VelPropertyGet;
31    import org.apache.velocity.util.introspection.VelPropertySet;
32   
33    /**
34    * Chainable Velocity Uberspector that checks for deprecated method calls. It does that by checking if the returned
35    * method has a Deprecated annotation. Because this is a chainable uberspector, it has to re-get the method using a
36    * default introspector, which is not safe; future uberspectors might not be able to return a precise method name, or a
37    * method of the original target object.
38    *
39    * @since 1.5M1
40    * @version $Id: 5ed2d8bc1fa378bab643da92aa535cd8caa05674 $
41    * @see ChainableUberspector
42    */
 
43    public class DeprecatedCheckUberspector extends AbstractChainableUberspector implements Uberspect, UberspectLoggable
44    {
 
45  95 toggle @Override
46    public void init()
47    {
48  95 super.init();
49  95 this.introspector = new Introspector(this.log);
50    }
51   
 
52  510677 toggle @Override
53    public VelMethod getMethod(Object obj, String methodName, Object[] args, Info i) throws Exception
54    {
55  510671 VelMethod method = super.getMethod(obj, methodName, args, i);
56  510660 if (method != null) {
57  456562 Method m = this.introspector.getMethod(obj.getClass(), method.getMethodName(), args);
58  456569 if (m != null
59    && (m.isAnnotationPresent(Deprecated.class)
60    || m.getDeclaringClass().isAnnotationPresent(Deprecated.class)
61    || obj.getClass().isAnnotationPresent(Deprecated.class))) {
62  3 logWarning("method", obj, method.getMethodName(), i);
63    }
64    }
65   
66  510665 return method;
67    }
68   
 
69  756693 toggle @Override
70    public VelPropertyGet getPropertyGet(Object obj, String identifier, Info i) throws Exception
71    {
72  756693 VelPropertyGet method = super.getPropertyGet(obj, identifier, i);
73  756673 if (method != null) {
74  754370 Method m = this.introspector.getMethod(obj.getClass(), method.getMethodName(), new Object[] {});
75  754357 if (m != null
76    && (m.isAnnotationPresent(Deprecated.class)
77    || m.getDeclaringClass().isAnnotationPresent(Deprecated.class)
78    || obj.getClass().isAnnotationPresent(Deprecated.class))) {
79  7 logWarning("getter", obj, method.getMethodName(), i);
80    }
81    }
82   
83  756677 return method;
84    }
85   
 
86  33979 toggle @Override
87    public VelPropertySet getPropertySet(Object obj, String identifier, Object arg, Info i) throws Exception
88    {
89    // TODO Auto-generated method stub
90  33979 VelPropertySet method = super.getPropertySet(obj, identifier, arg, i);
91  33978 if (method != null) {
92  33979 Method m = this.introspector.getMethod(obj.getClass(), method.getMethodName(), new Object[] { arg });
93  33980 if (m != null
94    && (m.isAnnotationPresent(Deprecated.class)
95    || m.getDeclaringClass().isAnnotationPresent(Deprecated.class)
96    || obj.getClass().isAnnotationPresent(Deprecated.class))) {
97  0 logWarning("setter", obj, method.getMethodName(), i);
98    }
99    }
100   
101  33979 return method;
102    }
103   
104    /**
105    * Helper method to log a warning when a deprecation has been found.
106    *
107    * @param deprecationType the type of deprecation (eg "getter", "setter", "method")
108    * @param object the object that has a deprecation
109    * @param methodName the deprecated method's name
110    * @param info a Velocity {@link org.apache.velocity.util.introspection.Info} object containing information about
111    * where the deprecation was located in the Velocity template file
112    */
 
113  10 toggle private void logWarning(String deprecationType, Object object, String methodName, Info info)
114    {
115  10 this.log.warn(String.format("Deprecated usage of %s [%s] in %s@%d,%d", deprecationType, object.getClass()
116    .getCanonicalName() + "." + methodName, info.getTemplateName(), info.getLine(), info.getColumn()));
117    }
118    }