This is an automated email from the ASF dual-hosted git repository. emilles pushed a commit to branch GROOVY_4_0_X in repository https://gitbox.apache.org/repos/asf/groovy.git
The following commit(s) were added to refs/heads/GROOVY_4_0_X by this push: new cb0e362374 GROOVY-11362: classgen: catch parameter is `Exception` for `catch (e) {` cb0e362374 is described below commit cb0e36237445c6b2d626ccdea212d4e64b98983c Author: Eric Milles <eric.mil...@thomsonreuters.com> AuthorDate: Sun Apr 21 14:18:36 2024 -0500 GROOVY-11362: classgen: catch parameter is `Exception` for `catch (e) {` --- .../groovy/classgen/asm/StatementWriter.java | 8 ++--- src/test/groovy/bugs/Groovy11362.groovy | 39 ++++++++++++++++++++++ 2 files changed, 43 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/codehaus/groovy/classgen/asm/StatementWriter.java b/src/main/java/org/codehaus/groovy/classgen/asm/StatementWriter.java index c33debbc95..d6f0bbde9e 100644 --- a/src/main/java/org/codehaus/groovy/classgen/asm/StatementWriter.java +++ b/src/main/java/org/codehaus/groovy/classgen/asm/StatementWriter.java @@ -350,8 +350,8 @@ public class StatementWriter { // create variable for the exception compileStack.pushState(); - // TODO: Is it okay that "catch (e)" makes variable type Object? - compileStack.defineVariable(catchStatement.getVariable(), true); + ClassNode type = catchStatement.getExceptionType(); + compileStack.defineVariable(catchStatement.getVariable(), type, true); // handle catch body catchStatement.visit(controller.getAcg()); // placeholder to avoid problems with empty catch block @@ -365,8 +365,8 @@ public class StatementWriter { mv.visitJumpInsn(GOTO, finallyStart); fallthroughFinally = true; } - compileStack.writeExceptionTable(tryBlock, catchBlock, - BytecodeHelper.getClassInternalName(catchStatement.getExceptionType())); + String typeName = BytecodeHelper.getClassInternalName(type); + compileStack.writeExceptionTable(tryBlock, catchBlock, typeName); } // used to handle exceptions in catches and regularly visited finals diff --git a/src/test/groovy/bugs/Groovy11362.groovy b/src/test/groovy/bugs/Groovy11362.groovy new file mode 100644 index 0000000000..d8c96a48da --- /dev/null +++ b/src/test/groovy/bugs/Groovy11362.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 + +import org.codehaus.groovy.classgen.asm.AbstractBytecodeTestCase + +final class Groovy11362 extends AbstractBytecodeTestCase { + + void testCatchException() { + def bytecode = compile method:'test', ''' + void test() { + try { + print 'f' + } catch (e) { + } + } + ''' + assert bytecode.hasSequence([ + 'LOCALVARIABLE this Lscript; L0 L6 0', + 'LOCALVARIABLE e Ljava/lang/Exception; L5 L3 1' // not Ljava/lang/Object; + ]) + } +}