You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@velocity.apache.org by cb...@apache.org on 2020/01/07 09:37:18 UTC

svn commit: r1872422 - in /velocity/engine/trunk/velocity-engine-core/src: main/java/org/apache/velocity/runtime/parser/node/ main/java/org/apache/velocity/util/ test/java/org/apache/velocity/test/ test/java/org/apache/velocity/test/issues/

Author: cbrisson
Date: Tue Jan  7 09:37:17 2020
New Revision: 1872422

URL: http://svn.apache.org/viewvc?rev=1872422&view=rev
Log:
[engine] Fix VELOCITY-924 - cache collision between an object and its class

Added:
    velocity/engine/trunk/velocity-engine-core/src/test/java/org/apache/velocity/test/issues/Velocity924TestCase.java   (with props)
Modified:
    velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/ASTMethod.java
    velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/util/ClassUtils.java
    velocity/engine/trunk/velocity-engine-core/src/test/java/org/apache/velocity/test/MethodCacheKeyTestCase.java
    velocity/engine/trunk/velocity-engine-core/src/test/java/org/apache/velocity/test/issues/Velocity855TestCase.java

Modified: velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/ASTMethod.java
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/ASTMethod.java?rev=1872422&r1=1872421&r2=1872422&view=diff
==============================================================================
--- velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/ASTMethod.java (original)
+++ velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/ASTMethod.java Tue Jan  7 09:37:17 2020
@@ -334,10 +334,26 @@ public class ASTMethod extends SimpleNod
      */
     public static class MethodCacheKey
     {
+        /**
+         * method name
+         */
         private final String methodName;
+
+        /**
+         * parameters classes
+         */
         private final Class[] params;
 
-        public MethodCacheKey(String methodName, Class[] params)
+        /**
+         * whether the target object is of Class type
+         * (meaning we're searching either for methods
+         * of Class, or for static methods of the class
+         * this Class objects refers to)
+         * @since 2.2
+         */
+        private boolean classObject;
+
+        public MethodCacheKey(String methodName, Class[] params, boolean classObject)
         {
             /**
              * Should never be initialized with nulls, but to be safe we refuse
@@ -345,6 +361,7 @@ public class ASTMethod extends SimpleNod
              */
             this.methodName = (methodName != null) ? methodName : StringUtils.EMPTY;
             this.params = (params != null) ? params : EMPTY_CLASS_ARRAY;
+            this.classObject = classObject;
         }
 
         /**
@@ -360,7 +377,8 @@ public class ASTMethod extends SimpleNod
             {
                 final MethodCacheKey other = (MethodCacheKey) o;
                 if (params.length == other.params.length &&
-                        methodName.equals(other.methodName))
+                        methodName.equals(other.methodName) &&
+                            classObject == other.classObject)
                 {
                     for (int i = 0; i < params.length; ++i)
                     {

Modified: velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/util/ClassUtils.java
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/util/ClassUtils.java?rev=1872422&r1=1872421&r2=1872422&view=diff
==============================================================================
--- velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/util/ClassUtils.java (original)
+++ velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/util/ClassUtils.java Tue Jan  7 09:37:17 2020
@@ -179,9 +179,10 @@ public class ClassUtils {
             /*
             * check the cache
             */
-            MethodCacheKey mck = new MethodCacheKey(methodName, paramClasses);
+            boolean classObject = (o instanceof Class);
+            MethodCacheKey mck = new MethodCacheKey(methodName, paramClasses, classObject);
             IntrospectionCacheData icd = context.icacheGet(mck);
-            Class clazz = o instanceof Class ? (Class)o : o.getClass();
+            Class clazz = classObject ? (Class)o : o.getClass();
 
             /*
             * like ASTIdentifier, if we have cache information, and the Class of

Modified: velocity/engine/trunk/velocity-engine-core/src/test/java/org/apache/velocity/test/MethodCacheKeyTestCase.java
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/velocity-engine-core/src/test/java/org/apache/velocity/test/MethodCacheKeyTestCase.java?rev=1872422&r1=1872421&r2=1872422&view=diff
==============================================================================
--- velocity/engine/trunk/velocity-engine-core/src/test/java/org/apache/velocity/test/MethodCacheKeyTestCase.java (original)
+++ velocity/engine/trunk/velocity-engine-core/src/test/java/org/apache/velocity/test/MethodCacheKeyTestCase.java Tue Jan  7 09:37:17 2020
@@ -20,6 +20,7 @@ package org.apache.velocity.test;
  */
 
 import junit.framework.TestCase;
+import org.apache.commons.io.filefilter.FalseFileFilter;
 import org.apache.commons.lang3.ArrayUtils;
 import org.apache.velocity.runtime.parser.node.ASTMethod;
 
@@ -35,44 +36,44 @@ public class MethodCacheKeyTestCase exte
     public void testMethodKeyCacheEquals()
     {
         Class [] elements1 = new Class [] { Object.class };
-        ASTMethod.MethodCacheKey mck1 = new ASTMethod.MethodCacheKey("test",elements1);
+        ASTMethod.MethodCacheKey mck1 = new ASTMethod.MethodCacheKey("test",elements1, false);
 
         selfEqualsAssertions(mck1);
 
         Class [] elements2 = new Class [] { Object.class };
-        ASTMethod.MethodCacheKey mck2 = new ASTMethod.MethodCacheKey("test",elements2);
+        ASTMethod.MethodCacheKey mck2 = new ASTMethod.MethodCacheKey("test",elements2, false);
 
         assertTrue(mck1.equals(mck2));
 
         Class [] elements3 = new Class [] { String.class };
-        ASTMethod.MethodCacheKey mck3 = new ASTMethod.MethodCacheKey("test",elements3);
+        ASTMethod.MethodCacheKey mck3 = new ASTMethod.MethodCacheKey("test",elements3, false);
 
         assertFalse(mck1.equals(mck3));
 
         Class [] elements4 = new Class [] { Object.class };
-        ASTMethod.MethodCacheKey mck4 = new ASTMethod.MethodCacheKey("boo",elements4);
+        ASTMethod.MethodCacheKey mck4 = new ASTMethod.MethodCacheKey("boo",elements4, false);
 
         assertFalse(mck1.equals(mck4));
 
         /** check for potential NPE's **/
         Class [] elements5 = ArrayUtils.EMPTY_CLASS_ARRAY;
-        ASTMethod.MethodCacheKey mck5 = new ASTMethod.MethodCacheKey("boo",elements5);
+        ASTMethod.MethodCacheKey mck5 = new ASTMethod.MethodCacheKey("boo",elements5, false);
         selfEqualsAssertions(mck5);
 
         Class [] elements6 = null;
-        ASTMethod.MethodCacheKey mck6 = new ASTMethod.MethodCacheKey("boo",elements6);
+        ASTMethod.MethodCacheKey mck6 = new ASTMethod.MethodCacheKey("boo",elements6, false);
         selfEqualsAssertions(mck6);
 
         Class [] elements7 = new Class [] {};
-        ASTMethod.MethodCacheKey mck7 = new ASTMethod.MethodCacheKey("boo",elements7);
+        ASTMethod.MethodCacheKey mck7 = new ASTMethod.MethodCacheKey("boo",elements7, false);
         selfEqualsAssertions(mck7);
 
         Class [] elements8 = new Class [] {null};
-        ASTMethod.MethodCacheKey mck8 = new ASTMethod.MethodCacheKey("boo",elements8);
+        ASTMethod.MethodCacheKey mck8 = new ASTMethod.MethodCacheKey("boo",elements8, false);
         selfEqualsAssertions(mck8);
 
         Class [] elements9 = new Class [] { Object.class };
-        ASTMethod.MethodCacheKey mck9 = new ASTMethod.MethodCacheKey("boo",elements9);
+        ASTMethod.MethodCacheKey mck9 = new ASTMethod.MethodCacheKey("boo",elements9, false);
         selfEqualsAssertions(mck9);
 
     }

Modified: velocity/engine/trunk/velocity-engine-core/src/test/java/org/apache/velocity/test/issues/Velocity855TestCase.java
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/velocity-engine-core/src/test/java/org/apache/velocity/test/issues/Velocity855TestCase.java?rev=1872422&r1=1872421&r2=1872422&view=diff
==============================================================================
--- velocity/engine/trunk/velocity-engine-core/src/test/java/org/apache/velocity/test/issues/Velocity855TestCase.java (original)
+++ velocity/engine/trunk/velocity-engine-core/src/test/java/org/apache/velocity/test/issues/Velocity855TestCase.java Tue Jan  7 09:37:17 2020
@@ -39,7 +39,7 @@ public class Velocity855TestCase extends
         context.put("typeKind", javax.lang.model.type.TypeKind.class);
     }
 
