Learn Java for Web Development

(Tina Meador) #1

408 APPENDIX B: Introduction to Groovy


   Line 4 has an expression, $name, that is evaluated to vishal, as shown in
the output.
 Line 5 has an expression, $path, that is evaluated to c:/groovy, as shown in
the output.
 Line 6 has a $ sign, but it is not an expression, so it is displayed in the output.
 Line 7 has a slash, which needs to be escaped.

Dollar Slashy Strings

In multiline slashy strings, a slash still needs to be escaped. Moreover, in multiline slashy strings, an
unescaped dollar sign that is not an expression results in a MissingPropertyException, as illustrated
in Listing B-14.


Listing B-14. MissingPropertyException in Multiline Slashy String



  1. def name = "vishal"

  2. def path= "c:/groovy"

  3. def multilineSlashy = /

  4. Hello $name

  5. path= $path

  6. dollar = $test

  7. path = c:\/groovy

  8. /

  9. println multilineSlashy


Caught: groovy.lang.MissingPropertyException: No such property: test for class:
hello
groovy.lang.MissingPropertyException: No such property: test for class: hello
at hello.run(hello.groovy:3)


In Listing B-14, there is no such property as test; $test in line 6 is interpreted as an expression,
which results in a MissingPropertyException.


Now, let’s look at the code in Listing B-15, specifically line 6.


Listing B-15. Unescaped Dollar Sign in Multiline Slashy String



  1. def name = "vishal"

  2. def path= "c:/groovy"

  3. def multilineSlashy = /

  4. Hello $name

  5. path= $path

  6. dollar = $ test

  7. path = c:\/groovy

  8. /

  9. println multilineSlashy

Free download pdf