groovy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pa...@apache.org
Subject [1/2] groovy git commit: GROOVY-7917: Sub class can't override final static property (closes #394)
Date Thu, 25 Aug 2016 23:55:19 GMT
Repository: groovy
Updated Branches:
  refs/heads/GROOVY_2_4_X fe2a2d655 -> 24f432860


GROOVY-7917: Sub class can't override final static property (closes #394)


Project: http://git-wip-us.apache.org/repos/asf/groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/9696abea
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/9696abea
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/9696abea

Branch: refs/heads/GROOVY_2_4_X
Commit: 9696abeae11d6eaa9e2c629861b6b6e52785288e
Parents: fe2a2d6
Author: paulk <paulk@asert.com.au>
Authored: Thu Aug 25 10:47:47 2016 +1000
Committer: paulk <paulk@asert.com.au>
Committed: Thu Aug 25 16:46:24 2016 +1000

----------------------------------------------------------------------
 src/main/org/codehaus/groovy/ast/ClassNode.java | 10 ++++--
 .../org/codehaus/groovy/classgen/Verifier.java  | 15 +++++---
 src/test/groovy/bugs/Groovy7917Bug.groovy       | 36 ++++++++++++++++++++
 3 files changed, 54 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/9696abea/src/main/org/codehaus/groovy/ast/ClassNode.java
----------------------------------------------------------------------
diff --git a/src/main/org/codehaus/groovy/ast/ClassNode.java b/src/main/org/codehaus/groovy/ast/ClassNode.java
index 12994ed..8c53441 100644
--- a/src/main/org/codehaus/groovy/ast/ClassNode.java
+++ b/src/main/org/codehaus/groovy/ast/ClassNode.java
@@ -1078,6 +1078,10 @@ public class ClassNode extends AnnotatedNode implements Opcodes {
     }
 
     public MethodNode getGetterMethod(String getterName) {
+        return getGetterMethod(getterName, true);
+    }
+
+    public MethodNode getGetterMethod(String getterName, boolean searchSuperClasses) {
         MethodNode getterMethod = null;
         for (MethodNode method : getDeclaredMethods(getterName)) {
             if (getterName.equals(method.getName())
@@ -1093,8 +1097,10 @@ public class ClassNode extends AnnotatedNode implements Opcodes {
             }
         }
         if (getterMethod != null) return getterMethod;
-        ClassNode parent = getSuperClass();
-        if (parent!=null) return parent.getGetterMethod(getterName);
+        if (searchSuperClasses) {
+            ClassNode parent = getSuperClass();
+            if (parent != null) return parent.getGetterMethod(getterName);
+        }
         return null;
     }
 

http://git-wip-us.apache.org/repos/asf/groovy/blob/9696abea/src/main/org/codehaus/groovy/classgen/Verifier.java
----------------------------------------------------------------------
diff --git a/src/main/org/codehaus/groovy/classgen/Verifier.java b/src/main/org/codehaus/groovy/classgen/Verifier.java
index 067cbfc..c3d01e6 100644
--- a/src/main/org/codehaus/groovy/classgen/Verifier.java
+++ b/src/main/org/codehaus/groovy/classgen/Verifier.java
@@ -610,15 +610,15 @@ public class Verifier implements GroovyClassVisitor, Opcodes {
 
         // GROOVY-3726: clear volatile, transient modifiers so that they don't get applied
to methods
         if ((propNodeModifiers & Modifier.VOLATILE) != 0) {
-            propNodeModifiers = propNodeModifiers - Modifier.VOLATILE;
+            propNodeModifiers -= Modifier.VOLATILE;
         }
         if ((propNodeModifiers & Modifier.TRANSIENT) != 0) {
-            propNodeModifiers = propNodeModifiers - Modifier.TRANSIENT;
+            propNodeModifiers -= Modifier.TRANSIENT;
         }
 
         Statement getterBlock = node.getGetterBlock();
         if (getterBlock == null) {
-            MethodNode getter = classNode.getGetterMethod(getterName);
+            MethodNode getter = classNode.getGetterMethod(getterName, !node.isStatic());
             if (getter == null && ClassHelper.boolean_TYPE == node.getType()) {
                 String secondGetterName = "is" + capitalize(name);
                 getter = classNode.getGetterMethod(secondGetterName);
@@ -638,9 +638,14 @@ public class Verifier implements GroovyClassVisitor, Opcodes {
             }
         }
 
+        int getterModifiers = propNodeModifiers;
+        // don't make static accessors final
+        if (node.isStatic() && (propNodeModifiers & Modifier.FINAL) != 0) {
+            getterModifiers -= Modifier.FINAL;
+        }
         if (getterBlock != null) {
             MethodNode getter =
-                    new MethodNode(getterName, propNodeModifiers, node.getType(), Parameter.EMPTY_ARRAY,
ClassNode.EMPTY_ARRAY, getterBlock);
+                    new MethodNode(getterName, getterModifiers, node.getType(), Parameter.EMPTY_ARRAY,
ClassNode.EMPTY_ARRAY, getterBlock);
             getter.setSynthetic(true);
             addPropertyMethod(getter);
             visitMethod(getter);
@@ -648,7 +653,7 @@ public class Verifier implements GroovyClassVisitor, Opcodes {
             if (ClassHelper.boolean_TYPE == node.getType() || ClassHelper.Boolean_TYPE ==
node.getType()) {
                 String secondGetterName = "is" + capitalize(name);
                 MethodNode secondGetter =
-                        new MethodNode(secondGetterName, propNodeModifiers, node.getType(),
Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, getterBlock);
+                        new MethodNode(secondGetterName, getterModifiers, node.getType(),
Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, getterBlock);
                 secondGetter.setSynthetic(true);
                 addPropertyMethod(secondGetter);
                 visitMethod(secondGetter);

http://git-wip-us.apache.org/repos/asf/groovy/blob/9696abea/src/test/groovy/bugs/Groovy7917Bug.groovy
----------------------------------------------------------------------
diff --git a/src/test/groovy/bugs/Groovy7917Bug.groovy b/src/test/groovy/bugs/Groovy7917Bug.groovy
new file mode 100644
index 0000000..7016cd3
--- /dev/null
+++ b/src/test/groovy/bugs/Groovy7917Bug.groovy
@@ -0,0 +1,36 @@
+/*
+ *  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.
+ */
+package groovy.bugs
+
+class Groovy7917Bug extends GroovyTestCase {
+    void testShouldBeAbleToOverrideStaticConstantProperties() {
+        assertScript '''
+            class Base {
+                static final String CONST = "Base Class"
+            }
+
+            class Derived extends Base {
+                static final String CONST = "Derived Class"
+            }
+
+            assert Base.CONST == 'Base Class'
+            assert Derived.CONST == 'Derived Class'
+        '''
+    }
+}


Mime
View raw message