+ All Categories
Home > Software > OpenJDK for the a'N'droidlings

OpenJDK for the a'N'droidlings

Date post: 12-Apr-2017
Category:
Upload: murat-yener
View: 143 times
Download: 0 times
Share this document with a friend
39
OpenJDK for a’N’droidlings Murat Yener @yenerm
Transcript
Page 1: OpenJDK for the a'N'droidlings

OpenJDK for a’N’droidlingsMurat Yener

@yenerm

Page 2: OpenJDK for the a'N'droidlings

who am I?Java, Web, Android

Android @Intel

Speaker (Devoxx, JavaOne, DroidCon..)

GDG Organizer

Book Author (Java EE DP, Android Studio)

GDE on Android & Java Champion

Page 3: OpenJDK for the a'N'droidlings

what the JDK?

Android is Java

but.. not on JRE

based on Apache Harmony

Object.java Object.class classes.dx

Page 4: OpenJDK for the a'N'droidlings

Java7? Java8?

but Harmony is Java 6??

Java 7 introduces changes to JVM! (JSR-292)

Java 8 introduces language features based on…

Page 5: OpenJDK for the a'N'droidlings

Java 8 Language FeaturesDefault and Static Interface methods

Lambda Expressions

Repeatable Annotations

Method References

Type Annotations

https://developer.android.com/guide/platform/j8-jack.html

Page 6: OpenJDK for the a'N'droidlings

Default Methodspublic interface TimeClient { void setTime(int hour, int minute, int second); void setDate(int day, int month, int year); void setDateAndTime(int day, int month, int year, int hour, int minute, int second); LocalDateTime getLocalDateTime(); static ZoneId getZoneId (String zoneString) { try { return ZoneId.of(zoneString); } catch (DateTimeException e) { return ZoneId.systemDefault(); } } default ZonedDateTime getZonedDateTime(String zoneString) { return ZonedDateTime.of(getLocalDateTime(), getZoneId(zoneString)); } }

Page 7: OpenJDK for the a'N'droidlings

Default Methodspublic interface TimeClient { void setTime(int hour, int minute, int second); void setDate(int day, int month, int year); void setDateAndTime(int day, int month, int year, int hour, int minute, int second); LocalDateTime getLocalDateTime(); static ZoneId getZoneId (String zoneString) { try { return ZoneId.of(zoneString); } catch (DateTimeException e) { return ZoneId.systemDefault(); } } default ZonedDateTime getZonedDateTime(String zoneString) { return ZonedDateTime.of(getLocalDateTime(), getZoneId(zoneString)); } }

Page 8: OpenJDK for the a'N'droidlings

Default Methodspublic interface TimeClient { void setTime(int hour, int minute, int second); void setDate(int day, int month, int year); void setDateAndTime(int day, int month, int year, int hour, int minute, int second); LocalDateTime getLocalDateTime(); static ZoneId getZoneId (String zoneString) { try { return ZoneId.of(zoneString); } catch (DateTimeException e) { return ZoneId.systemDefault(); } } default ZonedDateTime getZonedDateTime(String zoneString) { return ZonedDateTime.of(getLocalDateTime(), getZoneId(zoneString)); } } https://docs.oracle.com/javase/tutorial/java/IandI/defaultmethods.html

Page 9: OpenJDK for the a'N'droidlings

Lambdas

JButton testButton = new JButton("Test Button"); testButton.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent e){ System.out.println("Click Detected by Anon Class"); } });

Page 10: OpenJDK for the a'N'droidlings

Lambdas

JButton testButton = new JButton("Test Button"); testButton.addActionListener(e -> System.out.println("Click Detected by Lambda Listner"));

http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/Lambda-QuickStart/index.html

Page 11: OpenJDK for the a'N'droidlings

Lambdas with Streamsroster .stream() .filter( p -> p.getGender() == Person.Sex.MALE && p.getAge() >= 18 && p.getAge() <= 25) .map(p -> p.getEmailAddress()) .forEach(email -> System.out.println(email));

