1 package org.apache.velocity.util.introspection; 2 3 /* 4 * Licensed to the Apache Software Foundation (ASF) under one 5 * or more contributor license agreements. See the NOTICE file 6 * distributed with this work for additional information 7 * regarding copyright ownership. The ASF licenses this file 8 * to you under the Apache License, Version 2.0 (the 9 * "License"); you may not use this file except in compliance 10 * with the License. You may obtain a copy of the License at 11 * 12 * http://www.apache.org/licenses/LICENSE-2.0 13 * 14 * Unless required by applicable law or agreed to in writing, 15 * software distributed under the License is distributed on an 16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 17 * KIND, either express or implied. See the License for the 18 * specific language governing permissions and limitations 19 * under the License. 20 */ 21 22 import java.lang.reflect.Method; 23 24 /** 25 * Chainable Uberspector that checks for deprecated method calls. It does that by checking if the returned 26 * method has a Deprecated annotation. Because this is a chainable uberspector, it has to re-get the method using a 27 * default introspector, which is not safe; future uberspectors might not be able to return a precise method name, or a 28 * method of the original target object. 29 * 30 * Borrowed from the XWiki project. 31 * 32 * @since 2.0 33 * @version $Id:$ 34 * @see ChainableUberspector 35 */ 36 public class DeprecatedCheckUberspector extends AbstractChainableUberspector implements Uberspect 37 { 38 @Override init()39 public void init() 40 { 41 super.init(); 42 this.introspector = new Introspector(this.log); 43 } 44 45 @Override getMethod(Object obj, String methodName, Object[] args, Info i)46 public VelMethod getMethod(Object obj, String methodName, Object[] args, Info i) 47 { 48 VelMethod method = super.getMethod(obj, methodName, args, i); 49 if (method != null) { 50 Method m = this.introspector.getMethod(obj.getClass(), method.getMethodName(), args); 51 if (m != null 52 && (m.isAnnotationPresent(Deprecated.class) 53 || m.getDeclaringClass().isAnnotationPresent(Deprecated.class) 54 || obj.getClass().isAnnotationPresent(Deprecated.class))) { 55 logWarning("method", obj, method.getMethodName(), i); 56 } 57 } 58 59 return method; 60 } 61 62 @Override getPropertyGet(Object obj, String identifier, Info i)63 public VelPropertyGet getPropertyGet(Object obj, String identifier, Info i) 64 { 65 VelPropertyGet method = super.getPropertyGet(obj, identifier, i); 66 if (method != null) { 67 Method m = this.introspector.getMethod(obj.getClass(), method.getMethodName(), new Object[] {}); 68 if (m != null 69 && (m.isAnnotationPresent(Deprecated.class) 70 || m.getDeclaringClass().isAnnotationPresent(Deprecated.class) 71 || obj.getClass().isAnnotationPresent(Deprecated.class))) { 72 logWarning("getter", obj, method.getMethodName(), i); 73 } 74 } 75 76 return method; 77 } 78 79 @Override getPropertySet(Object obj, String identifier, Object arg, Info i)80 public VelPropertySet getPropertySet(Object obj, String identifier, Object arg, Info i) 81 { 82 // TODO Auto-generated method stub 83 VelPropertySet method = super.getPropertySet(obj, identifier, arg, i); 84 if (method != null) { 85 Method m = this.introspector.getMethod(obj.getClass(), method.getMethodName(), new Object[] { arg }); 86 if (m != null 87 && (m.isAnnotationPresent(Deprecated.class) 88 || m.getDeclaringClass().isAnnotationPresent(Deprecated.class) 89 || obj.getClass().isAnnotationPresent(Deprecated.class))) { 90 logWarning("setter", obj, method.getMethodName(), i); 91 } 92 } 93 94 return method; 95 } 96 97 /** 98 * Helper method to log a warning when a deprecation has been found. 99 * 100 * @param deprecationType the type of deprecation (eg "getter", "setter", "method") 101 * @param object the object that has a deprecation 102 * @param methodName the deprecated method's name 103 * @param info a Velocity {@link org.apache.velocity.util.introspection.Info} object containing information about 104 * where the deprecation was located in the Velocity template file 105 */ logWarning(String deprecationType, Object object, String methodName, Info info)106 private void logWarning(String deprecationType, Object object, String methodName, Info info) 107 { 108 this.log.warn("Deprecated usage of {} [{}] in {}@{},{}", deprecationType, object.getClass() 109 .getCanonicalName() + "." + methodName, info.getTemplateName(), info.getLine(), info.getColumn()); 110 } 111 } 112