You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by em...@apache.org on 2024/04/21 19:31:13 UTC

(groovy) branch master updated: GROOVY-11362: classgen: catch parameter is `Exception` for `catch (e) {`

This is an automated email from the ASF dual-hosted git repository.

emilles pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/groovy.git


The following commit(s) were added to refs/heads/master by this push:
     new 5bd57e9713 GROOVY-11362: classgen: catch parameter is `Exception` for `catch (e) {`
5bd57e9713 is described below

commit 5bd57e9713d6dc89dcefcba9fcd7e03f4d36f785
Author: Eric Milles <er...@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 c86b8272be..1e41fc9ac8 100644
--- a/src/main/java/org/codehaus/groovy/classgen/asm/StatementWriter.java
+++ b/src/main/java/org/codehaus/groovy/classgen/asm/StatementWriter.java
@@ -343,8 +343,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
@@ -358,8 +358,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;
+        ])
+    }
+}