Preview the new Java 9 modules, SDK, JDK, JVM and more in this comprehensive early adopter title. This book covers what’s new in Java 9 and how to use these new features. Java 9 Revealed is for experienced Java programmers looking to make the migration from Java 7 or Java 8 to Java 9.
Author Kishori Sharan begins by covering how to develop Java applications using new the module system introduced in Java 9 and how to use the JShell tool in Java 9 for quick prototyping. There is extensive coverage of new Java 9 features, such as the new layout of the modular JDK/JRE runtime image, new convenience factory methods for creating collections, the new spin-wait hints introduced to improve performance of spin loops in your code, and the new Desktop API for implementing platform-specific desktop features. Along the way you will also learn how to use the Reactive Streams API in Java 9 and, most importantly, this book will show you the breaking changes in Java 9.
- Discover what is new in the Process API
- Inspect a thread’s stack with the Stack-Walking API
- Use the jlink tool to create a custom runtime image
- Work with HTML5 Javadoc and use the new search feature in Javadoc
- Learn how to use new methods and collectors in the Streams API
- Learn how to create a custom logger to log messages from platform classes and how to use JVM logs
- Learn about new methods in the Optional class and how to use them
- Learn how to compare arrays and slices of arrays
- Learn how to use the enhanced try-with-resources blocks
- Make your object deserialization more secure by using object deserialization filters
Experienced Java programmers and developers.
Chapter 1: Introduction 1
Introduction to JDK 9 1
How to Read This Book 2
System Requirements 3
Installing the NetBeans IDE 4
Downloading the Source Code 5
Chapter 2: The Module System 7
Life Before Java 9 7
The New Module System 9
What Is a Module? 10
Module Dependencies 11
Module Graph 13
Aggregator Modules 17
Declaring Modules 17
Module Names 19
Controlling Access to Modules 20
Declaring Dependency 21
Configuring Services 21
Module Descriptors 22
Compiling Module Declarations 22
Module Version 23
Module Source Files Structure 23
Packaging Modules 24
A Module in a Directory 24
A Module in a Modular JAR 24
A Module in a JMOD File 25
Module Path 25
Observable Modules 28
Summary 29
Chapter 3: Creating Your First Module 31
Using the Command Prompt 31
Setting Up the Directories 31
Writing the Source Code 32
Compiling the Source Code 33
Packaging the Module Code 35
Running the Program 36
Using the NetBeans IDE 39
Configuring the IDE 39
Creating the Java Project 43
Setting the Project Properties 45
Adding the Module Declaration 47
Viewing Module Graph 50
Writing the Source Code 50
Compiling the Source Code 52
Packaging the Module Code 52
Running the Program 53
Summary 55
Chapter 4: Module Dependency 57
Declaring Module Dependency 57
Troubleshooting the Example 65
Empty Package Error 65
Module Not Found Error 66
Package Does Not Exist Error 66
Module Resolution Exception 66
Implicit Readability 67
Qualified Exports 72
Optional Dependency 73
Accessing Modules Using Reflection 74
Open Modules 75
Opening Packages 75
Using Deep Reflection 76
Type Accessibility 85
Splitting Packages Across Modules 85
Restrictions in Module Declarations 86
Types of Modules 86
Normal Modules 88
Open Modules 88
Automatic Modules 88
Unnamed Modules 92
Migration Path to JDK 9 98
Dissembling Module Definitions 99
Summary 103
Chapter 5: Implementing Services 105
What Is a Service? 105
Discovering Services 107
Providing Service Implementations 108
Defining the Service Interface 110
Defining Service Providers 113
Defining a Generic Prime Service Provider 113
Defining a Faster Prime Service Provider 115
Defining a Fastest Prime Service Provider 117
Testing the Prime Service 118
Selecting and Filtering Providers 121
Testing Prime Service in Legacy Mode 123
Summary 125
Chapter 6: Packaging Modules 127
The JAR Format 127
What Is a Multi-Release JAR? 128
Creating Multi-Release JARs 129
Rules for Multi-Release JARs 136
Multi-Release JARs and JAR URL 137
Multi-Release Manifest Attribute 138
The JMOD Format 138
Using the jmod Tool 138
Summary 145
Chapter 7: Creating Custom Runtime Images 147
What Is a Custom Runtime Image? 147
Creating Custom Runtime Images 148
Binding Services 151
Using Plugins with the jlink Tool 153
The jimage Tool 156
Summary 158
Chapter 8: Breaking Changes in JDK 9 159
The New JDK Versioning Scheme 159
Version Number 160
Prerelease Information 161
Build Information 161
Additional Information 161
Parsing Old and New Version Strings 162
Version Changes to System Properties 162
Using the Runtime. Version Class 162
Changes to the JDK and JRE 165
Layout Changes in JDK and JRE 166
Behavioral Changes 168
Changes in Class Loaders 169
Accessing Resources 173
Accessing Resources Before JDK 9 173
Accessing Resources in JDK 9 177
Using JDK Internal APIs 191
Patching Module Contents 193
Summary 195
Chapter 9: Breaking Module Encapsulation 197
What Is Breaking Module Encapsulation? 197
Command-Line Options 198
The --add-exports Option 198
The --add-opens Option 199
The --add-reads Option 200
The --permit-illegal-access Option 200
An Example 201
Using Manifest Attributes of a JAR 207
Summary 210
Chapter 10: The Module API 211
What Is the Module API? 211
Representing Modules 213
Describing Modules 213
Representing Module Statements 214
Representing a Module Version 216
Other Properties of Modules 217
Knowing Module Basic Info 218
Querying Modules 221
Updating Modules 223
Accessing Module Resources 226
Annotation on Modules 226
Loading Classes 228
Working with Module Layers 231
Finding Modules 233
Reading Module Contents 235
Creating Configurations 237
Creating Module Layers 238
Summary 246
Chapter 11: The Java Shell 249
What Is the Java Shell? 249
The JShell Architecture 251
Starting the JShell Tool 252
Exiting the JShell Tool 254
What Are Snippets and Commands? 254
Evaluating Expressions 256
Listing Snippets 258
Editing Snippets 262
Rerunning Previous Snippets 265
Declaring Variables 265
Import Statements 268
Method Declarations 272
Type Declarations 273
Setting Execution Environment 276
No Checked Exceptions 278
Auto-Completion 279
Snippets and Commands History 282
Reading JShell Stack Trace 283
Reusing JShell Sessions 284
Resetting the JShell State 286
Reloading the JShell State 287
Configuring JShell 289
Setting the Snippet Editor 289
Setting Feedback Mode 291
Creating Custom feedback Modes 293
Setting Up Startup Snippets 297
Using JShell Documentation 300
The JShell API 302
Creating a JShell 303
Working with Snippets 304
Handling Snippet Events 306
An Example 306
Summary 311
Chapter 12: Process API Updates 313
What Is the Process API? 313
The Current Process 314
Querying Process State 315
Comparing Processes 318
Creating a Process 318
Obtaining a Process Handle 326
Terminating Processes 328
Managing Process Permissions 329
Summary 331
Chapter 13: Collection API Updates 333
The Background 333
Unmodifiable Lists 336
Unmodifiable Sets 339
Unmodifiable Maps 342
Summary 346
Chapter 14: The HTTP/2 Client API 347
What Is the HTTP/2 Client API? 348
Setting Up Examples 350
Creating HTTP Clients 350
Processing HTTP Requests 352
Obtaining an HTTP Request Builder 352
Setting HTTP Request Parameters 353
Setting the Request Redirection Policy 363
Using the WebSocket Protocol 364
Creating a Server Endpoint 364
Creating a Client Endpoint 366
Running the WebSocket Program 370
Troubleshooting the WebSocket Application 374
Summary 374
Chapter 15: Enhanced Deprecation 377
What Is Deprecation? 377
How to Deprecate an API 377
Updates to @Deprecated in JDK 9 379
Suppressing Deprecation Warnings 381
A Deprecated API Example 382
Static Analysis of Deprecated APIs 386
Dynamic Analysis of Deprecated APIs 389
No Deprecation Warnings on Imports 389
Summary 390
Chapter 16: Stack Walking 391
What Is a Stack? 391
What Is Stack Walking? 392
Stack Walking in JDK 8 392
Drawbacks to Stack Walking 395
Stack Walking in JDK 9 396
Specifying Stack-Walking Options 396
Representing a Stack Frame 396
Obtaining a StackWalker 398
Walking the Stack 399
Knowing the Caller’s Class 404
Stack-Walking Permissions 407
Summary 408
Chapter 17: Reactive Streams 411
What Is a Stream? 411
What Is Reactive Streams? 412
The Reactive Streams API in JDK 9 414
Publisher-Subscriber Interactions 414
Creating Publishers 415
Publishing Items 416
A Quick Example 417
Creating Subscribers 419
Using Processors 424
Summary 427
Chapter 18: Streams API Updates 429
New Stream Operations 429
New Collectors 433
Summary 439
Chapter 19: Platform and JVM Logging 441
Platform Logging API 441
Setting up Log4j Libraries 442
Setting Up a NetBeans Project 442
Defining a Module 443
Adding a Log4j Configuration File 444
Creating a System Logger 445
Creating a Logger Finder 447
Testing the Platform Logger 448
Unified JVM Logging 452
Message Tags 453
Message Levels 454
Message Decorations 454
Message Output Destination 455
The -Xlog Syntax 455
Summary 459
Chapter 20: Other Changes in JDK 9 461
The Underscore Is a Keyword 462
Improved try-with-resources Blocks 463
Diamond Operator in Anonymous Classes 468
Private Methods in Interfaces 470
@SafeVarargs on Private Methods 472
Discarding Process Outputs 474
New Methods in the StrictMath Class 475
Changes to the ClassLoader Class 478
New Methods in the Optional<T> Class 478
Additions to the CompletableFuture<T> Class 481
Spin-Wait Hints 482
Enhancements to the Time API 483
The Clock Class 483
The Duration Class 484
The ofInstant() Factory Method 486
Obtaining Epoch Seconds 487
Stream of LocalDate 488
New Formatting Options 490
Stream Operations with Scanner 491
Enhancement to the Matcher Class 492
Enhancement to the Objects Class 494
Comparing Arrays 495
The Applet API Is Deprecated 497
Javadoc Enhancements 498
Native Desktop Features 500
Object Deserialization Filters 504
Additions to Java I/O API 512
Summary 513
Index 515
Добавить комментарий