project jigsaw: under the hood

62
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | Project Jigsaw: Under The Hood Alex Buckley Java Platform Group, Oracle October 2015

Upload: vanxuyen

Post on 03-Jan-2017

229 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Project Jigsaw: Under The Hood

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |

Project Jigsaw: Under The Hood

Alex Buckley Java Platform Group, Oracle October 2015

Page 2: Project Jigsaw: Under The Hood

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |

The Modularity Landscape

• Java Platform Module System

– JSR 376, started February 2015 – targeted for Java SE 9

• Java SE 9 Platform

– JSR not started yet – will own the modularization of the Java SE API

• Project Jigsaw – Reference Implementation of JSR 376 in OpenJDK (JEP 261)

– Modularization of the JDK (JEP 200, JEP 201, JEP 260)

– New run-time image format (JEP 220)

2

Page 3: Project Jigsaw: Under The Hood

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |

Project Jigsaw: Under The Hood

Part I: Accessibility and Readability

Part II: Different Kinds of Modules

Part III: Loaders and Layers

3

Page 4: Project Jigsaw: Under The Hood

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |

Part I: Accessibility and Readability

4

Page 5: Project Jigsaw: Under The Hood

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |

Accessibility 1995-2015

• public

• protected

• <package>

• private

5

Page 6: Project Jigsaw: Under The Hood

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |

Accessibility 2015-

• public to everyone

• public but only to specific modules

• public only within a module

• protected

• <package>

• private

6

Page 7: Project Jigsaw: Under The Hood

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |

‘public’ no longer means “accessible”.

7

Page 8: Project Jigsaw: Under The Hood

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |

The result:

8

Page 9: Project Jigsaw: Under The Hood

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |

Accessibility and Module Declarations

9

// src/java.sql/module-info.java

module java.sql {

exports java.sql;

exports javax.sql;

exports javax.transaction.xa;

}

Page 10: Project Jigsaw: Under The Hood

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |

ClassLoader1

class P.C class Q.D accesses

Accessibility and Class Loaders

delegates ClassLoader2

class Q.D

ClassLoader1

class P.C class Q.D no access

no delegation ClassLoader2

class Q.D

10

Page 11: Project Jigsaw: Under The Hood

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |

ClassLoader1

class P.C class Q.D accesses

Accessibility and Class Loaders

delegates ClassLoader2

class Q.D

ClassLoader1

class P.C class Q.D no access

no delegation ClassLoader2

class Q.D

11

Page 12: Project Jigsaw: Under The Hood

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |

ClassLoader1

Module X

One Class Loader, Many Modules

12

Module Y

class Q.D accesses?

class P.C

Page 13: Project Jigsaw: Under The Hood

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |

The Role of Readability

13

ClassLoader1

Module X Module Y

class Q.D accesses

class P.C

reads

Page 14: Project Jigsaw: Under The Hood

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |

The Role of Readability

14

module X {

requires Y;

}

module Y {

exports Q;

}

Module X Module Y

exports to

reads

Page 15: Project Jigsaw: Under The Hood

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |

Readability in the Java SE module graph

module java.sql {

requires java.logging;

exports java.sql;

}

module java.logging {

exports java.util.logging;

}

package java.util.logging;

public class Logger {

...

}

package java.sql;

import java.util.logging.Logger;

public class DriverManager {

new Logger() {..} }

15

Page 16: Project Jigsaw: Under The Hood

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |

Readability in the Java SE module graph

module java.sql {

requires java.logging;

exports java.sql;

}

module java.logging {

exports java.util.logging;

}

package java.util.logging;

public class Logger {

...

}

package java.sql;

import java.util.logging.Logger;

public interface Driver {

Logger getParentLogger(); }

16

Page 17: Project Jigsaw: Under The Hood

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |

Readability in the Java SE module graph

module java.sql {

requires java.logging;

exports java.sql;

}

module java.logging {

exports java.util.logging;

}

module myApp {

requires java.sql;

requires java.logging;

}

17

Page 18: Project Jigsaw: Under The Hood

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |

Readability in the Java SE module graph

module java.sql {

requires public java.logging;

exports java.sql;

}

module java.logging {

exports java.util.logging;

}

module myApp {

requires java.sql;

requires java.logging;

}

18

Page 19: Project Jigsaw: Under The Hood

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |

Readability in the Java SE module graph

module java.sql {

requires public java.logging;

requires public java.sql.time;

}

module java.logging {

exports java.util.logging;

}

module myApp {

requires java.sql;

}

module java.sql.time {

exports java.sql.time;

}

19

Page 20: Project Jigsaw: Under The Hood

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |

Direct and implied readability

• X reads Y if:

– X requires Y

or

– X reads Q, and Q requires public Y

Module X

requires Y

Module Y reads

Module X

requires Q

Module Q

requires public Y

reads Module Y

reads

