Java 9 Revealed - For Early Adoption and Migration
von: Kishori Sharan
Apress, 2017
ISBN: 9781484225929
Sprache: Englisch
530 Seiten, Download: 6498 KB
Format: PDF, auch als Online-Lesen
Contents | 5 | ||
About the Author | 16 | ||
About the Technical Reviewer | 17 | ||
Acknowledgments | 18 | ||
Introduction | 19 | ||
Chapter 1: Introduction | 21 | ||
Introduction to JDK 9 | 21 | ||
How to Read This Book | 22 | ||
System Requirements | 23 | ||
Installing the NetBeans IDE | 24 | ||
Downloading the Source Code | 25 | ||
Chapter 2: The Module System | 26 | ||
Life Before Java 9 | 26 | ||
The New Module System | 28 | ||
What Is a Module? | 29 | ||
Module Dependencies | 30 | ||
Module Graph | 32 | ||
Aggregator Modules | 36 | ||
Declaring Modules | 36 | ||
Module Names | 38 | ||
Controlling Access to Modules | 38 | ||
Declaring Dependency | 40 | ||
Configuring Services | 40 | ||
Module Descriptors | 41 | ||
Compiling Module Declarations | 41 | ||
Module Version | 41 | ||
Module Source Files Structure | 42 | ||
Packaging Modules | 43 | ||
A Module in a Directory | 43 | ||
A Module in a Modular JAR | 43 | ||
A Module in a JMOD File | 44 | ||
Module Path | 44 | ||
Observable Modules | 46 | ||
Summary | 48 | ||
Chapter 3: Creating Your First Module | 49 | ||
Using the Command Prompt | 49 | ||
Setting Up the Directories | 49 | ||
Writing the Source Code | 50 | ||
Compiling the Source Code | 51 | ||
Packaging the Module Code | 53 | ||
Running the Program | 54 | ||
Using the NetBeans IDE | 57 | ||
Configuring the IDE | 57 | ||
Creating the Java Project | 61 | ||
Setting the Project Properties | 63 | ||
Adding the Module Declaration | 65 | ||
Viewing Module Graph | 68 | ||
Writing the Source Code | 68 | ||
Compiling the Source Code | 70 | ||
Packaging the Module Code | 70 | ||
Running the Program | 71 | ||
Summary | 73 | ||
Chapter 4: Module Dependency | 75 | ||
Declaring Module Dependency | 75 | ||
Troubleshooting the Example | 83 | ||
Empty Package Error | 83 | ||
Module Not Found Error | 84 | ||
Package Does Not Exist Error | 84 | ||
Module Resolution Exception | 84 | ||
Implicit Readability | 85 | ||
Qualified Exports | 89 | ||
Optional Dependency | 90 | ||
Accessing Modules Using Reflection | 91 | ||
Open Modules | 92 | ||
Opening Packages | 92 | ||
Using Deep Reflection | 93 | ||
Type Accessibility | 102 | ||
Splitting Packages Across Modules | 102 | ||
Restrictions in Module Declarations | 103 | ||
Types of Modules | 103 | ||
Normal Modules | 105 | ||
Open Modules | 105 | ||
Automatic Modules | 105 | ||
Unnamed Modules | 109 | ||
Normal Modules to Unnamed Modules | 110 | ||
Unnamed Modules to Normal Modules | 112 | ||
Migration Path to JDK 9 | 115 | ||
Disassembling Module Definitions | 116 | ||
Summary | 120 | ||
Chapter 5: Implementing Services | 122 | ||
What Is a Service? | 122 | ||
Discovering Services | 124 | ||
Providing Service Implementations | 125 | ||
Defining the Service Interface | 127 | ||
Defining Service Providers | 130 | ||
Defining a Generic Prime Service Provider | 130 | ||
Defining a Faster Prime Service Provider | 132 | ||
Defining a Fastest Prime Service Provider | 134 | ||
Testing the Prime Service | 135 | ||
Selecting and Filtering Providers | 138 | ||
Testing Prime Service in Legacy Mode | 140 | ||
Summary | 142 | ||
Chapter 6: Packaging Modules | 143 | ||
The JAR Format | 143 | ||
What Is a Multi-Release JAR? | 144 | ||
Creating Multi-Release JARs | 145 | ||
Rules for Multi-Release JARs | 152 | ||
Modular Multi-Release JARs | 152 | ||
Modular Multi-Release JARs and Encapsulation | 152 | ||
Multi-Release JARs and Boot Loader | 153 | ||
Same Versioned Files | 153 | ||
Multi-Release JARs and JAR URL | 153 | ||
Multi-Release Manifest Attribute | 154 | ||
The JMOD Format | 154 | ||
Using the jmod Tool | 154 | ||
Creating JMOD Files | 156 | ||
Extracting JMOD File Contents | 157 | ||
Listing JMOD File Contents | 157 | ||
Describing a JMOD File | 158 | ||
Recording Modules Hashes | 158 | ||
Summary | 161 | ||
Chapter 7: Creating Custom Runtime Images | 162 | ||
What Is a Custom Runtime Image? | 162 | ||
Creating Custom Runtime Images | 163 | ||
Binding Services | 166 | ||
Using Plugins with the jlink Tool | 168 | ||
The jimage Tool | 171 | ||
Summary | 173 | ||
Chapter 8: Breaking Changes in JDK 9 | 174 | ||
The New JDK Versioning Scheme | 174 | ||
Version Number | 175 | ||
Prerelease Information | 176 | ||
Build Information | 176 | ||
Additional Information | 176 | ||
Parsing Old and New Version Strings | 177 | ||
Version Changes to System Properties | 177 | ||
Using the Runtime.Version Class | 177 | ||
Changes to the JDK and JRE | 180 | ||
Layout Changes in JDK and JRE | 181 | ||
Behavioral Changes | 183 | ||
Endorsed Standards Override Mechanism | 183 | ||
Extension Mechanism | 184 | ||
Changes in Class Loaders | 184 | ||
Accessing Resources | 188 | ||
Accessing Resources Before JDK 9 | 188 | ||
Accessing Resources in JDK 9 | 192 | ||
Resource Naming Syntax | 193 | ||
Rules to Find Resources | 193 | ||
An Example of Accessing Resources in Named Modules | 196 | ||
Accessing Resources in the Runtime Image | 201 | ||
Using JDK Internal APIs | 206 | ||
Patching Module Contents | 208 | ||
Summary | 210 | ||
Chapter 9: Breaking Module Encapsulation | 212 | ||
What Is Breaking Module Encapsulation? | 212 | ||
Command-Line Options | 213 | ||
The --add-exports Option | 213 | ||
The --add-opens Option | 214 | ||
The --add-reads Option | 215 | ||
The --illegal-access Option | 215 | ||
An Example | 216 | ||
Using Manifest Attributes of a JAR | 224 | ||
Summary | 227 | ||
Chapter 10: The Module API | 229 | ||
What Is the Module API? | 229 | ||
Representing Modules | 231 | ||
Describing Modules | 231 | ||
Representing Module Statements | 232 | ||
Representing the exports Statement | 232 | ||
Representing the opens Statement | 233 | ||
Representing the provides Statement | 233 | ||
Representing the requires Statement | 233 | ||
Representing a Module Version | 234 | ||
Other Properties of Modules | 235 | ||
Knowing Module Basic Info | 236 | ||
Querying Modules | 239 | ||
Updating Modules | 241 | ||
Accessing Module Resources | 244 | ||
Annotation on Modules | 244 | ||
Loading Classes | 246 | ||
Working with Module Layers | 249 | ||
Finding Modules | 251 | ||
Reading Module Contents | 253 | ||
Creating Configurations | 255 | ||
Creating Module Layers | 256 | ||
Summary | 264 | ||
Chapter 11: The Java Shell | 266 | ||
What Is the Java Shell? | 266 | ||
The JShell Architecture | 268 | ||
Starting the JShell Tool | 269 | ||
Exiting the JShell Tool | 271 | ||
What Are Snippets and Commands? | 271 | ||
Evaluating Expressions | 273 | ||
Listing Snippets | 275 | ||
Editing Snippets | 279 | ||
Rerunning Previous Snippets | 281 | ||
Declaring Variables | 281 | ||
Import Statements | 284 | ||
Method Declarations | 288 | ||
Type Declarations | 289 | ||
Setting Execution Environment | 292 | ||
No Checked Exceptions | 294 | ||
Auto-Completion | 295 | ||
Snippets and Commands History | 298 | ||
Reading JShell Stack Trace | 299 | ||
Reusing JShell Sessions | 300 | ||
Resetting the JShell State | 302 | ||
Reloading the JShell State | 303 | ||
Configuring JShell | 305 | ||
Setting the Snippet Editor | 305 | ||
Setting Feedback Mode | 307 | ||
Creating Custom feedback Modes | 309 | ||
Setting Up Startup Snippets | 313 | ||
Using JShell Documentation | 316 | ||
The JShell API | 318 | ||
Creating a JShell | 319 | ||
Working with Snippets | 320 | ||
Handling Snippet Events | 322 | ||
An Example | 322 | ||
Summary | 326 | ||
Chapter 12: Process API Updates | 327 | ||
What Is the Process API? | 327 | ||
The Current Process | 328 | ||
Querying Process State | 329 | ||
Comparing Processes | 332 | ||
Creating a Process | 333 | ||
Obtaining a Process Handle | 341 | ||
Terminating Processes | 343 | ||
Managing Process Permissions | 343 | ||
Summary | 346 | ||
Chapter 13: Collection API Updates | 347 | ||
The Background | 347 | ||
Unmodifiable Lists | 350 | ||
Unmodifiable Sets | 353 | ||
Unmodifiable Maps | 356 | ||
Summary | 360 | ||
Chapter 14: The HTTP/2 Client API | 361 | ||
What Is the HTTP/2 Client API? | 362 | ||
Setting Up Examples | 363 | ||
Creating HTTP Clients | 364 | ||
Processing HTTP Requests | 366 | ||
Obtaining an HTTP Request Builder | 366 | ||
Setting HTTP Request Parameters | 367 | ||
Setting Request Headers | 368 | ||
Setting the Request Body | 368 | ||
Creating HTTP Requests | 370 | ||
Processing HTTP Responses | 370 | ||
Processing Response Status and Headers | 371 | ||
Processing the Response Body | 372 | ||
Processing Response Trailers | 376 | ||
Setting the Request Redirection Policy | 376 | ||
Using the WebSocket Protocol | 377 | ||
Creating a Server Endpoint | 377 | ||
Creating a Client Endpoint | 380 | ||
Creating a Listener | 380 | ||
Building an Endpoint | 382 | ||
Sending Messages to a Peer | 383 | ||
Running the WebSocket Program | 383 | ||
Troubleshooting the WebSocket Application | 387 | ||
Summary | 388 | ||
Chapter 15: Enhanced Deprecation | 389 | ||
What Is Deprecation? | 389 | ||
How to Deprecate an API | 389 | ||
Updates to @Deprecated in JDK 9 | 391 | ||
Suppressing Deprecation Warnings | 393 | ||
A Deprecated API Example | 394 | ||
Static Analysis of Deprecated APIs | 398 | ||
Dynamic Analysis of Deprecated APIs | 401 | ||
No Deprecation Warnings on Imports | 401 | ||
Summary | 402 | ||
Chapter 16: Stack Walking | 403 | ||
What Is a Stack? | 403 | ||
What Is Stack Walking? | 404 | ||
Stack Walking in JDK 8 | 404 | ||
Drawbacks to Stack Walking | 407 | ||
Stack Walking in JDK 9 | 408 | ||
Specifying Stack-Walking Options | 408 | ||
Representing a Stack Frame | 408 | ||
Obtaining a StackWalker | 410 | ||
Walking the Stack | 411 | ||
Knowing the Caller’s Class | 416 | ||
Stack-Walking Permissions | 419 | ||
Summary | 420 | ||
Chapter 17: Reactive Streams | 422 | ||
What Is a Stream? | 422 | ||
What Is Reactive Streams? | 423 | ||
The Reactive Streams API in JDK 9 | 425 | ||
Publisher-Subscriber Interactions | 425 | ||
Creating Publishers | 426 | ||
Publishing Items | 427 | ||
A Quick Example | 428 | ||
Creating Subscribers | 430 | ||
Using Processors | 435 | ||
Summary | 438 | ||
Chapter 18: Streams API Updates | 439 | ||
New Stream Operations | 439 | ||
New Collectors | 443 | ||
Summary | 449 | ||
Chapter 19: Platform and JVM Logging | 450 | ||
Platform Logging API | 450 | ||
Setting up Log4j Libraries | 451 | ||
Setting Up a NetBeans Project | 451 | ||
Defining a Module | 452 | ||
Adding a Log4j Configuration File | 453 | ||
Creating a System Logger | 454 | ||
Creating a Logger Finder | 456 | ||
Testing the Platform Logger | 457 | ||
Further Work | 460 | ||
Unified JVM Logging | 461 | ||
Message Tags | 462 | ||
Message Levels | 463 | ||
Message Decorations | 463 | ||
Message Output Destination | 464 | ||
The -Xlog Syntax | 464 | ||
Summary | 468 | ||
Chapter 20: Other Changes in JDK 9 | 469 | ||
The Underscore Is a Keyword | 470 | ||
Improved try-with-resources Blocks | 471 | ||
Diamond Operator in Anonymous Classes | 476 | ||
Private Methods in Interfaces | 478 | ||
@SafeVarargs on Private Methods | 480 | ||
Discarding Process Outputs | 482 | ||
New Methods in the StrictMath Class | 483 | ||
Changes to the ClassLoader Class | 486 | ||
New Methods in the Optional |
486 | ||
Additions to the CompletableFuture |
489 | ||
Spin-Wait Hints | 490 | ||
Enhancements to the Time API | 491 | ||
The Clock Class | 491 | ||
The Duration Class | 492 | ||
Dividing a Duration by Another Duration | 492 | ||
Converting and Retrieving Duration Parts | 493 | ||
Truncating Duration | 494 | ||
The ofInstant() Factory Method | 494 | ||
Obtaining Epoch Seconds | 495 | ||
Stream of LocalDate | 496 | ||
New Formatting Options | 498 | ||
Modified Julian Day Formatter | 498 | ||
Generic Time Zone Names | 498 | ||
Stream Operations with Scanner | 499 | ||
Enhancement to the Matcher Class | 500 | ||
Enhancement to the Objects Class | 502 | ||
Comparing Arrays | 503 | ||
The Applet API Is Deprecated | 505 | ||
Javadoc Enhancements | 506 | ||
Native Desktop Features | 508 | ||
Object Deserialization Filters | 512 | ||
Additions to Java I/O API | 520 | ||
Summary | 521 | ||
Erratum | 523 | ||
Index | 524 |