1. Project Clover database Sat Feb 2 2019 06:45:20 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
117
69
20
0.95
4.2
5
4

Classes

Class Line # Actions
DeprecatedCheckUberspector 42 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: 7836007548b28e9d6f8a3a0f8b4a5da4619c3020 $
41    */
 
42    public class DeprecatedCheckUberspector extends AbstractChainableUberspector implements Uberspect, UberspectLoggable
43    {
 
44  102 toggle @Override
45    public void init()
46    {
47  102 super.init();
48  102 this.introspector = new Introspector(this.log);
49    }
50   
 
51  6893489 toggle @Override
52    public VelMethod getMethod(Object obj, String methodName, Object[] args, Info i) throws Exception
53    {
54  6895299 VelMethod method = super.getMethod(obj, methodName, args, i);
55  6893787 if (method != null) {
56  6046839 Method m = this.introspector.getMethod(obj.getClass(), method.getMethodName(), args);
57  6050277 if (m != null
58    && (m.isAnnotationPresent(Deprecated.class)
59    || m.getDeclaringClass().isAnnotationPresent(Deprecated.class)
60    || obj.getClass().isAnnotationPresent(Deprecated.class))) {
61  20 logWarning("method", obj, method.getMethodName(), i);
62    }
63    }
64   
65  6897789 return method;
66    }
67   
 
68  11002962 toggle @Override
69    public VelPropertyGet getPropertyGet(Object obj, String identifier, Info i) throws Exception
70    {
71  11003306 VelPropertyGet method = super.getPropertyGet(obj, identifier, i);
72  11003010 if (method != null) {
73  10770261 Method m = this.introspector.getMethod(obj.getClass(), method.getMethodName(), new Object[] {});
74  10770042 if (m != null
75    && (m.isAnnotationPresent(Deprecated.class)
76    || m.getDeclaringClass().isAnnotationPresent(Deprecated.class)
77    || obj.getClass().isAnnotationPresent(Deprecated.class))) {
78  82 logWarning("getter", obj, method.getMethodName(), i);
79    }
80    }
81   
82  11002340 return method;
83    }
84   
 
85  454088 toggle @Override
86    public VelPropertySet getPropertySet(Object obj, String identifier, Object arg, Info i) throws Exception
87    {
88    // TODO Auto-generated method stub
89  454090 VelPropertySet method = super.getPropertySet(obj, identifier, arg, i);
90  454087 if (method != null) {
91  454088 Method m = this.introspector.getMethod(obj.getClass(), method.getMethodName(), new Object[] { arg });
92  454089 if (m != null
93    && (m.isAnnotationPresent(Deprecated.class)
94    || m.getDeclaringClass().isAnnotationPresent(Deprecated.class)
95    || obj.getClass().isAnnotationPresent(Deprecated.class))) {
96  0 logWarning("setter", obj, method.getMethodName(), i);
97    }
98    }
99   
100  454089 return method;
101    }
102   
103    /**
104    * Helper method to log a warning when a deprecation has been found.
105    *
106    * @param deprecationType the type of deprecation (eg "getter", "setter", "method")
107    * @param object the object that has a deprecation
108    * @param methodName the deprecated method's name
109    * @param info a Velocity {@link org.apache.velocity.util.introspection.Info} object containing information about
110    * where the deprecation was located in the Velocity template file
111    */
 
112  102 toggle private void logWarning(String deprecationType, Object object, String methodName, Info info)
113    {
114  102 this.log.warn(String.format("Deprecated usage of %s [%s] in %s@%d,%d", deprecationType, object.getClass()
115    .getCanonicalName() + "." + methodName, info.getTemplateName(), info.getLine(), info.getColumn()));
116    }
117    }