reads

20

Page 21: Project Jigsaw: Under The Hood

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | 21

void doSomething(Class<?> c) {

Method[] ms = c.getDeclaredMethods();

ms[0].invoke(…);

}

Core Reflection

Page 22: Project Jigsaw: Under The Hood

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | 22

Core Reflection

setAccessible(true)

Page 23: Project Jigsaw: Under The Hood

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |

Summary of Part I: Accessibility and Readability

• Accessibility used to be a simple check for ‘public’ or “same package”.

• In Java SE 9, accessibility strongly encapsulates module internals.

• Accessibility relies on readability, which can be direct or implied.

• Accessibility is enforced by the compiler, VM, and Core Reflection.

23

Page 24: Project Jigsaw: Under The Hood

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |

Part II: Different Kinds of Modules

24

Page 25: Project Jigsaw: Under The Hood

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |

Named Modules

25

java.base java.sql

jdk.compiler jdk.javadoc

Named modules

guava.jar junit.jar

glassfish.jar hibernate.jar

classpath

Page 26: Project Jigsaw: Under The Hood

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |

Unnamed module

The Unnamed Module

26

java.base java.sql

jdk.compiler jdk.javadoc

Named modules

guava.jar junit.jar

glassfish.jar hibernate.jar

Page 27: Project Jigsaw: Under The Hood

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |

Unnamed module

The Unnamed Module

27

java.base java.sql

jdk.compiler jdk.javadoc

Named modules

guava.jar junit.jar

glassfish.jar hibernate.jar

Page 28: Project Jigsaw: Under The Hood

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |

Automatic Modules

28

Unnamed module

java.base java.sql

jdk.compiler jdk.javadoc

Named modules

guava junit.jar

glassfish.jar hibernate.jar

Page 29: Project Jigsaw: Under The Hood

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |

Automatic Modules

29

Unnamed module

java.base java.sql

jdk.compiler jdk.javadoc

Named modules

guava junit.jar

glassfish.jar hibernate.jar

Page 30: Project Jigsaw: Under The Hood

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |

Automatic Modules

30

Unnamed module

java.base java.sql

jdk.compiler jdk.javadoc

Named modules

guava junit.jar

glassfish.jar hibernate.jar

javafx.core

Page 31: Project Jigsaw: Under The Hood

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |

Multiple Automatic Modules

31

Unnamed module

java.base java.sql

jdk.compiler jdk.javadoc

Named modules

guava junit.jar

glassfish.jar

hibernate

Page 32: Project Jigsaw: Under The Hood

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |

Multiple Automatic Modules

Unnamed module

java.base java.sql

jdk.compiler jdk.javadoc

Named modules

guava junit.jar

glassfish.jar

hibernate

32

Page 33: Project Jigsaw: Under The Hood

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |

Summary of Part II: Different Kinds of Modules

• Explicit named modules (java.sql)

• Automatic named modules (guava)

• Unnamed module (a.k.a. classpath)

• Lots of readability “for free” to help with migration.

33

Page 34: Project Jigsaw: Under The Hood

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |

Part III: Loaders and Layers

34

Page 35: Project Jigsaw: Under The Hood

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |

Class loading doesn’t change.

35

Page 36: Project Jigsaw: Under The Hood

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |

Application loader Extension loader Bootstrap loader

Class Loaders in the JDK

Java Platform Module System

java.base java.logging java.corba java.transaction jdk.compiler guava

Java Virtual Machine

36

Page 37: Project Jigsaw: Under The Hood

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |

Boot layer

Layers

37

Application loader Extension loader Bootstrap loader

Java Platform Module System

java.base java.logging java.corba java.transaction jdk.compiler guava

Java Virtual Machine

Page 38: Project Jigsaw: Under The Hood

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |

Layer creation

String moduleName -> {

switch (moduleName) {

case “java.base”:

case “java.logging”:

return BOOTSTRAP_LDR;

default:

return APP_LDR;

}

}

(1) (2)

38

Page 39: Project Jigsaw: Under The Hood

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |

Boot layer

Layers and the VM

39

Application loader Extension loader Bootstrap loader

Java Platform Module System

java.base java.logging java.corba java.transaction jdk.compiler guava

Java Virtual Machine

java.base java.logging java.corba java.transaction jdk.compiler guava

Page 40: Project Jigsaw: Under The Hood

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | 40

Well-formed graphs

Page 41: Project Jigsaw: Under The Hood

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |

Well-formed graphs

“Excessive dependencies are bad. But,

cyclic dependencies are especially bad.”

41

Page 42: Project Jigsaw: Under The Hood

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |

Well-formed graphs

“Generally speaking, cycles are always bad!

However, some cycles are worse than others.

Cycles among classes are tolerable, assuming

they don’t cause cycles among the packages or

modules containing them.

Cycles among packages may also be tolerable,

