velocity-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cbris...@apache.org
Subject svn commit: r1753491 - in /velocity/engine/trunk: ./ velocity-engine-core/src/main/java/org/apache/velocity/util/introspection/ velocity-engine-core/src/test/java/org/apache/velocity/test/util/introspection/
Date Wed, 20 Jul 2016 12:07:33 GMT
Author: cbrisson
Date: Wed Jul 20 12:07:33 2016
New Revision: 1753491

URL: http://svn.apache.org/viewvc?rev=1753491&view=rev
Log:
[engine] borrow the DeprecatedCheckUberspector from the XWiki project

Added:
    velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/util/introspection/DeprecatedCheckUberspector.java
    velocity/engine/trunk/velocity-engine-core/src/test/java/org/apache/velocity/test/util/introspection/DeprecatedCheckUberspectorsTestCase.java
      - copied, changed from r1753137, velocity/engine/trunk/velocity-engine-core/src/test/java/org/apache/velocity/test/util/introspection/ChainedUberspectorsTestCase.java
Modified:
    velocity/engine/trunk/NOTICE

Modified: velocity/engine/trunk/NOTICE
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/NOTICE?rev=1753491&r1=1753490&r2=1753491&view=diff
==============================================================================
--- velocity/engine/trunk/NOTICE (original)
+++ velocity/engine/trunk/NOTICE Wed Jul 20 12:07:33 2016
@@ -9,3 +9,6 @@ The files
  - velocity-engine-scripting/src/main/java/org/apache/velocity/script/VelocityScriptEngine.java
  - velocity-engine-scripting/src/main/java/org/apache/velocity/script/VelocityScriptEngineFactory.java
 are Copyright 2006 Sun Microsystems, Inc., and licenced under a BSD-like licence.
+
+The file velocity-engine-core/src/main/java/org/apache/velocity/util/introspection/DeprecatedCheckUberspector.java
+is originating from the xwiki project, and licenced under the LGPL licence.

Added: velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/util/introspection/DeprecatedCheckUberspector.java
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/util/introspection/DeprecatedCheckUberspector.java?rev=1753491&view=auto
==============================================================================
--- velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/util/introspection/DeprecatedCheckUberspector.java
(added)
+++ velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/util/introspection/DeprecatedCheckUberspector.java
Wed Jul 20 12:07:33 2016
@@ -0,0 +1,111 @@
+/*
+ * See the NOTICE file distributed with this work for additional
+ * information regarding copyright ownership.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.apache.velocity.util.introspection;
+
+import java.lang.reflect.Method;
+
+/**
+ * Chainable Uberspector that checks for deprecated method calls. It does that by checking
if the returned
+ * method has a Deprecated annotation. Because this is a chainable uberspector, it has to
re-get the method using a
+ * default introspector, which is not safe; future uberspectors might not be able to return
a precise method name, or a
+ * method of the original target object.
+ *
+ * Borrowed from the XWiki project.
+ *
+ * @since 2.0
+ * @version $Id:$
+ * @see ChainableUberspector
+ */
+public class DeprecatedCheckUberspector extends AbstractChainableUberspector implements Uberspect,
UberspectLoggable
+{
+    @Override
+    public void init()
+    {
+        super.init();
+        this.introspector = new Introspector(this.log);
+    }
+
+    @Override
+    public VelMethod getMethod(Object obj, String methodName, Object[] args, Info i)
+    {
+        VelMethod method = super.getMethod(obj, methodName, args, i);
+        if (method != null) {
+            Method m = this.introspector.getMethod(obj.getClass(), method.getMethodName(),
args);
+            if (m != null
+                && (m.isAnnotationPresent(Deprecated.class)
+                    || m.getDeclaringClass().isAnnotationPresent(Deprecated.class)
+                    || obj.getClass().isAnnotationPresent(Deprecated.class))) {
+                logWarning("method", obj, method.getMethodName(), i);
+            }
+        }
+
+        return method;
+    }
+
+    @Override
+    public VelPropertyGet getPropertyGet(Object obj, String identifier, Info i)
+    {
+        VelPropertyGet method = super.getPropertyGet(obj, identifier, i);
+        if (method != null) {
+            Method m = this.introspector.getMethod(obj.getClass(), method.getMethodName(),
new Object[] {});
+            if (m != null
+                && (m.isAnnotationPresent(Deprecated.class)
+                    || m.getDeclaringClass().isAnnotationPresent(Deprecated.class)
+                    || obj.getClass().isAnnotationPresent(Deprecated.class))) {
+                logWarning("getter", obj, method.getMethodName(), i);
+            }
+        }
+
+        return method;
+    }
+
+    @Override
+    public VelPropertySet getPropertySet(Object obj, String identifier, Object arg, Info
i)
+    {
+        // TODO Auto-generated method stub
+        VelPropertySet method = super.getPropertySet(obj, identifier, arg, i);
+        if (method != null) {
+            Method m = this.introspector.getMethod(obj.getClass(), method.getMethodName(),
new Object[] { arg });
+            if (m != null
+                && (m.isAnnotationPresent(Deprecated.class)
+                    || m.getDeclaringClass().isAnnotationPresent(Deprecated.class)
+                    || obj.getClass().isAnnotationPresent(Deprecated.class))) {
+                logWarning("setter", obj, method.getMethodName(), i);
+            }
+        }
+
+        return method;
+    }
+
+    /**
+     * Helper method to log a warning when a deprecation has been found.
+     *
+     * @param deprecationType the type of deprecation (eg "getter", "setter", "method")
+     * @param object the object that has a deprecation
+     * @param methodName the deprecated method's name
+     * @param info a Velocity {@link org.apache.velocity.util.introspection.Info} object
containing information about
+     *            where the deprecation was located in the Velocity template file
+     */
+    private void logWarning(String deprecationType, Object object, String methodName, Info
info)
+    {
+        this.log.warn("Deprecated usage of {} [{}] in {}@{},{}", deprecationType, object.getClass()
+            .getCanonicalName() + "." + methodName, info.getTemplateName(), info.getLine(),
info.getColumn());
+    }
+}