-    public void testSpaceBeforeRParen()
+    public void testVelocity855()
     {
         assertEvalEquals("ENUM DECLARED", "$elementKind.valueOf('ENUM') $typeKind.valueOf('DECLARED')");
     }

Added: velocity/engine/trunk/velocity-engine-core/src/test/java/org/apache/velocity/test/issues/Velocity924TestCase.java
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/velocity-engine-core/src/test/java/org/apache/velocity/test/issues/Velocity924TestCase.java?rev=1872422&view=auto
==============================================================================
--- velocity/engine/trunk/velocity-engine-core/src/test/java/org/apache/velocity/test/issues/Velocity924TestCase.java (added)
+++ velocity/engine/trunk/velocity-engine-core/src/test/java/org/apache/velocity/test/issues/Velocity924TestCase.java Tue Jan  7 09:37:17 2020
@@ -0,0 +1,64 @@
+package org.apache.velocity.test.issues;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import org.apache.velocity.VelocityContext;
+import org.apache.velocity.test.BaseTestCase;
+
+/**
+ * This class tests VELOCITY-855.
+ */
+public class Velocity924TestCase extends BaseTestCase
+{
+    public Velocity924TestCase(String name)
+    {
+        super(name);
+    }
+
+    public static class Foo
+    {
+        public String getName() { return "foo"; }
+    }
+
+    protected void setUpContext(VelocityContext context)
+    {
+        context.put("var", new Foo());
+    }
+
+    public void testVelocity924Getter()
+    {
+        assertEvalEquals("org.apache.velocity.test.issues.Velocity924TestCase$Foo foo", "$var.class.name $var.name");
+    }
+
+    public void testVelocity924Method()
+    {
+        assertEvalEquals("org.apache.velocity.test.issues.Velocity924TestCase$Foo foo", "$var.class.getName() $var.getName()");assertEvalEquals("org.apache.velocity.test.issues.Velocity924TestCase$Foo foo", "$var.class.name $var.name");
+    }
+
+    public void testVelocity924Getter2()
+    {
+        assertEvalEquals("foo org.apache.velocity.test.issues.Velocity924TestCase$Foo", "$var.name $var.class.name");
+    }
+
+    public void testVelocity924Method2()
+    {
+        assertEvalEquals("foo org.apache.velocity.test.issues.Velocity924TestCase$Foo", "$var.getName() $var.class.getName()");assertEvalEquals("org.apache.velocity.test.issues.Velocity924TestCase$Foo foo", "$var.class.name $var.name");
+    }
+}

Propchange: velocity/engine/trunk/velocity-engine-core/src/test/java/org/apache/velocity/test/issues/Velocity924TestCase.java
------------------------------------------------------------------------------
    svn:executable = *