Scripting in Java: Integrating with Groovy and JavaScript

At a Glance; Contents; About the Author; About the Technical Reviewer; Acknowledgments; Introduction; Chapter 1: Introdu

341 79 3MB

English Pages 388 [372] Year 2014

Report DMCA / Copyright

DOWNLOAD FILE

Scripting in Java: Integrating with Groovy and JavaScript

Table of contents :
Contents at a Glance......Page 3
Contents......Page 361
About the Author......Page 370
About the Technical Reviewers......Page 371
Acknowledgments......Page 372
Introduction......Page 4
What Is Scripting in Java?......Page 7
Executing Your First Script......Page 9
Using the jjs Command-line Tool......Page 11
Printing Text in Nashorn......Page 12
Using Other Scripting Languages......Page 13
The ScriptEngine and ScriptEngineFactory Interfaces......Page 17
The Bindings Interface and the SimpleBindings Class......Page 18
Discovering and Instantiating ScriptEngines......Page 19
Summary......Page 21
Using the eval( ) Method......Page 22
Passing Parameters from Java Code to Scripts......Page 24
Passing Parameters from Scripts to Java Code......Page 27
Summary......Page 28
Bindings......Page 29
Scope......Page 31
Defining the Script Context......Page 32
Putting Them Together......Page 37
Using a Custom ScriptContext......Page 44
Return Value of the eval( ) Method......Page 47
Reserved Keys for Engine Scope Bindings......Page 49
Changing the Default ScriptContext......Page 50
Sending Scripts Output to a File......Page 51
Summary......Page 53
Chapter 4: Writing Scripts in Nashorn......Page 54
Strict and Nonstrict Modes......Page 55
Identifiers......Page 56
Comments......Page 57
Declaring Variables......Page 58
The Undefined Type......Page 59
The Number Type......Page 60
The String Type......Page 61
Operators......Page 64
To Boolean Conversion......Page 69
To Number Conversion......Page 71
To String Conversion......Page 72
Statements......Page 74
Block Statement......Page 77
Expression Statement......Page 78
The if Statement......Page 79
Iteration Statements......Page 80
The continue, break, and return Statements......Page 82
The with Statement......Page 83
The switch Statement......Page 84
Labelled Statements......Page 85
The try Statement......Page 86
The debugger Statement......Page 90
Function Declaration......Page 91
Working with Function Arguments......Page 93
Function Expression......Page 95
The Function( ) Constructor......Page 98
The Object Type......Page 99
Using an Object Literal......Page 100
Accessing Object’s Properties......Page 101
Defining Accessor Properties......Page 105
Setting Property Attributes......Page 106
Using a Constructor Function......Page 111
Object Inheritance......Page 116
Using Object.create( ) Method......Page 126
Binding Object Properties......Page 127
Locking Objects......Page 131
Accessing Missing Properties......Page 132
Serializing Objects......Page 134
Dynamically Evaluating Scripts......Page 135
Variable Scoping and Hoisting......Page 136
The Object Object......Page 139
The Function Object......Page 142
The String Object......Page 146
The Number Object......Page 148
The Boolean Object......Page 150
The Date Object......Page 151
The Math Object......Page 153
The RegExp Object......Page 154
Knowing Script Location......Page 165
The parseInt( ) Function......Page 166
The parseFloat() Function......Page 167
The isNaN() Function......Page 168
The decodeURI( ) Function......Page 169
The encodeURI( ) Function......Page 170
The load( ) and loadWithNewGlobal Functions......Page 171
Summary......Page 175
Invoking Procedures in Scripts......Page 177
Implementing Java Interfaces in Scripts......Page 183
Using Compiled Scripts......Page 189
Summary......Page 191
Importing Java Types......Page 193
Using the Java Global Object......Page 194
Using the importPackage() and importClass() Functions......Page 195
Creating and Using Java Objects......Page 197
Using Overloaded Java Methods......Page 199
Using Java Arrays......Page 201
Using a Script Object......Page 203
Using the JavaAdapter Object......Page 205
Using the Java.extend() Method......Page 206
Accessing Methods of a Superclass......Page 208
Using Lambda Expressions......Page 210
Summary......Page 211
What Is an Array in Nashorn?......Page 212
Using an Array Literal......Page 213
Using the Array Object......Page 219
Passing One Argument......Page 220
Passing Two or More Arguments......Page 221
Deleting Array Elements......Page 222
Length of an Array......Page 223
Using the for Loop......Page 224
Using the forEach() Method......Page 226
Using the for-in Loop......Page 227
Checking for an Array......Page 228
Methods of the Array Object......Page 229
Reversing Array Elements......Page 230
Splicing an Array......Page 231
Sorting an Array......Page 232
Adding and Removing Elements at Ends......Page 233
Searching an Array......Page 236
Evaluating Predicates......Page 237
Stream-Like Processing of Arrays......Page 238
Array-Like Objects......Page 241
The ArrayBuffer Object......Page 243
Views of an ArrayBuffer......Page 244
Typed Arrays......Page 245
DataView View......Page 250
Using a Java List as a Nashorn Array......Page 252
Using a Java Map as a Nashorn Object......Page 254
Using Java Arrays......Page 255
Arrays to Java Collections Conversions......Page 259
Summary......Page 261
Introduction......Page 263
The Expression Class......Page 265
The parse() Method......Page 269
The JKScriptEngine Class......Page 270
The JKScriptEngineFactory Class......Page 272
Preparing for Deployment......Page 274
Using the JKScript Script Engine......Page 275
Summary......Page 279
The Syntax......Page 280
Batch Mode......Page 282
Interactive Mode......Page 283
Using Other Script Engines......Page 284
Passing Arguments to Scripts......Page 285
Global Functions......Page 286
Summary......Page 292
The Syntax......Page 293
The Options......Page 294
Using jjs in Interactive Mode......Page 297
Passing Arguments to jjs......Page 298
Using jjs in Scripting Mode......Page 299
Summary......Page 304
Structure of JavaFX Applications in Scripts......Page 305
Importing JavaFX Types......Page 312
Summary......Page 316
What Are Java APIs for Nashorn?......Page 317
Instantiating the Nashorn Engine......Page 320
Sharing the Engine Globals......Page 321
Using Script Objects in Java Code......Page 325
Using Properties of Script Objects......Page 328
Creating Nashorn Objects in Java......Page 332
Invoking Script Functions From Java......Page 335
Converting Script Dates to Java Dates......Page 338
Summary......Page 339
Chapter 13: Debugging, Tracing, and Profiling Scripts......Page 340
Debugging Standalone Scripts......Page 341
Debugging Scripts from Java Code......Page 344
Tracing and Profiling Scripts......Page 348
Summary......Page 351
Index......Page 352

Polecaj historie