Copied: velocity/engine/trunk/velocity-engine-core/src/test/java/org/apache/velocity/test/util/introspection/DeprecatedCheckUberspectorsTestCase.java
(from r1753137, velocity/engine/trunk/velocity-engine-core/src/test/java/org/apache/velocity/test/util/introspection/ChainedUberspectorsTestCase.java)
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/velocity-engine-core/src/test/java/org/apache/velocity/test/util/introspection/DeprecatedCheckUberspectorsTestCase.java?p2=velocity/engine/trunk/velocity-engine-core/src/test/java/org/apache/velocity/test/util/introspection/DeprecatedCheckUberspectorsTestCase.java&p1=velocity/engine/trunk/velocity-engine-core/src/test/java/org/apache/velocity/test/util/introspection/ChainedUberspectorsTestCase.java&r1=1753137&r2=1753491&rev=1753491&view=diff
==============================================================================
--- velocity/engine/trunk/velocity-engine-core/src/test/java/org/apache/velocity/test/util/introspection/ChainedUberspectorsTestCase.java
(original)
+++ velocity/engine/trunk/velocity-engine-core/src/test/java/org/apache/velocity/test/util/introspection/DeprecatedCheckUberspectorsTestCase.java
Wed Jul 20 12:07:33 2016
@@ -23,6 +23,7 @@ import junit.framework.Test;
 import junit.framework.TestSuite;
 import org.apache.velocity.VelocityContext;
 import org.apache.velocity.app.Velocity;
+import org.apache.velocity.app.VelocityEngine;
 import org.apache.velocity.test.BaseTestCase;
 import org.apache.velocity.test.misc.TestLogger;
 import org.apache.velocity.util.introspection.AbstractChainableUberspector;
@@ -32,13 +33,14 @@ import org.apache.velocity.util.introspe
 import org.apache.velocity.util.introspection.VelPropertySet;
 
 import java.io.StringWriter;
+import java.util.ArrayList;
 
 /**
- * Tests uberspectors chaining
+ * Tests DeprecatedCheckUberspector
  */
