[groovy] branch GROOVY_2_5_X updated: GROOVY-9469: Preserve exception in global AST transformation creation (closes #1193)

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

[groovy] branch GROOVY_2_5_X updated: GROOVY-9469: Preserve exception in global AST transformation creation (closes #1193)

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

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


The following commit(s) were added to refs/heads/GROOVY_2_5_X by this push:
     new 6463991  GROOVY-9469: Preserve exception in global AST transformation creation (closes #1193)
6463991 is described below

commit 6463991563ebc35aa2eb366ba22122b9eec1bea8
Author: Marcin ZajÄ…czkowski <[hidden email]>
AuthorDate: Sun Mar 15 17:53:33 2020 +0100

    GROOVY-9469: Preserve exception in global AST transformation creation (closes #1193)
   
    InvocationTargetException is not very meaningful. It worked up until Groovy 2.5.7.
---
 .../groovy/transform/ASTTransformationVisitor.java |  3 ++-
 .../TransformsAndCustomClassLoadersTest.groovy     | 24 ++++++++++++++++++++++
 2 files changed, 26 insertions(+), 1 deletion(-)

diff --git a/src/main/java/org/codehaus/groovy/transform/ASTTransformationVisitor.java b/src/main/java/org/codehaus/groovy/transform/ASTTransformationVisitor.java
index 950ca9d..bca1c80 100644
--- a/src/main/java/org/codehaus/groovy/transform/ASTTransformationVisitor.java
+++ b/src/main/java/org/codehaus/groovy/transform/ASTTransformationVisitor.java
@@ -369,9 +369,10 @@ public final class ASTTransformationVisitor extends ClassCodeVisitorSupport {
                         + entry.getValue().toExternalForm() + " is not an ASTTransformation.", null));
                 }
             } catch (Exception e) {
+                Throwable effectiveException = e instanceof InvocationTargetException ? e.getCause() : e;
                 compilationUnit.getErrorCollector().addError(new SimpleMessage(
                     "Could not instantiate global transform class " + entry.getKey() + " specified at "
-                    + entry.getValue().toExternalForm() + "  because of exception " + e.toString(), null));
+                    + entry.getValue().toExternalForm() + "  because of exception " + effectiveException.toString(), null));
             }
         }
     }
diff --git a/src/test/org/codehaus/groovy/transform/classloading/TransformsAndCustomClassLoadersTest.groovy b/src/test/org/codehaus/groovy/transform/classloading/TransformsAndCustomClassLoadersTest.groovy
index 456f8ab..ea4adbe 100644
--- a/src/test/org/codehaus/groovy/transform/classloading/TransformsAndCustomClassLoadersTest.groovy
+++ b/src/test/org/codehaus/groovy/transform/classloading/TransformsAndCustomClassLoadersTest.groovy
@@ -23,6 +23,7 @@ import org.codehaus.groovy.ast.ASTNode
 import org.codehaus.groovy.ast.ModuleNode
 import org.codehaus.groovy.ast.ClassNode
 import org.codehaus.groovy.control.CompilationUnit
+import org.codehaus.groovy.control.MultipleCompilationErrorsException
 import org.codehaus.groovy.control.SourceUnit
 import org.codehaus.groovy.control.CompilePhase
 import org.codehaus.groovy.transform.GroovyASTTransformationClass
@@ -35,6 +36,7 @@ import java.lang.annotation.Retention
 import java.lang.annotation.RetentionPolicy
 import java.lang.annotation.Target
 import java.lang.annotation.ElementType
+import java.nio.file.FileSystemNotFoundException
 
 /**
  * Tests whether local and global transforms are successfully detected, loaded,
@@ -88,6 +90,18 @@ class TransformsAndCustomClassLoadersTest extends GroovyTestCase {
         assert clazz.name == "FOO"
     }
 
+    void testShouldKeepOriginalExceptionDuringGlobalTransformApplyingGroovy9469Bug() {
+        try {
+            transformLoader = new GlobalTestTransformClassLoader(transformLoader, FailingWithMeaningfulMessageTransformation)
+            compileAndLoadClass("class Foo {}", dependencyLoader, transformLoader)
+            fail("Excepted MultipleCompilationErrorsException not thrown")
+        } catch(MultipleCompilationErrorsException e) {
+            assert e.message.contains("FailingWithMeaningfulMessageTransformation")
+            assert e.message.contains("FileSystemNotFoundException")
+            assert e.message.contains("meaningful error message")
+        }
+    }
+
     private compileAndLoadClass(String source, GroovyClassLoader dependencyLoader, GroovyClassLoader transformLoader) {
         def unit = new CompilationUnit(null, null, dependencyLoader, transformLoader)
         unit.addSource("Foo.groovy", source)
@@ -158,3 +172,13 @@ class ToUpperCaseGlobalTransform implements ASTTransformation {
         }
     }
 }
+
+@GroovyASTTransformation(phase = CompilePhase.SEMANTIC_ANALYSIS)
+class FailingWithMeaningfulMessageTransformation implements ASTTransformation {
+    FailingWithMeaningfulMessageTransformation() {
+        throw new FileSystemNotFoundException("Custom exception with meaningful error message")
+    }
+    @Override
+    void visit(ASTNode[] nodes, SourceUnit source) {
+    }
+}