Page 12: OpenJDK for the a'N'droidlings

Lambdas with Stream

Q: print emails of male persons between 18-25 years old from

a source of objects…

Page 13: OpenJDK for the a'N'droidlings

Lambdas with Streampublic class Person { public enum Sex { MALE, FEMALE } String name; LocalDate birthday; Sex gender; String emailAddress;

public int getAge() {} public void printPerson() {} }

Q: print emails of male persons between 18-25 years old from

a source of objects…

Page 14: OpenJDK for the a'N'droidlings

Lambdas with Streamsroster .stream() .filter( p -> p.getGender() == Person.Sex.MALE && p.getAge() >= 18 && p.getAge() <= 25) .map(p -> p.getEmailAddress()) .forEach(email -> System.out.println(email));

Page 15: OpenJDK for the a'N'droidlings

Lambdas with Streamsroster .stream() .filter( p -> p.getGender() == Person.Sex.MALE && p.getAge() >= 18 && p.getAge() <= 25) .map(p -> p.getEmailAddress()) .forEach(email -> System.out.println(email));

Page 16: OpenJDK for the a'N'droidlings

Lambdas with Streamsroster .stream() .filter( p -> p.getGender() == Person.Sex.MALE && p.getAge() >= 18 && p.getAge() <= 25) .map(p -> p.getEmailAddress()) .forEach(email -> System.out.println(email));

Page 17: OpenJDK for the a'N'droidlings

Lambdas with Streamsroster .stream() .filter( p -> p.getGender() == Person.Sex.MALE && p.getAge() >= 18 && p.getAge() <= 25) .map(p -> p.getEmailAddress()) .forEach(email -> System.out.println(email));

https://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html

Page 18: OpenJDK for the a'N'droidlings

Method References

Lambdas are great for anon methods…

but what if the method already exists..

Page 19: OpenJDK for the a'N'droidlings

Method Referencespublic class Person { public enum Sex { MALE, FEMALE } String name; LocalDate birthday; Sex gender; String emailAddress;

}

Page 20: OpenJDK for the a'N'droidlings

Method Referencespublic class Person { public enum Sex { MALE, FEMALE } String name; LocalDate birthday; Sex gender; String emailAddress;

}

Array.sort(personArray, ???)

Page 21: OpenJDK for the a'N'droidlings

Method Referencespublic class Person { public enum Sex { MALE, FEMALE } String name; LocalDate birthday; Sex gender; String emailAddress;

}

class PersonAgeComparator implements Comparator<Person> { public int compare(Person a, Person b) { return a.getBirthday().compareTo(b.getBirthday()); } }

Page 22: OpenJDK for the a'N'droidlings

Method Referencespublic class Person { public enum Sex { MALE, FEMALE } String name; LocalDate birthday; Sex gender; String emailAddress;

}

class PersonAgeComparator implements Comparator<Person> { public int compare(Person a, Person b) { return a.getBirthday().compareTo(b.getBirthday()); } }

Arrays.sort(rosterAsArray, new PersonAgeComparator());

Page 23: OpenJDK for the a'N'droidlings

Method Referencespublic class Person { public enum Sex { MALE, FEMALE } String name; LocalDate birthday; Sex gender; String emailAddress;

}

class PersonAgeComparator implements Comparator<Person> { public int compare(Person a, Person b) { return a.getBirthday().compareTo(b.getBirthday()); } }

Arrays.sort(rosterAsArray, (Person a, Person b) -> { return a.getBirthday().compareTo(b.getBirthday()); } );

Page 24: OpenJDK for the a'N'droidlings

Method Referencespublic class Person { public enum Sex { MALE, FEMALE } String name; LocalDate birthday; Sex gender; String emailAddress;

public static int compareByAge(Person a, Person b) { return a.birthday.compareTo(b.birthday); } }

Page 25: OpenJDK for the a'N'droidlings

