You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by pa...@apache.org on 2016/08/31 02:42:27 UTC

groovy git commit: GROOVY-7921: using "this" as the target of an assignment not working with Category annotation (closes #400)

Repository: groovy
Updated Branches:
  refs/heads/master e0b36ebc9 -> 2be3fe859


GROOVY-7921: using "this" as the target of an assignment not working with Category annotation (closes #400)


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

Branch: refs/heads/master
Commit: 2be3fe8599e2535873f3c181f53eeee255df5c3d
Parents: e0b36eb
Author: paulk <pa...@asert.com.au>
Authored: Sun Aug 28 19:21:37 2016 +1000
Committer: paulk <pa...@asert.com.au>
Committed: Wed Aug 31 12:41:40 2016 +1000

----------------------------------------------------------------------
 .../groovy/classgen/VariableScopeVisitor.java   | 10 +++---
 src/test/groovy/bugs/Groovy7921Bug.groovy       | 36 ++++++++++++++++++++
 2 files changed, 42 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/2be3fe85/src/main/org/codehaus/groovy/classgen/VariableScopeVisitor.java
----------------------------------------------------------------------
diff --git a/src/main/org/codehaus/groovy/classgen/VariableScopeVisitor.java b/src/main/org/codehaus/groovy/classgen/VariableScopeVisitor.java
index 34bc352..9085eb7 100644
--- a/src/main/org/codehaus/groovy/classgen/VariableScopeVisitor.java
+++ b/src/main/org/codehaus/groovy/classgen/VariableScopeVisitor.java
@@ -385,10 +385,12 @@ public class VariableScopeVisitor extends ClassCodeVisitorSupport {
     // TODO handle local variables
     private void checkForFinal(final Expression expression, VariableExpression ve) {
         Variable v = ve.getAccessedVariable();
-        boolean isFinal = isFinal(v.getModifiers());
-        boolean isParameter = v instanceof Parameter;
-        if (isFinal && isParameter) {
-            addError("Cannot assign a value to final variable '" + v.getName() + "'", expression);
+        if (v != null) {
+            boolean isFinal = isFinal(v.getModifiers());
+            boolean isParameter = v instanceof Parameter;
+            if (isFinal && isParameter) {
+                addError("Cannot assign a value to final variable '" + v.getName() + "'", expression);
+            }
         }
     }
 

http://git-wip-us.apache.org/repos/asf/groovy/blob/2be3fe85/src/test/groovy/bugs/Groovy7921Bug.groovy
----------------------------------------------------------------------
diff --git a/src/test/groovy/bugs/Groovy7921Bug.groovy b/src/test/groovy/bugs/Groovy7921Bug.groovy
new file mode 100644
index 0000000..5f68ba7
--- /dev/null
+++ b/src/test/groovy/bugs/Groovy7921Bug.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 Groovy7921Bug extends GroovyTestCase {
+    void testShouldBeAbleToAssignThisInsideCategoryAnnotatedClass() {
+        assertScript '''
+            @Category(Integer)
+            class IntegerEx {
+              def reset(Integer value) {
+                this = value // this will be transformed to $this
+                this
+              }
+            }
+            use(IntegerEx){
+              assert 3.reset(1) == 1
+            }
+        '''
+    }
+}