[groovy] branch master 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 master 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 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 b02dbbb  GROOVY-9469: Preserve exception in global AST transformation creation (closes #1193)
b02dbbb is described below

commit b02dbbb0c23eaa408064127d59a8ae3dc4ebc9ed
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 016c0fd..ee9bebc 100644
--- a/src/main/java/org/codehaus/groovy/transform/ASTTransformationVisitor.java
+++ b/src/main/java/org/codehaus/groovy/transform/ASTTransformationVisitor.java
@@ -345,9 +345,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 55be8d9..c04c371 100644
--- a/src/test/org/codehaus/groovy/transform/classloading/TransformsAndCustomClassLoadersTest.groovy
+++ b/src/test/org/codehaus/groovy/transform/classloading/TransformsAndCustomClassLoadersTest.groovy
@@ -24,6 +24,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
@@ -37,6 +38,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,
@@ -90,6 +92,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)
@@ -161,3 +175,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) {
+    }
+}