Method Referencespublic class Person { public enum Sex { MALE, FEMALE } String name; LocalDate birthday; Sex gender; String emailAddress;

public static int compareByAge(Person a, Person b) { return a.birthday.compareTo(b.birthday); } }

Arrays.sort(rosterAsArray, compareByAge);

Page 26: OpenJDK for the a'N'droidlings

Method Referencespublic class Person { public enum Sex { MALE, FEMALE } String name; LocalDate birthday; Sex gender; String emailAddress;

public static int compareByAge(Person a, Person b) { return a.birthday.compareTo(b.birthday); } }

Arrays.sort(rosterAsArray, Person::compareByAge);

https://docs.oracle.com/javase/tutorial/java/javaOO/methodreferences.html

Page 27: OpenJDK for the a'N'droidlings

Repeatable Annotations

public @interface Schedule { String dayOfMonth() default "first"; String dayOfWeek() default "Mon"; int hour() default 12; }

@Schedule(dayOfMonth=“last”, dayOfWeek="Fri", hour="23") public void doPeriodicCleanup() { ... }

Page 28: OpenJDK for the a'N'droidlings

Repeatable Annotations@Repeatable(Schedules.class) public @interface Schedule { String dayOfMonth() default "first"; String dayOfWeek() default "Mon"; int hour() default 12; }

@Schedule(dayOfMonth=“last”) @Schedule(dayOfWeek="Fri", hour="23") public void doPeriodicCleanup() { ... }

public @interface Schedules { Schedule[] value(); }

https://docs.oracle.com/javase/tutorial/java/annotations/repeating.html

Page 29: OpenJDK for the a'N'droidlings

Type Annotations

Pre Java 8, annotations could only be applied to declarations

With Java 8, annotations can also be applied to any type use

@NonNull String str;

https://docs.oracle.com/javase/tutorial/java/annotations/type_annotations.html

Page 30: OpenJDK for the a'N'droidlings

how is this possible?!?

Page 31: OpenJDK for the a'N'droidlings

JSR-292

introduced with Java 7

new instruction on JVM! invokedynamic

new Java API (java.lang.invoke)

new classes (MethodType, MethodHandle…)

Page 32: OpenJDK for the a'N'droidlings

Invoking methods - reflectionObject str, Object from, Object to; Method m = str.getClass().getMethod(“replace”, from.getClass, to.getClass));

m.invoke(str, from, to);

Page 33: OpenJDK for the a'N'droidlings

Invoking methods - invokeExactObject str, Object from, Object to; Method m = str.getClass().getMethod(“replace”, from.getClass, to.getClass));

MethodHandles.Lookup lookup = MethodHandles.lookup(); MethodHandle mh = lookup.unreflect(method); mh.asType(mt.generic());

mh.invokeExact(str, from, to);

Page 34: OpenJDK for the a'N'droidlings

Invoking methods - invokeGenericObject str, Object from, Object to; Method m = str.getClass().getMethod(“replace”, from.getClass, to.getClass));

MethodHandles.Lookup lookup = MethodHandles.lookup(); MethodHandle mh = lookup.unreflect(method); mh.asType(mt.generic());

mh.invokeGeneric(str, from, to);

https://www.youtube.com/watch?v=STDBY5kT4_M

Page 35: OpenJDK for the a'N'droidlings

Invoke Dynamic

provides linking at runtime

directly translates to VM

used in lambdas*

Page 36: OpenJDK for the a'N'droidlings

Jack & Jill

enable Java 8 with Jack

Jack shrinks, obfuscates, repackages, multidex

Jill translates jar files to Jack

Page 37: OpenJDK for the a'N'droidlings

Jack

javac: java to class dx: class to dex

Jack: java to jack to dex

Jack can handle Lambdas… goodbye RetroLambda

Proguard included!

Page 38: OpenJDK for the a'N'droidlings

Enable Jack!android { ... defaultConfig { ... jackOptions { enabled true } } compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } }

Page 39: OpenJDK for the a'N'droidlings

</slides> <questions>

thanks!! @yenerm


Recommended