jdk 7 and beyond - oracle...modularity project jigsaw modularity – the solution • “jar hell”...

45
<Insert Picture Here> JDK 7 and Beyond Modularity and Closure Lee Chuk Munn [email protected]

Upload: others

Post on 04-Jun-2020

11 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: JDK 7 and Beyond - Oracle...Modularity Project Jigsaw Modularity – the Solution • “JAR hell” – Eliminates class path – Package modules for automatic download & install

<Insert Picture Here>

JDK 7 and BeyondModularity and ClosureLee Chuk [email protected]

Page 2: JDK 7 and Beyond - Oracle...Modularity Project Jigsaw Modularity – the Solution • “JAR hell” – Eliminates class path – Package modules for automatic download & install

<Insert Picture Here>

Agenda

• Project Coin• Modularity• Closures

Page 3: JDK 7 and Beyond - Oracle...Modularity Project Jigsaw Modularity – the Solution • “JAR hell” – Eliminates class path – Package modules for automatic download & install

<Insert Picture Here>

Section DividerSmallSmall

(Language)(Language)ChangesChanges

Project CoinProject Coin

Page 4: JDK 7 and Beyond - Oracle...Modularity Project Jigsaw Modularity – the Solution • “JAR hell” – Eliminates class path – Package modules for automatic download & install

Better Integer Literal• Binary literals

• With underscores for clarity

int mask = 0b101010101010;

int mask = 0b1010_1010_1010;long big = 9_223_783_036_967_937L;

Page 5: JDK 7 and Beyond - Oracle...Modularity Project Jigsaw Modularity – the Solution • “JAR hell” – Eliminates class path – Package modules for automatic download & install

String Switch Statement• Today case label includes integer constants and enum

constants• Strings are constants too

Page 6: JDK 7 and Beyond - Oracle...Modularity Project Jigsaw Modularity – the Solution • “JAR hell” – Eliminates class path – Package modules for automatic download & install

Discriminating Strings Today

