Repository: groovy Updated Branches: refs/heads/parrot a033231e2 -> 5392eb4fa
GROOVY-8060: @Log annotation does not check logging enablement inside closures which are arguments to methods Project: http://git-wip-us.apache.org/repos/asf/groovy/repo Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/e96a9619 Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/e96a9619 Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/e96a9619 Branch: refs/heads/parrot Commit: e96a9619ca3e51d867290056194fe5259c55c084 Parents: 90fe6d2 Author: paulk <pa...@asert.com.au> Authored: Mon Jan 23 21:42:52 2017 +1000 Committer: paulk <pa...@asert.com.au> Committed: Mon Jan 23 21:42:52 2017 +1000 ---------------------------------------------------------------------- .../groovy/transform/LogASTTransformation.java | 24 +++++++----- src/test/groovy/bugs/Groovy8060Bug.groovy | 39 ++++++++++++++++++++ 2 files changed, 54 insertions(+), 9 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/groovy/blob/e96a9619/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 42fb9a3..ecf92bc 100644 --- a/src/main/org/codehaus/groovy/transform/LogASTTransformation.java +++ b/src/main/org/codehaus/groovy/transform/LogASTTransformation.java @@ -129,24 +129,30 @@ public class LogASTTransformation extends AbstractASTTransformation implements C } private Expression transformMethodCallExpression(Expression exp) { - MethodCallExpression mce = (MethodCallExpression) exp; + Expression modifiedCall = addGuard((MethodCallExpression) exp); + return modifiedCall == null ? super.transform(exp) : modifiedCall; + } + + private Expression addGuard(MethodCallExpression mce) { + // only add guard to methods of the form: logVar.logMethod(params) if (!(mce.getObjectExpression() instanceof VariableExpression)) { - return exp; + return null; } VariableExpression variableExpression = (VariableExpression) mce.getObjectExpression(); if (!variableExpression.getName().equals(logFieldName) || !(variableExpression.getAccessedVariable() instanceof DynamicVariable)) { - return exp; + return null; } + String methodName = mce.getMethodAsString(); - if (methodName == null) return exp; - if (usesSimpleMethodArgumentsOnly(mce)) return exp; + if (methodName == null) return null; + if (!loggingStrategy.isLoggingMethod(methodName)) return null; + // also don't bother with guard if we have "simple" method args + // since there is no saving + if (usesSimpleMethodArgumentsOnly(mce)) return null; variableExpression.setAccessedVariable(logNode); - - if (!loggingStrategy.isLoggingMethod(methodName)) return exp; - - return loggingStrategy.wrapLoggingMethodCall(variableExpression, methodName, exp); + return loggingStrategy.wrapLoggingMethodCall(variableExpression, methodName, mce); } private boolean usesSimpleMethodArgumentsOnly(MethodCallExpression mce) { http://git-wip-us.apache.org/repos/asf/groovy/blob/e96a9619/src/test/groovy/bugs/Groovy8060Bug.groovy ---------------------------------------------------------------------- diff --git a/src/test/groovy/bugs/Groovy8060Bug.groovy b/src/test/groovy/bugs/Groovy8060Bug.groovy new file mode 100644 index 0000000..741929b --- /dev/null +++ b/src/test/groovy/bugs/Groovy8060Bug.groovy @@ -0,0 +1,39 @@ +/* + * 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 Groovy8060Bug extends GroovyTestCase { + void testLoggingWithinClosuresThatAreMethodArgsShouldHaveGuards() { + assertScript ''' + @Grab('org.slf4j:slf4j-simple:1.7.21') + import groovy.util.logging.Slf4j + + @Slf4j + class LogMain { + public static int count = 0 + + static void main(args) { + assert !log.isTraceEnabled() + 1.times { log.trace("${count++}") } + assert !count + } + } + ''' + } +}