groovy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pa...@apache.org
Subject incubator-groovy git commit: GROOVY-7645: Stub generator unescapes escaped backslashes in String annotation values (closes #168)
Date Sat, 31 Oct 2015 00:15:19 GMT
Repository: incubator-groovy
Updated Branches:
  refs/heads/GROOVY_2_4_X d17628a75 -> dbce93026


GROOVY-7645: Stub generator unescapes escaped backslashes in String annotation values (closes
#168)


Project: http://git-wip-us.apache.org/repos/asf/incubator-groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-groovy/commit/dbce9302
Tree: http://git-wip-us.apache.org/repos/asf/incubator-groovy/tree/dbce9302
Diff: http://git-wip-us.apache.org/repos/asf/incubator-groovy/diff/dbce9302

Branch: refs/heads/GROOVY_2_4_X
Commit: dbce93026197c217d1987e38d59bd68a0fc2c378
Parents: d17628a
Author: paulk <paulk@asert.com.au>
Authored: Fri Oct 30 19:20:11 2015 +1000
Committer: paulk <paulk@asert.com.au>
Committed: Sat Oct 31 10:15:05 2015 +1000

----------------------------------------------------------------------
 .../codehaus/groovy/runtime/InvokerHelper.java  | 18 ++++++++-----
 .../groovy/tools/javac/JavaStubGenerator.java   |  4 ++-
 .../EscapingOfStringAnnotationValuesTest.groovy | 27 ++++++++++++--------
 3 files changed, 30 insertions(+), 19 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-groovy/blob/dbce9302/src/main/org/codehaus/groovy/runtime/InvokerHelper.java
----------------------------------------------------------------------
diff --git a/src/main/org/codehaus/groovy/runtime/InvokerHelper.java b/src/main/org/codehaus/groovy/runtime/InvokerHelper.java
index 21b4509..bf8bbc1 100644
--- a/src/main/org/codehaus/groovy/runtime/InvokerHelper.java
+++ b/src/main/org/codehaus/groovy/runtime/InvokerHelper.java
@@ -616,13 +616,7 @@ public class InvokerHelper {
         }
         if (arguments instanceof String) {
             if (verbose) {
-                String arg = ((String) arguments)
-                        // must replace backslashes first, as the other replacements add
backslashes not to be escaped
-                        .replace("\\", "\\\\")      // backslash
-                        .replace("\n", "\\n")    // line feed
-                        .replaceAll("\\r", "\\\\r")      // carriage return
-                        .replaceAll("\\t", "\\\\t")      // tab
-                        .replaceAll("\\f", "\\\\f")      // form feed
+                String arg = escapeBackslashes((String) arguments)
                         .replaceAll("'", "\\\\'");    // single quotation mark
                 return "\'" + arg + "\'";
             } else {
@@ -634,6 +628,16 @@ public class InvokerHelper {
         return arguments.toString();
     }
 
+    public static String escapeBackslashes(String orig) {
+        // must replace backslashes first, as the other replacements add backslashes not
to be escaped
+        return orig
+                .replace("\\", "\\\\")           // backslash
+                .replace("\n", "\\n")            // line feed
+                .replaceAll("\\r", "\\\\r")      // carriage return
+                .replaceAll("\\t", "\\\\t")      // tab
+                .replaceAll("\\f", "\\\\f");     // form feed
+    }
+
     private static String formatMap(Map map, boolean verbose, int maxSize) {
         if (map.isEmpty()) {
             return "[:]";

http://git-wip-us.apache.org/repos/asf/incubator-groovy/blob/dbce9302/src/main/org/codehaus/groovy/tools/javac/JavaStubGenerator.java
----------------------------------------------------------------------
diff --git a/src/main/org/codehaus/groovy/tools/javac/JavaStubGenerator.java b/src/main/org/codehaus/groovy/tools/javac/JavaStubGenerator.java
index 358326a..6c53ea8 100644
--- a/src/main/org/codehaus/groovy/tools/javac/JavaStubGenerator.java
+++ b/src/main/org/codehaus/groovy/tools/javac/JavaStubGenerator.java
@@ -33,6 +33,7 @@ import org.codehaus.groovy.ast.stmt.ExpressionStatement;
 import org.codehaus.groovy.ast.stmt.Statement;
 import org.codehaus.groovy.classgen.Verifier;
 import org.codehaus.groovy.control.ResolveVisitor;
+import org.codehaus.groovy.runtime.InvokerHelper;
 import org.codehaus.groovy.tools.Utilities;
 import org.codehaus.groovy.transform.trait.Traits;
 import org.objectweb.asm.Opcodes;
@@ -952,7 +953,8 @@ public class JavaStubGenerator {
     }
 
     private static String escapeSpecialChars(String value) {
-        return value.replace("\n", "\\n").replace("\r", "\\r").replace("\"", "\\\"");
+        return InvokerHelper.escapeBackslashes(value).replace("\"", "\\\"");
+
     }
 
     private static boolean isInterfaceOrTrait(ClassNode cn) {

http://git-wip-us.apache.org/repos/asf/incubator-groovy/blob/dbce9302/src/test/org/codehaus/groovy/tools/stubgenerator/EscapingOfStringAnnotationValuesTest.groovy
----------------------------------------------------------------------
diff --git a/src/test/org/codehaus/groovy/tools/stubgenerator/EscapingOfStringAnnotationValuesTest.groovy
b/src/test/org/codehaus/groovy/tools/stubgenerator/EscapingOfStringAnnotationValuesTest.groovy
index a06dd50..7f70435 100644
--- a/src/test/org/codehaus/groovy/tools/stubgenerator/EscapingOfStringAnnotationValuesTest.groovy
+++ b/src/test/org/codehaus/groovy/tools/stubgenerator/EscapingOfStringAnnotationValuesTest.groovy
@@ -19,10 +19,7 @@
 package org.codehaus.groovy.tools.stubgenerator
 
 /**
- * Test case for GROOVY-4470, GROOVY-4604 and GROOVY-4601
- *
- * @author Peter Niederwieser
- * @author Guillaume Laforge
+ * Test case for GROOVY-4470, GROOVY-4604, GROOVY-4601, GROOVY-7645
  */
 class EscapingOfStringAnnotationValuesTest extends StringSourcesStubTestCase {
 
@@ -36,27 +33,35 @@ class EscapingOfStringAnnotationValuesTest extends StringSourcesStubTestCase
{
                     }
                 ''',
 
-                'StringAnnUsage.groovy': '''
+                'StringAnnUsage1.groovy': '''
                     |@StringAnn(str = """Now that's what
                     |I
                     |\tcall an
                     |  "unescaped"
                     |String!""")
-                    |class StringAnnUsage {}
+                    |class StringAnnUsage1 {}
                 '''.stripMargin('|'),
 
-                'StringAnnoUser.groovy': '''
+                'StringAnnUsage2.groovy': '''
                     @StringAnn(str = 'single quote string with "double quote string"')
-                    class StringAnnoUser {}
+                    class StringAnnUsage2 {}
+                ''',
+
+                'StringAnnUsage3.groovy': '''
+                    @StringAnn(str = '{id:[^.]\\\\.[^.]}')
+                    class StringAnnUsage3 {}
                 '''
         ]
     }
 
     void verifyStubs() {
-        def ann1 = classes['StringAnnUsage'].annotations[0]
-        assert "\"Now that's what\\nI\\n\tcall an\\n  \\\"unescaped\\\"\\nString!\"" == ann1.getNamedParameter("str")
+        def ann1 = classes['StringAnnUsage1'].annotations[0]
+        assert "\"Now that's what\\nI\\n\\tcall an\\n  \\\"unescaped\\\"\\nString!\"" ==
ann1.getNamedParameter("str")
 
-        def ann2 = classes['StringAnnoUser'].annotations[0]
+        def ann2 = classes['StringAnnUsage2'].annotations[0]
         assert '"single quote string with \\"double quote string\\""' == ann2.getNamedParameter("str")
+
+        def ann3 = classes['StringAnnUsage3'].annotations[0]
+        assert '"{id:[^.]\\\\.[^.]}"' == ann3.getNamedParameter("str")
     }
 }
\ No newline at end of file


Mime
View raw message