-public class ChainedUberspectorsTestCase extends BaseTestCase {
+public class DeprecatedCheckUberspectorsTestCase extends BaseTestCase {
 
-    public ChainedUberspectorsTestCase(String name)
+    public DeprecatedCheckUberspectorsTestCase(String name)
     	throws Exception
     {
         super(name);
@@ -46,72 +48,64 @@ public class ChainedUberspectorsTestCase
 
     public static Test suite()
     {
-        return new TestSuite(ChainedUberspectorsTestCase.class);
+        return new TestSuite(DeprecatedCheckUberspectorsTestCase.class);
     }
 
-    public void setUp()
-            throws Exception
+    protected void setUpEngine(VelocityEngine engine)
     {
-        Velocity.reset();
-        Velocity.setProperty(Velocity.RUNTIME_LOG_INSTANCE, new TestLogger());
-        Velocity.addProperty(Velocity.UBERSPECT_CLASSNAME,"org.apache.velocity.util.introspection.UberspectImpl");
-        Velocity.addProperty(Velocity.UBERSPECT_CLASSNAME,"org.apache.velocity.test.util.introspection.ChainedUberspectorsTestCase$ChainedUberspector");
-        Velocity.addProperty(Velocity.UBERSPECT_CLASSNAME,"org.apache.velocity.test.util.introspection.ChainedUberspectorsTestCase$LinkedUberspector");
-	    Velocity.init();
+        engine.setProperty(Velocity.RUNTIME_LOG_INSTANCE, new TestLogger(false, true));
+        engine.addProperty(Velocity.UBERSPECT_CLASSNAME, "org.apache.velocity.util.introspection.UberspectImpl");
+        engine.addProperty(Velocity.UBERSPECT_CLASSNAME, "org.apache.velocity.util.introspection.DeprecatedCheckUberspector");
     }
 
-    public void tearDown()
+    protected void setUpContext(VelocityContext context)
     {
+        context.put("obj1", new StandardObject());
+        context.put("obj2", new DeprecatedObject());
     }
 
-    public void testChaining()
+    public void testDeprecatedCheck()
     	throws Exception
     {
-        VelocityContext context = new VelocityContext();
-        context.put("foo",new Foo());
+        engine.init(); // make sure the engine is initialized, so that we get the logger
we configured
+        TestLogger logger =(TestLogger)engine.getLog();
+        logger.startCapture(); // reset log capture
         StringWriter writer = new StringWriter();
-
-        Velocity.evaluate(context,writer,"test","$foo.zeMethod()");
-        assertEquals(writer.toString(),"ok");
-
-        Velocity.evaluate(context,writer,"test","#set($foo.foo = 'someValue')");
-
-        writer = new StringWriter();
-        Velocity.evaluate(context,writer,"test","$foo.bar");
-        assertEquals(writer.toString(),"someValue");
-
-        writer = new StringWriter();
-        Velocity.evaluate(context,writer,"test","$foo.foo");
-        assertEquals(writer.toString(),"someValue");
+        engine.evaluate(context, writer, "test", "$obj1.foo() $obj1.bar $obj2.foo() $obj2.bar");
+        String log = logger.getLog();
+        String lines[] = log.split("\\r?\\n");
+        assertEquals(lines[0], "  [info] Deprecated usage of method [org.apache.velocity.test.util.introspection.DeprecatedCheckUberspectorsTestCase.StandardObject.foo]
in test@1,7");
+        assertEquals(lines[1], "  [info] Deprecated usage of getter [org.apache.velocity.test.util.introspection.DeprecatedCheckUberspectorsTestCase.StandardObject.getBar]
in test@1,19");
+        assertEquals(lines[2], "  [info] Deprecated usage of method [org.apache.velocity.test.util.introspection.DeprecatedCheckUberspectorsTestCase.DeprecatedObject.foo]
in test@1,29");
+        assertEquals(lines[3], "  [info] Deprecated usage of getter [org.apache.velocity.test.util.introspection.DeprecatedCheckUberspectorsTestCase.DeprecatedObject.getBar]
in test@1,41");
     }
 
-    // replaces getFoo by getBar
-    public static class ChainedUberspector extends AbstractChainableUberspector
+    public static class StandardObject
     {
-        public VelPropertySet getPropertySet(Object obj, String identifier, Object arg, Info
info)
+        @Deprecated
+        public String foo()
         {
-            identifier = identifier.replaceAll("foo","bar");
-            return inner.getPropertySet(obj,identifier,arg,info);
+            return "foo";
         }
-    }
 
-    // replaces setFoo by setBar
-    public static class LinkedUberspector extends UberspectImpl
-    {
-        public VelPropertyGet getPropertyGet(Object obj, String identifier, Info info)
+        @Deprecated
+        public String getBar()
         {
-            identifier = identifier.replaceAll("foo","bar");
-            return super.getPropertyGet(obj,identifier,info);
+            return "bar";
         }
     }
 
-    public static class Foo
+    @Deprecated
+    public static class DeprecatedObject
     {
-        private String bar;
+        public String foo()
+        {
+            return "foo";
+        }
 
-        public String zeMethod() { return "ok"; }
-        public String getBar() { return bar; }
-        public void setBar(String s) { bar = s; }
+        public String getBar()
+        {
+            return "bar";
+        }
     }
-
 }



Mime
View raw message