Groovyのレシピ本
「ねーねーGroovy逆引きレシピって何色?」だと!?
@masanobuimai さんより
ネタにマジレスですんません。レシピ本はすでにありますよ(念のため。
http://pragprog.com/book/sdgrvr/groovy-recipes
レシピ本ってあったんだ。
Groovy Recipes: Greasing the Wheels of Java
目次
1 Preface 12 2 Introduction 14 2.1 Groovy, the Way Java Should Be . . . . . . . . . . . . . . 16 2.2 Stripping Away the Verbosity . . . . . . . . . . . . . . . . 18 2.3 Groovy: The Blue Pill or the Red Pill? . . . . . . . . . . . 19 2.4 Road Map . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 3 Getting Started 23 3.1 Installing Groovy . . . . . . . . . . . . . . . . . . . . . . . 23 3.2 Running a Groovy Script (groovy) . . . . . . . . . . . . . 26 3.3 Compiling Groovy (groovyc) . . . . . . . . . . . . . . . . . 27 3.4 Running the Groovy Shell (groovysh) . . . . . . . . . . . 28 3.5 Running the Groovy Console (groovyConsole) . . . . . . 33 3.6 Running Groovy on a Web Server (Groovlets) . . . . . . . 33 3.7 Groovy + Eclipse . . . . . . . . . . . . . . . . . . . . . . . 36 3.8 Groovy + IntelliJ IDEA . . . . . . . . . . . . . . . . . . . . 39 3.9 Groovy + TextMate . . . . . . . . . . . . . . . . . . . . . . 39 3.10 Groovy + [Insert Your IDE or Text Editor Here] . . . . . 40 4 New to Groovy 41 4.1 Automatic Imports . . . . . . . . . . . . . . . . . . . . . . 41 4.2 Optional Semicolons . . . . . . . . . . . . . . . . . . . . . 42 4.3 Optional Parentheses . . . . . . . . . . . . . . . . . . . . 44 4.4 Optional Return Statements . . . . . . . . . . . . . . . . 45 4.5 Optional Datatype Declaration (Duck Typing) . . . . . . 46 4.6 Optional Exception Handling . . . . . . . . . . . . . . . . 48 4.7 Operator Overloading . . . . . . . . . . . . . . . . . . . . 50 4.8 Safe Dereferencing (?) . . . . . . . . . . . . . . . . . . . . 51 4.9 Autoboxing . . . . . . . . . . . . . . . . . . . . . . . . . . 52 4.10 Groovy Truth . . . . . . . . . . . . . . . . . . . . . . . . . 54 4.11 Embedded Quotes . . . . . . . . . . . . . . . . . . . . . . 56CONTENTS 8 4.12 Heredocs (Triple Quotes) . . . . . . . . . . . . . . . . . . 56 4.13 GStrings . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 4.14 List Shortcuts . . . . . . . . . . . . . . . . . . . . . . . . . 57 4.15 Map shortcuts . . . . . . . . . . . . . . . . . . . . . . . . 61 4.16 Ranges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 4.17 Closures and Blocks . . . . . . . . . . . . . . . . . . . . . 66 5 Java and Groovy Integration 69 5.1 GroovyBeans (aka POGOs) . . . . . . . . . . . . . . . . . 69 5.2 Autogenerated Getters and Setters . . . . . . . . . . . . 71 5.3 getProperty and setProperty . . . . . . . . . . . . . . . . 74 5.4 Making Attributes Read-Only . . . . . . . . . . . . . . . . 75 5.5 Constructor Shortcut Syntax . . . . . . . . . . . . . . . . 76 5.6 Optional Parameters/Default Values . . . . . . . . . . . 77 5.7 Private Methods . . . . . . . . . . . . . . . . . . . . . . . 78 5.8 Calling Groovy from Java . . . . . . . . . . . . . . . . . . 79 5.9 Calling Java from Groovy . . . . . . . . . . . . . . . . . . 81 5.10 Interfaces in Groovy and Java . . . . . . . . . . . . . . . 81 5.11 The Groovy Joint Compiler . . . . . . . . . . . . . . . . . 82 5.12 Compiling Our Project with Ant . . . . . . . . . . . . . . 84 5.13 Compiling Our Project with Maven . . . . . . . . . . . . 85 6 Groovy from the Command Line 86 6.1 Running Uncompiled Groovy Scripts . . . . . . . . . . . 86 6.2 Shebanging Groovy . . . . . . . . . . . . . . . . . . . . . 87 6.3 Accepting Command-Line Arguments . . . . . . . . . . . 88 6.4 Running a Shell Command . . . . . . . . . . . . . . . . . 89 6.5 Using Shell Wildcards in Groovy Scripts . . . . . . . . . 90 6.6 Running Multiple Shell Commands at Once . . . . . . . 91 6.7 Waiting for a Shell Command to Finish Before Continuing 91 6.8 Getting System Properties . . . . . . . . . . . . . . . . . . 92 6.9 Getting Environment Variables . . . . . . . . . . . . . . . 94 6.10 Evaluating a String . . . . . . . . . . . . . . . . . . . . . 95 6.11 Calling Another Groovy Script . . . . . . . . . . . . . . . 96 6.12 Groovy on the Fly (groovy -e) . . . . . . . . . . . . . . . . 98 6.13 Including JARs at the Command Line . . . . . . . . . . . 98 CLICK HERE to purchase this book now.CONTENTS 9 7 File Tricks 100 7.1 Listing All Files in a Directory . . . . . . . . . . . . . . . 100 7.2 Reading the Contents of a File . . . . . . . . . . . . . . . 104 7.3 Writing Text to a File . . . . . . . . . . . . . . . . . . . . . 105 7.4 Copying Files . . . . . . . . . . . . . . . . . . . . . . . . . 108 7.5 Using AntBuilder to Copy a File . . . . . . . . . . . . . . 109 7.6 Using AntBuilder to Copy a Directory . . . . . . . . . . . 110 7.7 Moving/Renaming Files . . . . . . . . . . . . . . . . . . . 112 7.8 Deleting Files . . . . . . . . . . . . . . . . . . . . . . . . . 112 7.9 Creating a ZIP File/Tarball . . . . . . . . . . . . . . . . . 113 7.10 Unzipping/Untarring Files . . . . . . . . . . . . . . . . . 114 8 Parsing XML 116 8.1 The “I’m in a Hurry” Guide to Parsing XML . . . . . . . . 116 8.2 Understanding the Difference Between XmlParser and XmlSlurper117 8.3 Parsing XML Documents . . . . . . . . . . . . . . . . . . 121 8.4 Dealing with XML Attributes . . . . . . . . . . . . . . . . 121 8.5 Getting the Body of an XML Element . . . . . . . . . . . 123 8.6 Dealing with Mixed-Case Element Names . . . . . . . . 125 8.7 Dealing with Hyphenated Element Names . . . . . . . . 125 8.8 Navigating Deeply Nested XML . . . . . . . . . . . . . . . 127 8.9 Parsing an XML Document with Namespaces . . . . . . 131 8.10 Populating a GroovyBean from XML . . . . . . . . . . . . 134 9 Writing XML 136 9.1 The “I’m in a Hurry” Guide to Creating an XML Document 136 9.2 Creating Mixed-Case Element Names . . . . . . . . . . . 137 9.3 Creating Hyphenated Element Names . . . . . . . . . . . 138 9.4 Creating Namespaced XML Using MarkupBuilder . . . . 138 9.5 Understanding the Difference Between MarkupBuilder and StreamingMarkupBuilder139 9.6 Creating Parts of the XML Document Separately . . . . 140 9.7 Creating Namespaced XML Using StreamingMarkupBuilder141 9.8 Printing Out the XML Declaration . . . . . . . . . . . . . 142 9.9 Printing Out Processing Instructions . . . . . . . . . . . 143 9.10 Printing Arbitrary Strings (Comments, CDATA) . . . . . 143 9.11 Writing StreamingMarkupBuilder Output to a File . . . 145 9.12 StreamingMarkupBuilder at a Glance . . . . . . . . . . . 145 9.13 Creating HTML on the Fly . . . . . . . . . . . . . . . . . . 145 9.14 Converting CSV to XML . . . . . . . . . . . . . . . . . . . 148 9.15 Converting JDBC ResultSets to XML . . . . . . . . . . . 151 CLICK HERE to purchase this book now.CONTENTS 10 10 Web Services 153 10.1 Finding Our Local IP Address and Name . . . . . . . . . 153 10.2 Finding a Remote IP Address and Domain Name . . . . 155 10.3 Making an HTTP GET Request . . . . . . . . . . . . . . . 156 10.4 Working with QueryStrings . . . . . . . . . . . . . . . . . 160 10.5 Making an HTTP POST Request . . . . . . . . . . . . . . 164 10.6 Making an HTTP PUT Request . . . . . . . . . . . . . . . 168 10.7 Making an HTTP DELETE Request . . . . . . . . . . . . 170 10.8 Making a RESTful Request . . . . . . . . . . . . . . . . . 171 10.9 Making a CSV Request . . . . . . . . . . . . . . . . . . . 173 10.10Making a SOAP Request . . . . . . . . . . . . . . . . . . . 173 10.11Making an XML-RPC Request . . . . . . . . . . . . . . . 175 10.12Parsing Yahoo Search Results as XML . . . . . . . . . . 177 10.13Parsing an Atom Feed . . . . . . . . . . . . . . . . . . . . 178 10.14Parsing an RSS Feed . . . . . . . . . . . . . . . . . . . . . 180 11 Metaprogramming 182 11.1 Discovering the Class . . . . . . . . . . . . . . . . . . . . 183 11.2 Discovering the Fields of a Class . . . . . . . . . . . . . . 184 11.3 Checking for the Existence of a Field . . . . . . . . . . . 186 11.4 Discovering the Methods of a Class . . . . . . . . . . . . 189 11.5 Checking for the Existence of a Method . . . . . . . . . . 191 11.6 Creating a Field Pointer . . . . . . . . . . . . . . . . . . . 193 11.7 Creating a Method Pointer . . . . . . . . . . . . . . . . . 194 11.8 Calling Methods That Don’t Exist (invokeMethod) . . . . 194 11.9 Creating an Expando . . . . . . . . . . . . . . . . . . . . 195 11.10Adding Methods to a Class Dynamically (Categories) . . 196 11.11Adding Methods to a Class Dynamically (ExpandoMetaClass) 199 12 Working with Grails 201 12.1 Installing Grails . . . . . . . . . . . . . . . . . . . . . . . 201 12.2 Creating Your First Grails App . . . . . . . . . . . . . . . 205 12.3 Understanding Grails Environments . . . . . . . . . . . 213 12.4 Running Grails on a Different Port . . . . . . . . . . . . 214 12.5 Generating a WAR . . . . . . . . . . . . . . . . . . . . . . 215 12.6 Changing Databases . . . . . . . . . . . . . . . . . . . . . 215 12.7 Changing the Home Page . . . . . . . . . . . . . . . . . . 219 12.8 Understanding Controllers and Views . . . . . . . . . . . 220 12.9 Dynamic Scaffolding . . . . . . . . . . . . . . . . . . . . . 222 12.10Validating Our Data . . . . . . . . . . . . . . . . . . . . . 224 12.11Managing Table Relationships . . . . . . . . . . . . . . . 228 CLICK HERE to purchase this book now.CONTENTS 11 12.12Mapping Classes to Legacy Databases . . . . . . . . . . 232 13 Grails and Web Services 234 13.1 Returning XML . . . . . . . . . . . . . . . . . . . . . . . . 234 13.2 Returning JSON . . . . . . . . . . . . . . . . . . . . . . . 236 13.3 Returning an Excel Spreadsheet . . . . . . . . . . . . . . 238 13.4 Setting Up an Atom Feed . . . . . . . . . . . . . . . . . . 240 13.5 Setting Up an RSS Feed for Podcasts . . . . . . . . . . . 244 13.6 Installing Plug-Ins . . . . . . . . . . . . . . . . . . . . . . 248 Index 249
Amazonにもあった。
Groovy and Grails ならもう一冊。