int monthNameToDays(String s, int year) {

if("April".equals(s) || "June".equals(s) ||"September".equals(s) ||"November".equals(s))

return 30;

if("January".equals(s) || "March".equals(s) ||"May".equals(s) || "July".equals(s) ||"August".equals(s) || "December".equals(s))

return 31;

if("February".equals(s))...

Page 7: JDK 7 and Beyond - Oracle...Modularity Project Jigsaw Modularity – the Solution • “JAR hell” – Eliminates class path – Package modules for automatic download & install

With String Switchint monthNameToDays(String s, int year) {

switch(s) {case "April": case "June":case "September": case "November":

return 30;

case "January": case "March":case "May": case "July":case "August": case "December":

return 31;

case "February":...

default:...

Page 8: JDK 7 and Beyond - Oracle...Modularity Project Jigsaw Modularity – the Solution • “JAR hell” – Eliminates class path – Package modules for automatic download & install

Inferring Types with <>• Pre generics

• With generics

• With diamond (<>) to infer the type

List strList = new ArrayList();

List<String> strList = new ArrayList<String>();

List<Map<String, List<String>> strList =new ArrayList<Map<String, List<String>>();

List<String> strList = new ArrayList<>();

List<Map<String, List<String>> strList =new ArrayList<>();

Page 9: JDK 7 and Beyond - Oracle...Modularity Project Jigsaw Modularity – the Solution • “JAR hell” – Eliminates class path – Package modules for automatic download & install

Exceptions Galoretry {

...} catch(ClassNotFoundException cnfe) {

log(cnfe);throw cnfe;

} catch(InstantiationException ie) {log(ie);throw ie;

} catch(NoSuchMethodException nsme) {log(nsme);throw nsme;

} catch(InvocationTargetException ite) {log(ite);throw ite;

}

Page 10: JDK 7 and Beyond - Oracle...Modularity Project Jigsaw Modularity – the Solution • “JAR hell” – Eliminates class path – Package modules for automatic download & install

Multi-Catch

try {// Reflective operations calling Class.forName,// Class.newInstance, Class.getMethod,// Method.invoke, etc.

} catch(final ClassNotFoundException |InstantiationException |NoSuchMethodException |InvocationTargetException e) {

log(e);throw e;

}

Page 11: JDK 7 and Beyond - Oracle...Modularity Project Jigsaw Modularity – the Solution • “JAR hell” – Eliminates class path – Package modules for automatic download & install

Copying Streams

InputStream in = new FileInputStream(src);OutputStream out = new FileOutputStream(dest);

byte[] buf = new byte[8192];int n;

while ((n = in.read(buf)) >= 0)out.write(buf, 0, n);

• How to make this piece of code robust?

Page 12: JDK 7 and Beyond - Oracle...Modularity Project Jigsaw Modularity – the Solution • “JAR hell” – Eliminates class path – Package modules for automatic download & install

InputStream in = null;OutputStream out = null;try {

in = new FileInputStream(src);} catch (FileNotFoundException fne) {

return;}try {

out = new FileOutputStream(dst);try {

byte[] buf = new byte[8192];int n;while ((n = in.read(buf)) >= 0)

out.write(buf, 0, n);} catch (IOException ioe) {

//Do something} finally {

try {out.flush();out.close();

} catch (IOException ioe) { }}

} catch (FileNoutFoundException fne) {} finally {

try {in.close();

} catch (IOException ioe) { }}

Page 13: JDK 7 and Beyond - Oracle...Modularity Project Jigsaw Modularity – the Solution • “JAR hell” – Eliminates class path – Package modules for automatic download & install

Automatic Resource Management

• API support– New interface java.lang.AutoClosable– java.io.Closable– JDBC 4.1 retrofitted to use AutoClosable

try (InputStream in = new FileInputStream(src);OutputStream out = new FileOutputStream(ds)) {byte[] buf = new byte[8192];int n;

while ((n = in.read(buf)) >= 0)out.write(buf, 0, n);

}

Page 14: JDK 7 and Beyond - Oracle...Modularity Project Jigsaw Modularity – the Solution • “JAR hell” – Eliminates class path – Package modules for automatic download & install

ModularityModularityProject JigsawProject Jigsaw

Page 15: JDK 7 and Beyond - Oracle...Modularity Project Jigsaw Modularity – the Solution • “JAR hell” – Eliminates class path – Package modules for automatic download & install

Modularity – the Solution• “JAR hell”

– Eliminates class path– Package modules for automatic download & install– Generate native packages – deb, rpm, ips, etc

• Performance – download time, startup time– Incremental download → fast classloading– Remembering startup classes

• Platform scalability – down to small devices– SE subset can fit into small devices

Page 16: JDK 7 and Beyond - Oracle...Modularity Project Jigsaw Modularity – the Solution • “JAR hell” – Eliminates class path – Package modules for automatic download & install

Modularity Goals• Grouping• Dependencies• Versioning• Encapsulation• Optional modules• Virtual modules

Page 17: JDK 7 and Beyond - Oracle...Modularity Project Jigsaw Modularity – the Solution • “JAR hell” – Eliminates class path – Package modules for automatic download & install

Packages

package com.foo.app

public class Main {

public class Other {

package com.foo.app.ui

public class Shell {

com.foo.app.Main

com.foo.app.Other

com.foo.app.ui.Shell

com.foo.app

Page 18: JDK 7 and Beyond - Oracle...Modularity Project Jigsaw Modularity – the Solution • “JAR hell” – Eliminates class path – Package modules for automatic download & install

module-info.java

module com.foo.app @ 1.0.0 {class com.foo.app.Mainprovides com.foo.app.lib @ 1.0.0;

}

Entry point

Module name Version

Virtual module

com.foo.app.Main

com.foo.app.Other

com.foo.app.ui.Shell

com.foo.app

Page 19: JDK 7 and Beyond - Oracle...Modularity Project Jigsaw Modularity – the Solution • “JAR hell” – Eliminates class path – Package modules for automatic download & install

module-info.java

com.foo.app

org.bar.lib

com.foo.lib

edu.baz.util

module com.foo.app @ 1.0.0 {class com.foo.app.Mainrequires com.foo.lib @ 2.1-alpha;provides com.foo.app.lib @ 1.0.0;

}

Dependency

import com.foo.lib.*;public class Main {

Page 20: JDK 7 and Beyond - Oracle...Modularity Project Jigsaw Modularity – the Solution • “JAR hell” – Eliminates class path – Package modules for automatic download & install

module-info.java

com.foo.app

org.bar.lib

com.foo.lib

edu.baz.util

com.foo.extra

Optional modules

module com.foo @ 1.0.0 {class com.foo.app.Mainrequires com.foo.lib @ 2.1-alpha;provides com.foo.app.lib @ 1.0.0;requires optional com.foo.extra;

}

Page 21: JDK 7 and Beyond - Oracle...Modularity Project Jigsaw Modularity – the Solution • “JAR hell” – Eliminates class path – Package modules for automatic download & install

module-info.java

com.foo.app

com.foo.secret

org.bar.lib

com.foo.lib

edu.baz.util

com.foo.extramodule com.foo.secret @ 1 {permits com.foo.lib;

}

Encapsulating modules

Page 22: JDK 7 and Beyond - Oracle...Modularity Project Jigsaw Modularity – the Solution • “JAR hell” – Eliminates class path – Package modules for automatic download & install

Packaging

$ javac -modulepath mods src/com.foo.app/...

$ ls mods com.foo.app/com.foo.extra/com.foo.lib/

Page 23: JDK 7 and Beyond - Oracle...Modularity Project Jigsaw Modularity – the Solution • “JAR hell” – Eliminates class path – Package modules for automatic download & install

Packaging

$ javac -modulepath mods src/com.foo.app/...

$ ls mods com.foo.app/com.foo.extra/com.foo.lib/

$ ls -R mods/com.foo.appmods/com.foo.app/com/foo/app/Main.classmods/com.foo.app/com/foo/app/Other.classmods/com.foo.app/com/foo/ui/Shell.class

Page 24: JDK 7 and Beyond - Oracle...Modularity Project Jigsaw Modularity – the Solution • “JAR hell” – Eliminates class path – Package modules for automatic download & install

Packaging

$ javac -modulepath mods src/com.foo.app/...

$ ls mods com.foo.app/com.foo.extra/com.foo.lib/

$ jpkg -modulepath mods jmod \com.foo.app com.foo.extra com.foo.lib

$ ls *[email protected]@[email protected]

Page 25: JDK 7 and Beyond - Oracle...Modularity Project Jigsaw Modularity – the Solution • “JAR hell” – Eliminates class path – Package modules for automatic download & install

Library

$ ls *[email protected]@[email protected]

$ jmod -L mlib create

./mlib

Page 26: JDK 7 and Beyond - Oracle...Modularity Project Jigsaw Modularity – the Solution • “JAR hell” – Eliminates class path – Package modules for automatic download & install

Library

org.bar.lib

edu.baz.util

./mlib$ ls *[email protected]@[email protected]

$ jmod -L mlib install \ $EXT/edu.baz.util@*.jmod \ $EXT/org.bar.lib@*.jmod

Page 27: JDK 7 and Beyond - Oracle...Modularity Project Jigsaw Modularity – the Solution • “JAR hell” – Eliminates class path – Package modules for automatic download & install

Library

$ ls *[email protected]@[email protected]

$ jmod -L mlib install \ *.jmod

$ jmod -L mlib lscom.foo.app @ 1.0.0com.foo.extra @ 0.9acom.foo.lib @ 1.0.2edu.baz.util @ 5.2_11org.bar.lib @ 2.1-alpha

com.foo.app

org.bar.lib

com.foo.lib

edu.baz.util

com.foo.extra

./mlib

Page 28: JDK 7 and Beyond - Oracle...Modularity Project Jigsaw Modularity – the Solution • “JAR hell” – Eliminates class path – Package modules for automatic download & install

Execution

$ java -L mlib \ -m com.foo.app

Welcome to Foo...

com.foo.app

org.bar.lib

com.foo.lib

edu.baz.util

com.foo.extra

./mlib

Page 29: JDK 7 and Beyond - Oracle...Modularity Project Jigsaw Modularity – the Solution • “JAR hell” – Eliminates class path – Package modules for automatic download & install

Library Delegation

ORACLE CONFIDENTIAL

com.foo.app

org.bar.lib

com.foo.lib

edu.baz.util

com.foo.extra

jdk.base

jdk.boot

jdk.desktop

jdk.xml

$JRE/lib/modules

./mlib

Page 30: JDK 7 and Beyond - Oracle...Modularity Project Jigsaw Modularity – the Solution • “JAR hell” – Eliminates class path – Package modules for automatic download & install

Repositories

ORACLE CONFIDENTIAL

jdk.base

jdk.boot

jdk.desktop

jdk.xml

$JRE/lib/modules

http://jig.sfbay/linux/x86

[email protected]

[email protected]

[email protected]

...

$ jmod add-repo http://jig.sfbay

$ jmod install jdk.toolsDownloading jdk.tools@7-ea ...Configuring jdk.tools@7-ea ...

Page 32: JDK 7 and Beyond - Oracle...Modularity Project Jigsaw Modularity – the Solution • “JAR hell” – Eliminates class path – Package modules for automatic download & install

Native Packages

[email protected]

[email protected]

$ jpkg -m mods deb com.foo.app com.foo.lib

com.foo.app-1.0.deb

com.foo.lib-3.22.deb

Requires: com.foo.lib >= 3.0

Page 33: JDK 7 and Beyond - Oracle...Modularity Project Jigsaw Modularity – the Solution • “JAR hell” – Eliminates class path – Package modules for automatic download & install

<Insert Picture Here>

Section Divider

ClosureClosureProject LambdaProject Lambda

Page 34: JDK 7 and Beyond - Oracle...Modularity Project Jigsaw Modularity – the Solution • “JAR hell” – Eliminates class path – Package modules for automatic download & install

Motivation for Lambda Project• Moore's law, multi core chip• Library is one of Java's key strength

– Better libraries are key to making parallelization better

• Higher level operations tend to improve readability of code as well as performance

• Without more language support for parallel idioms, people will instinctively reach for serial idioms

Page 35: JDK 7 and Beyond - Oracle...Modularity Project Jigsaw Modularity – the Solution • “JAR hell” – Eliminates class path – Package modules for automatic download & install

For loop – External Iteration

• Code is inherently serial– Stateful – use of > and highestScore– Client (for loop) of students determines iteration mechanism

• Serial – from first to last – no requirement for ordering

Collection<Student> students = ...

double highestScore = 0.0;for (Student s : students) {

if ((s.gradYear == 2010) && (s.score > highestScore))

highestScore = s.score;}

Page 36: JDK 7 and Beyond - Oracle...Modularity Project Jigsaw Modularity – the Solution • “JAR hell” – Eliminates class path – Package modules for automatic download & install

Hypothetical Collection Methods

• Assume collection has filter, map, reduce, max, etc.

• Not inherently serial– students traversal not determined by developer

• Anonymous inner class!– Too much boiler plate code

double highestScore = students.filter(new Predicate<Student>() {

public boolean isTrue(Student s) {return s.gradYear == 2010;

}}).map(new Extractor<Student,Double>() {

public Double extract(Student s) {return s.score;

}}).max();

Page 37: JDK 7 and Beyond - Oracle...Modularity Project Jigsaw Modularity – the Solution • “JAR hell” – Eliminates class path – Package modules for automatic download & install

Introducing Lambda Expressions

• Lambda expressions introduced with #– Signal to the JVM to defer execution of the code

• Zero or more formal parameters– Like methods

• Body may be an expression or statement– If expression, no need 'return' or ';'– Unlike methods

double highestScore = students.filter(#{ Student s -> s.gradYear == 2010 }).map(#{ Student s -> s.score }).max();

Page 38: JDK 7 and Beyond - Oracle...Modularity Project Jigsaw Modularity – the Solution • “JAR hell” – Eliminates class path – Package modules for automatic download & install

Lambda Expression

#{ Student s -> s.gradYear == 2010 }

Denotes lambda expression

Body

Formal parameter(s)

Page 39: JDK 7 and Beyond - Oracle...Modularity Project Jigsaw Modularity – the Solution • “JAR hell” – Eliminates class path – Package modules for automatic download & install

Lambda Expressions – Internal Iteration

• Cleaner syntax• Potentially faster because collection (implementation)

determines how to iterate– Opportunities for lazy evaluation– Opportunities for parallelism

double highestScore = students.filter(#{ Student s -> s.gradYear == 2010 }).map(#{ Student s -> s.score }).max();

Page 40: JDK 7 and Beyond - Oracle...Modularity Project Jigsaw Modularity – the Solution • “JAR hell” – Eliminates class path – Package modules for automatic download & install

Lambda Expression Type

• A function type from Student to boolean• Java does not have function types

– But have single method interfaces (or abstract classes)

– “Function type” from T to int

#{ Student s → s.gradYear == 2010 }

public interface Comparable<T> {public int compareTo(T object);

}

Page 41: JDK 7 and Beyond - Oracle...Modularity Project Jigsaw Modularity – the Solution • “JAR hell” – Eliminates class path – Package modules for automatic download & install

SAM Types• SAM type is an interface or abstract class with a

Single Abstract Method– Examples of existing SAM type in JDK

• No special syntax to declare SAM type

interface Runnable { void run(); }interface Callable<T> { T call(); }interface ActionListener {

void actionPerformed(ActionEvent evt); }interface Comparable<T> {

int compareTo(T obj); }

Page 42: JDK 7 and Beyond - Oracle...Modularity Project Jigsaw Modularity – the Solution • “JAR hell” – Eliminates class path – Package modules for automatic download & install

SAM Conversion• Infers a SAM type from a lambda expression

– Same parameter types and arity as SAM type's method– Return type compatible with SAM type's method– Checked exceptions compatible with SAM type's method

• SAM type's method name is not relevant– Almost like duck typing

ActionListener listener = #{ ActionEvent aEvt ->System.out.println(“hello world”); });

//void run();Runnable run = #{ ->

System.out.println(“hello world”); });//void dispatch();ActiveEvent event = #{ ->

System.out.println(“hello world”); });

Page 43: JDK 7 and Beyond - Oracle...Modularity Project Jigsaw Modularity – the Solution • “JAR hell” – Eliminates class path – Package modules for automatic download & install

Benefits of SAM Type• Target typing – inferring types from SAM type's

method– Can omit formal parameter type

• Compatibility with existing libraries

• No special method to invoke lambda expression– Just call the SAM type's method

button.addActionListener(#{ aEvt ->System.out.println(“hello”); });

ActionListener listener = #{ ... };listener.actionPerformed(new ActionEvent( ... ));

button.addActionListener(#{ aEvt ->System.out.println(“hello”); });

Page 44: JDK 7 and Beyond - Oracle...Modularity Project Jigsaw Modularity – the Solution • “JAR hell” – Eliminates class path – Package modules for automatic download & install
Page 45: JDK 7 and Beyond - Oracle...Modularity Project Jigsaw Modularity – the Solution • “JAR hell” – Eliminates class path – Package modules for automatic download & install

We encourage you to use the newly minted corporate tagline“Software. Hardware. Complete.” at the end of all your presentations.This message should replace any reference to our previous corporate tagline “Oracle Is the Information Company.”