assuming they don’t cause cycles among the

modules containing them.

Module relationships must never be cyclic.”

42

Page 43: Project Jigsaw: Under The Hood

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |

Well-formed graphs

• A module may read at most one module that exports a package called P.

43

Module X

requires Y requires Z

Module Y

exports P

reads

Module Z

exports P

reads

Page 44: Project Jigsaw: Under The Hood

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |

Well-formed maps

• Different modules with the same package map to different loaders.

• (Loader delegation respects module readability.)

44

String moduleName -> {

switch (moduleName) {

case “java.base”:

case “java.logging”:

return BOOTSTRAP_LDR;

default:

return APP_LDR;

}

}

Page 45: Project Jigsaw: Under The Hood

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |

ClassLoader2

Loaders and Readability

45

ClassLoader1

Module X Module Y

class Q.D accesses

class P.C

reads

delegates

Page 46: Project Jigsaw: Under The Hood

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |

Example: DOM APIs

46

org.w3c.dom.css

org.w3c.dom.stylesheets

org.w3c.dom.html

org.w3c.dom.xpath

Page 47: Project Jigsaw: Under The Hood

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |

Example: DOM APIs

47

Page 48: Project Jigsaw: Under The Hood

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | 48

Example: DOM APIs

module jdk.xml.dom {

requires public java.xml;

exports org.w3c.dom.css;

exports org.w3c.dom.html;

exports org.w3c.dom.stylesheets;

exports org.w3c.dom.xpath;

}

delegates

Bootstrap loader

java.xml

class javax.xml…

Extension loader

jdk.xml.dom

interface org.w3c.dom.css....

reads

Page 49: Project Jigsaw: Under The Hood

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |

Example: DOM APIs

delegates

Bootstrap loader

java.xml

class javax.xml…

Extension loader

jdk.xml.dom

interface org.w3c.dom.css....

reads

jdk.plugin

class com.sun…

reads

delegates?

49

Page 50: Project Jigsaw: Under The Hood

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |

Loader delegation respects module readability

50

ClassLoader2 ClassLoader1

Module X

requires Y requires Z

Module Y

exports P

reads

delegates

ClassLoader3

Module Z

exports P

reads

delegates

Page 51: Project Jigsaw: Under The Hood

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |

Split packages (missing class)

51

module java.X {

exports javax.annotation;

}

javax/annotation/MyAnno1.class

javax/annotation/MyAnno2.class

Module java.X JAR file Y

Page 52: Project Jigsaw: Under The Hood

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |

Split packages (missing class)

52

Unnamed module

java.base java.sql

jdk.compiler java.annotations.common

Named modules

guava.jar

jsr305.jar

Page 53: Project Jigsaw: Under The Hood

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |

Split packages (duplicate class)

53

Unnamed module

java.base

java.xml

Named modules

xerces.jar

Page 54: Project Jigsaw: Under The Hood

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |

Layers of layers

54

Boot layer

Application loader Extension loader Bootstrap loader

Java Platform Module System

Java Virtual Machine

Page 55: Project Jigsaw: Under The Hood

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |

Layers and Versions

55

Boot layer

Application loader Extension loader Bootstrap loader

Java Platform Module System

java.base java.logging java.corba java.transaction myapp mylib

Java Virtual Machine

Hadoop layer

Loader 16

guava@11 hadoop

JavaScript layer

Loader 23

guava@18 closure- compiler

Loader 17

jackson@1

Loader 24

jackson@2

Page 56: Project Jigsaw: Under The Hood

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |

Summary of Part III: Loaders and Layers

• Modules do a better job of encapsulation than class loaders, but class loaders are still necessary.

• Layers control the relationship between modules and class loaders.

• Assuming class loaders respect the module graph, the system is safe by construction – no cycles or split packages.

56

Page 57: Project Jigsaw: Under The Hood

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |

Summary of Summaries

• Strong encapsulation of modules by the compiler, VM, Core Reflection.

• Unnamed and automatic modules help with migration.

• The system is safe by construction – no cycles or split packages.

57

Page 58: Project Jigsaw: Under The Hood

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |

The module system: a seat belt, not a jetpack

58

Page 59: Project Jigsaw: Under The Hood

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |

Meta

59

Personal photo of speaker, France, 2001

Page 60: Project Jigsaw: Under The Hood

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |

What can you do to prepare for JDK 9?

• Try JDK 9 with Jigsaw – jdk9.java.net/jigsaw

• Run jdeps on your code and on your classpath.

• Subscribe to jigsaw-dev @ OpenJDK to see common problems + solutions.

60

Page 61: Project Jigsaw: Under The Hood

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |

Safe Harbor Statement

The preceding is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract. It is not a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. The development, release, and timing of any features or functionality described for Oracle’s products remains at the sole discretion of Oracle.

61

Page 62: Project Jigsaw: Under The Hood