Scripting in Java: Integrating with Groovy and JavaScript

Book description

Scripting in Java teaches you how to use the Java Scripting API and JavaScript to execute scripts and take advantage of the features of a scripting language while developing Java applications. The book also covers topics that enable scripting languages to take advantage of Java features and the Java class library, including the new Java Collections and JavaFX 8 APIs. Most of the examples in this book use JavaScript on the Nashorn engine.

Author Kishori Sharan will show you scripts in JavaScript to demonstrate its power and use in your Java applications. Some of the examples use the jrunscript and jjs command-line tools. Furthermore, debugging is discussed to equip you for situations when or if you encounter any issues with this kind of Java scripting. After reading and using this book, you will have most of what you need to do scripting in Java.

Table of contents

  1. Cover
  2. Title
  3. Copyright
  4. Dedication
  5. Contents at a Glance
  6. Contents
  7. About the Author
  8. About the Technical Reviewers
  9. Acknowledgments
  10. Introduction
  11. Chapter 1: Getting Started
    1. What Is Scripting in Java?
    2. Executing Your First Script
    3. Using the jjs Command-line Tool
    4. Printing Text in Nashorn
    5. Using Other Scripting Languages
    6. Exploring the javax.script Package
      1. The ScriptEngine and ScriptEngineFactory Interfaces
      2. The AbstractScriptEngine Class
      3. The ScriptEngineManager Class
      4. The Compilable Interface and the CompiledScript Class
      5. The Invocable Interface
      6. The Bindings Interface and the SimpleBindings Class
      7. The ScriptContext Interface and the SimpleScriptContext Class
      8. The ScriptException Class
    7. Discovering and Instantiating ScriptEngines
    8. Summary
  12. Chapter 2: Executing Scripts
    1. Using the eval() Method
    2. Passing Parameters
      1. Passing Parameters from Java Code to Scripts
      2. Passing Parameters from Scripts to Java Code
    3. Summary
  13. Chapter 3: Passing Parameters to Scripts
    1. Bindings, Scope, and Context
      1. Bindings
      2. Scope
      3. Defining the Script Context
      4. Putting Them Together
    2. Using a Custom ScriptContext
    3. Return Value of the eval() Method
    4. Reserved Keys for Engine Scope Bindings
    5. Changing the Default ScriptContext
    6. Sending Scripts Output to a File
    7. Summary
  14. Chapter 4: Writing Scripts in Nashorn
    1. Strict and Nonstrict Modes
    2. Identifiers
    3. Comments
    4. Declaring Variables
    5. Data Types
      1. The Undefined Type
      2. The Null Type
      3. The Number Type
      4. The Boolean Type
      5. The String Type
    6. Operators
    7. Type Conversion
      1. To Boolean Conversion
      2. To Number Conversion
      3. To String Conversion
    8. Statements
      1. Block Statement
      2. Variable Statement
      3. Empty Statement
      4. Expression Statement
      5. The if Statement
      6. Iteration Statements
      7. The continue, break, and return Statements
      8. The with Statement
      9. The switch Statement
      10. Labelled Statements
      11. The throw Statement
      12. The try Statement
      13. The debugger Statement
    9. Defining Functions
      1. Function Declaration
      2. Function Expression
      3. The Function() Constructor
    10. The Object Type
      1. Using an Object Literal
      2. Using a Constructor Function
      3. Object Inheritance
      4. Using Object.create() Method
    11. Binding Object Properties
    12. Locking Objects
    13. Accessing Missing Properties
    14. Serializing Objects
    15. Dynamically Evaluating Scripts
    16. Variable Scoping and Hoisting
    17. Using Strict Mode
    18. Built-in Global Objects
      1. The Object Object
      2. The Function Object
      3. The String Object
      4. The Number Object
      5. The Boolean Object
      6. The Date Object
      7. The Math Object
      8. The RegExp Object
      9. Knowing Script Location
    19. Built-in Global Functions
      1. The parseInt() Function
      2. The parseFloat() Function
      3. The isNaN() Function
      4. The isFinite() Function
      5. The decodeURI() Function
      6. The decodeURIComponent() Function
      7. The encodeURI() Function
      8. The encodeURIComponent() Function
      9. The load() and loadWithNewGlobal Functions
    20. Summary
  15. Chapter 5: Procedures and Compiled Scripts
    1. Invoking Procedures in Scripts
    2. Implementing Java Interfaces in Scripts
    3. Using Compiled Scripts
    4. Summary
  16. Chapter 6: Using Java in Scripting Languages
    1. Importing Java Types
      1. Using the Packages Global Object
      2. Using the Java Global Object
      3. Using the importPackage() and importClass() Functions
      4. Using the JavaImporter Object
    2. Creating and Using Java Objects
    3. Using Overloaded Java Methods
    4. Using Java Arrays
    5. Extending Java Classes Implementing Interfaces
      1. Using a Script Object
      2. Using the Anonymous Class–Like Syntax
      3. Using the JavaAdapter Object
      4. Using the Java.extend() Method
      5. Using a JavaScript Function
      6. Accessing Methods of a Superclass
    6. Using Lambda Expressions
    7. Summary
  17. Chapter 7: Collections
    1. What Is an Array in Nashorn?
    2. Creating an Array
      1. Using an Array Literal
      2. Using the Array Object
      3. Passing No Arguments
      4. Passing One Argument
      5. Passing Two or More Arguments
    3. Deleting Array Elements
    4. Length of an Array
    5. Iterating Over Array Elements
      1. Using the for Loop
      2. Using the forEach() Method
      3. Using the for-in Loop
    6. Checking for an Array
    7. Multidimensional Arrays
    8. Methods of the Array Object
      1. Concatenating Elements
      2. Joining Array Elements
      3. Reversing Array Elements
      4. Slicing an Array
      5. Splicing an Array
      6. Sorting an Array
      7. Adding and Removing Elements at Ends
      8. Searching an Array
      9. Evaluating Predicates
    9. Converting an Array to a String
    10. Stream-Like Processing of Arrays
    11. Array-Like Objects
    12. Typed Arrays
      1. The ArrayBuffer Object
      2. Views of an ArrayBuffer
    13. Using Lists, Maps, and Sets
      1. Using a Java List as a Nashorn Array
      2. Using a Java Map as a Nashorn Object
    14. Using Java Arrays
    15. Arrays to Java Collections Conversions
    16. Summary
  18. Chapter 8: Implementing a Script Engine
    1. Introduction
    2. The Expression Class
      1. The Instance Variables
      2. The Constructor
      3. The parse() Method
      4. The getOperandValue() Method
      5. The eval() Method
    3. The JKScriptEngine Class
    4. The JKScriptEngineFactory Class
    5. Preparing for Deployment
    6. Packaging the JKScript Files
    7. Using the JKScript Script Engine
    8. Summary
  19. Chapter 9: The jrunscript Command-Line Shell
    1. The Syntax
    2. Execution Modes of the Shell
      1. One-liner Mode
      2. Batch Mode
      3. Interactive Mode
    3. Listing Available Script Engines
    4. Adding a Script Engine to the Shell
    5. Using Other Script Engines
    6. Passing Arguments to Scripts
    7. Global Functions
    8. Summary
  20. Chapter 10: The jjs Command-Line Tool
    1. The Syntax
    2. The Options
    3. Using jjs in Interactive Mode
    4. Passing Arguments to jjs
    5. Using jjs in Scripting Mode
    6. Summary
  21. Chapter 11: Using JavaFX in Nashorn
    1. JavaFX Support in jjs
    2. Structure of JavaFX Applications in Scripts
    3. Importing JavaFX Types
    4. Summary
  22. Chapter 12: Java APIs for Nashorn
    1. What Are Java APIs for Nashorn?
    2. Instantiating the Nashorn Engine
    3. Sharing the Engine Globals
    4. Using Script Objects in Java Code
      1. Using Properties of Script Objects
      2. Creating Nashorn Objects in Java
      3. Invoking Script Functions From Java
      4. Converting Script Dates to Java Dates
    5. Summary
  23. Chapter 13: Debugging, Tracing, and Profiling Scripts
    1. Debugging Standalone Scripts
    2. Debugging Scripts from Java Code
    3. Tracing and Profiling Scripts
    4. Summary
  24. Index

Product information

  • Title: Scripting in Java: Integrating with Groovy and JavaScript
  • Author(s): Kishori Sharan
  • Release date: December 2014
  • Publisher(s): Apress
  • ISBN: 9781484207130