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/28 20:44:16 UTC

groovy git commit: GROOVY-6932: @Log annotation does not check logging enablement inside closures (closes #401)

Repository: groovy
Updated Branches:
  refs/heads/master 86eff312d -> 0480d3995


GROOVY-6932: @Log annotation does not check logging enablement inside closures (closes #401)


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

Branch: refs/heads/master
Commit: 0480d3995be01254b1b4cb19077d3bc93f0607fc
Parents: 86eff31
Author: paulk <pa...@asert.com.au>
Authored: Sun Aug 28 22:33:49 2016 +1000
Committer: paulk <pa...@asert.com.au>
Committed: Mon Aug 29 06:43:13 2016 +1000

----------------------------------------------------------------------
 .../groovy/transform/LogASTTransformation.java  | 13 +++++
 src/test/groovy/bugs/Groovy6932Bug.groovy       | 50 ++++++++++++++++++++
 2 files changed, 63 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/0480d399/src/main/org/codehaus/groovy/transform/LogASTTransformation.java
----------------------------------------------------------------------
diff --git a/src/main/org/codehaus/groovy/transform/LogASTTransformation.java b/src/main/org/codehaus/groovy/transform/LogASTTransformation.java
index 7643ebe..42fb9a3 100644
--- a/src/main/org/codehaus/groovy/transform/LogASTTransformation.java
+++ b/src/main/org/codehaus/groovy/transform/LogASTTransformation.java
@@ -30,11 +30,13 @@ import org.codehaus.groovy.ast.ClassHelper;
 import org.codehaus.groovy.ast.ClassNode;
 import org.codehaus.groovy.ast.DynamicVariable;
 import org.codehaus.groovy.ast.FieldNode;
+import org.codehaus.groovy.ast.expr.ClosureExpression;
 import org.codehaus.groovy.ast.expr.ConstantExpression;
 import org.codehaus.groovy.ast.expr.Expression;
 import org.codehaus.groovy.ast.expr.MethodCallExpression;
 import org.codehaus.groovy.ast.expr.TupleExpression;
 import org.codehaus.groovy.ast.expr.VariableExpression;
+import org.codehaus.groovy.ast.stmt.BlockStatement;
 import org.codehaus.groovy.classgen.VariableScopeVisitor;
 import org.codehaus.groovy.control.CompilationUnit;
 import org.codehaus.groovy.control.CompilePhase;
@@ -99,6 +101,9 @@ public class LogASTTransformation extends AbstractASTTransformation implements C
                 if (exp instanceof MethodCallExpression) {
                     return transformMethodCallExpression(exp);
                 }
+                if (exp instanceof ClosureExpression) {
+                    return transformClosureExpression((ClosureExpression) exp);
+                }
                 return super.transform(exp);
             }
 
@@ -115,6 +120,14 @@ public class LogASTTransformation extends AbstractASTTransformation implements C
                 super.visitClass(node);
             }
 
+            private Expression transformClosureExpression(ClosureExpression exp) {
+                if (exp.getCode() instanceof BlockStatement) {
+                    BlockStatement code = (BlockStatement) exp.getCode();
+                    super.visitBlockStatement(code);
+                }
+                return exp;
+            }
+
             private Expression transformMethodCallExpression(Expression exp) {
                 MethodCallExpression mce = (MethodCallExpression) exp;
                 if (!(mce.getObjectExpression() instanceof VariableExpression)) {

http://git-wip-us.apache.org/repos/asf/groovy/blob/0480d399/src/test/groovy/bugs/Groovy6932Bug.groovy
----------------------------------------------------------------------
diff --git a/src/test/groovy/bugs/Groovy6932Bug.groovy b/src/test/groovy/bugs/Groovy6932Bug.groovy
new file mode 100644
index 0000000..d392a02
--- /dev/null
+++ b/src/test/groovy/bugs/Groovy6932Bug.groovy
@@ -0,0 +1,50 @@
+/*
+ *  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 Groovy6932Bug extends GroovyTestCase {
+    void testLoggingWithinClosuresShouldHaveGuards() {
+        assertScript '''
+            @Grab('org.slf4j:slf4j-simple:1.7.21')
+            import groovy.util.logging.Slf4j
+
+            new TestCode().doSomethingThatLogs()
+
+            @Slf4j
+            class TestCode {
+                void doSomethingThatLogs(){
+                    int info = 0
+                    int trace = 0
+                    log.info createLogString("${info++}")
+                    log.trace createLogString("${trace++}")
+                    Closure c1 = { log.info createLogString("${info++}") }
+                    c1()
+                    Closure c2 = { log.trace createLogString("${trace++}") }
+                    c2()
+                    assert info == 2
+                    assert trace == 0
+                }
+
+                String createLogString(def p){
+                    "called with $p"
+                }
+            }
+        '''
+    }
+}