SlideShare ist ein Scribd-Unternehmen logo
1 von 47
Downloaden Sie, um offline zu lesen
8 
8 
Srikanth Sankaran 
IBM India 
Stephan Herrmann 
GK Software 
BETA_JAVA8
8 Eclipse and Java™ 8 
● Java 8 features available in early access builds: 
– JSR308 - Type Annotations. 
– JEP120 - Repeating Annotations. 
– JEP118 - Method Parameter Reflection. 
– JSR269 - Pluggable Annotation Processor API & 
javax.lang.model API enhancements for Java 8. 
– Support for “code carrying” interface methods. 
● Partial support for 
– Lambda Expressions & Method/Constructor references 
– Overload resolution & Type inference changes still evolving 
● had been blocked by incomplete specification until late September 
Srikanth Sankaran, Stephan Herrmann: Eclipse and Java 8 - EclipseCon Europe 2013 # 3
8 Eclipse and Java™ 8 
● IDE features available in early access builds: 
– Type annotations based static null analysis 
● substantially complete, open issues exist 
– Code completion, code selection/navigation, search engine 
● for completed features 
– Code formatter and code “reconciler” 
– IDE enablement support: 
● AST API's, programmatic AST rewriting API support etc 
● Full fledged IDE support work in high gear: 
– Early access build refreshes very soon. 
● JDT team: 
– 12 committers + 1 contributor + 1 GSOC student 
+ 2 past committers on consulting role. 
Java8 
Java8 
Srikanth Sankaran, Stephan Herrmann: Eclipse and Java 8 - EclipseCon Europe 2013 # 4
8 New Kinds of Methods 
● defender methods 
● virtual extension methods 
● static interface methods 
● default methods 
Srikanth Sankaran, Stephan Herrmann: Eclipse and Java 8 - EclipseCon Europe 2013 # 6
8 New Kinds of Methods 
Those are only 2 kinds :) 
● Code carrying methods in interfaces: 
– static methods 
● non-OO programming even in interfaces 
– default methods 
previous names: 
● defender methods 
● virtual extension methods 
Srikanth Sankaran, Stephan Herrmann: Eclipse and Java 8 - EclipseCon Europe 2013 # 7
8 Default Methods - Intention 
● Intention 
– support evolution of interfaces in libraries: 
add methods to an interface w/o breaking implementors 
– still legal: 
Srikanth Sankaran, Stephan Herrmann: Eclipse and Java 8 - EclipseCon Europe 2013 # 8
Srikanth Sankaran, Stephan Herrmann: Eclipse and Java 8 - EclipseCon Europe 2013 # 9 
8 
Default Methods - Consequences 
● Consequences 
– Java 8 has multiple inheritance, sort-of 
– but little help for corner cases 
Root 
m() 
Both 
Left 
D m() 
Right 
D m() 
resolve:
Srikanth Sankaran, Stephan Herrmann: Eclipse and Java 8 - EclipseCon Europe 2013 # 10 
8 
Default Methods - Consequences 
● Consequences 
– Java 8 has multiple inheritance, sort-of 
– but little help for corner cases 
Both 
Left 
D m() 
Right 
m() 
The default method m() inherited from Left conflicts 
with another method inherited from Right 
Advice: Don't!
8 Default Methods - Compatibility 
● Designed for compatibility during evolution 
● For the price of compatibility 
– Recipe for disaster: 
● implement java.util.List → compile against JRE 7 → OK 
● upgrade to JRE 8 but compile as 1.7 
The type MyList<E> must implement the inherited abstract method Collection<E>.stream() 
– Undefined compiler behavior 
● 1.7 compiler cannot handle default methods (from .class) 
● ecj and javac differ – no plans to assimilate 
● depending on compiler implementation details, not specification 
Srikanth Sankaran, Stephan Herrmann: Eclipse and Java 8 - EclipseCon Europe 2013 # 12
8 Default Methods - Compatibility 
● Designed for compatibility during evolution 
● For the price of compatibility 
– Recipe for disaster: 
● implement java.util.List → compile against JRE 7 → OK 
● upgrade to JRE 8 but compile as 1.7 
The type MyList<E> must implement the inherited abstract method Collection<E>.stream() 
– Undefined compiler behavior 
● 1.7 compiler cannot handle default methods (from .class) 
● ecj and javac differ – no plans to assimilate 
● depending on compiler implementation details, not specification 
Advice: Don't mix -source 1.7 & JRE8! 
Srikanth Sankaran, Stephan Herrmann: Eclipse and Java 8 - EclipseCon Europe 2013 # 13
Srikanth Sankaran, Stephan Herrmann: Eclipse and Java 8 - EclipseCon Europe 2013 # 14 
8 
λ 
(JSR 335)
Srikanth Sankaran, Stephan Herrmann: Eclipse and Java 8 - EclipseCon Europe 2013 # 15 
8 
Introducing Lambda Expressions 
● Lambda Expressions, Closures, Anonymous Methods 
– what is in a name ? 
● A function + “captured state” (may have non-locals) 
– accessible even when outside of defining lexical scope 
● Theoretical framework dates back several decades: 
– Lambda (1930's – Church, Lisp – 1958) 
– Closure (1960's - Landin, Scheme - 1975) 
– Common idiomatic device in functional programming. 
● Paradigm of passing a "code block as data"
Srikanth Sankaran, Stephan Herrmann: Eclipse and Java 8 - EclipseCon Europe 2013 # 16 
8 
Java 7 Style of “Code as Data” 
● Via anonymous classes
Srikanth Sankaran, Stephan Herrmann: Eclipse and Java 8 - EclipseCon Europe 2013 # 17 
8 
Java 7 Style of “Code as Data” 
● Via anonymous classes 
So if lambda expressions 
help model "code as data" 
are they just 
old wine in a new bottle? 
No!
8 Lambda Expressions 
● Eliminate thorny issues with anonymous classes 
– Verbose/clunky Syntax 
– Scoping 
● anonymous classes introduce their own scopes 
● interplay between names in enclosing scope ↔ inherited names 
– Capture 
● can only capture explicitly final outer locals 
● necessarily capture enclosing class instance (memory leaks) 
● Lambdas 
– address the above, designed to be flexible & optimizable 
– much more pleasing & efficient to program with call backs 
– With default methods, enabled Collections overhaul. 
Srikanth Sankaran, Stephan Herrmann: Eclipse and Java 8 - EclipseCon Europe 2013 # 18
8 λ Syntax and Examples 
Srikanth Sankaran, Stephan Herrmann: Eclipse and Java 8 - EclipseCon Europe 2013 # 20
8 Target typing 
● Lambda expression is basically 
– an anonymous method 
– with possibly "captured" enclosing state 
● At the grammar level, lambdas are expressions (duh!) 
– Can occur anywhere an expression can 
– Introduces severe complications in parsing. 
● Legally however, 
– Lambda needs a context that defines a "target type" 
– Everywhere else will have to rejected by the compiler. 
– Assignment, initialization, casts, method/ctor invocation, 
lambda expression body, conditional expressions and return 
statements are contexts that provide target type. 
Srikanth Sankaran, Stephan Herrmann: Eclipse and Java 8 - EclipseCon Europe 2013 # 21
8 Target Typing Examples 
Srikanth Sankaran, Stephan Herrmann: Eclipse and Java 8 - EclipseCon Europe 2013 # 22
8 Functional Interfaces 
● The target type must be a functional interface 
● Functional interface 
– Declares a single abstract method 
– Some methods don't count: 
● Default & static methods - they are not abstract 
● May freely redeclare java.lang.Object's methods 
– May be tagged with @FunctionalInterface to express intent 
● So the lambda object implements the functional interface 
● Another way of constructing objects 
– apart from the SE7 ways 
(new, reflection, serialization, clone ...) 
Srikanth Sankaran, Stephan Herrmann: Eclipse and Java 8 - EclipseCon Europe 2013 # 23
8 Lambda Syntax examples 
– Parameter types 
● explicit, or 
● inferred from target functional interface methods 
– Body 
● expression, or 
● block. 
Srikanth Sankaran, Stephan Herrmann: Eclipse and Java 8 - EclipseCon Europe 2013 # 24 
– () 
● can be omitted if a singleton parameter
8 Lambda Syntax examples 
– Parameter types 
● explicit, or 
Trade-off between 
conciseness of expression 
● inferred from target functional interface methods 
– Body 
● expression, or 
● block. 
Srikanth Sankaran, Stephan Herrmann: Eclipse and Java 8 - EclipseCon Europe 2013 # 25 
– () 
● can be omitted if a singleton parameter 
vs 
clarity of code 
- 
in the hands of the programmer
8 Variable capture 
● A lambda can refer to enclosing method's locals 
– Captured state constitutes the "referencing environment" 
– Compiler can infer finality 
– Captured state is not mutable. 
● Properly: capture of "immutable outer local's value" 
– Mutability of outer locals implies race conditions 
– Mutability would necessarily impose serial execution. 
– Lambda creation & invocation could be far apart. 
● may be in different threads, may be after enclosing method return 
● This "constraint" actually enables parallelism. 
● Enclosing class instance captured only if referred to. 
Srikanth Sankaran, Stephan Herrmann: Eclipse and Java 8 - EclipseCon Europe 2013 # 26
8 Method References 
● Similar to lambda expressions 
– Require a target type that must be a functional interface 
– Serve as instances of the functional interface 
– Rather than providing a method body, 
they refer to an existing method 
– Several variants offered. 
Srikanth Sankaran, Stephan Herrmann: Eclipse and Java 8 - EclipseCon Europe 2013 # 27
Srikanth Sankaran, Stephan Herrmann: Eclipse and Java 8 - EclipseCon Europe 2013 # 28 
8 
Lambda code generation 
● Not lowered into anonymous classes - i.e not just syntactic sugar 
● What does the following print ? 
● Lambda object creation under the hood: 
– Handled by "Lambda metafactory" API in JRE. 
– Compilers generates suitable calls to the metafactory. 
– Enables various optimizations.
8 Putting it all together 
● Here is a nice example from SOTL[*] 
[*] http://cr.openjdk.java.net/~briangoetz/lambda/lambda-state-final.html 
Srikanth Sankaran, Stephan Herrmann: Eclipse and Java 8 - EclipseCon Europe 2013 # 29
JDT features in the works 8 
Srikanth Sankaran, Stephan Herrmann: Eclipse and Java 8 - EclipseCon Europe 2013 # 30
JDT features in the works 8 
Srikanth Sankaran, Stephan Herrmann: Eclipse and Java 8 - EclipseCon Europe 2013 # 31
JDT features in the works 8 
Srikanth Sankaran, Stephan Herrmann: Eclipse and Java 8 - EclipseCon Europe 2013 # 32
JDT features in the works 8 
Srikanth Sankaran, Stephan Herrmann: Eclipse and Java 8 - EclipseCon Europe 2013 # 33
Srikanth Sankaran, Stephan Herrmann: Eclipse and Java 8 - EclipseCon Europe 2013 # 35 
8 
@ 
(JSR 308)
8 Annotations in more Places 
● Java 5: annotate declarations 
– ElementType: packages, classes, fields, methods, locals … 
● Java 8: annotate types 
– ElementType.TYPE_USE 
– ElementType.TYPE_PARAMETER 
So what? 
Srikanth Sankaran, Stephan Herrmann: Eclipse and Java 8 - EclipseCon Europe 2013 # 36
8 Why Care About Types? 
● Type = Constraints on values 
To detect anomalies 
– missing capability 
– incompatible assignment 
– undeclared capability 
● Constraint checking avoids errors 
– No Such Method / Field 
● Basic statically typed OO 
– ClassCastException 
● Generics 
– ??ExcepMtioyn pet example: NullPointerException 
Srikanth Sankaran, Stephan Herrmann: Eclipse and Java 8 - EclipseCon Europe 2013 # 37
Let the Type System 8 
Handle Nullity 
● Ideally: Java would force explicit choice 
– String definitely a String, never null 
– String? either a String or null 
– Type system ensures: no dereferencing of null 
● Nullity as a language feature? 
– Heavy weight, incompatible change 
– Language change for each new constraint? 
Srikanth Sankaran, Stephan Herrmann: Eclipse and Java 8 - EclipseCon Europe 2013 # 38
8 Pluggable Type System 
● Make it easier to add new constraints 
– Only one new syntax for all kinds of constraints 
● Make it easier to add new type checkers 
– Checker Framework (Michael Ernst – U of Washington) 
● Examples 
– @NonNull 
– @Interned equals(== , equals) 
– @Immutable value cannot change (Java 5 ?) 
– @ReadOnly value cannot change via this reference 
– @UI code requires to run on the SWT UI thread 
Srikanth Sankaran, Stephan Herrmann: Eclipse and Java 8 - EclipseCon Europe 2013 # 39
Can't Java 5 Do All This? 
@Target(ElementType.PARAMETER) 
@interface NonNull5 {} 
void java5(@NonNull5 String arg); 
arg is qualified to be non-null 
@Target(ElementType.TYPE_USE) 
@interface NonNull8 {} 
void java8(@NonNull8 String arg); 
String is qualified to be non-null 
String is qualified to be non-null 
@Target(ElementType.METHOD) 
@interface NonNull5 {} 
@NonNull5 String java5(); 
java5 is qualified to be non-null 
Srikanth Sankaran, Stephan Herrmann: Eclipse and Java 8 - EclipseCon Europe 2013 # 40 
8 
@Target(ElementType.TYPE_USE) 
@interface NonNull8 {} 
@NonNull8 String java8(); 
● We've been lying about the method result 
– but we can't lie about everything, e.g.: 
void printFirstDog(@NonNull List<Dog> dogs) { 
dogs.get(0).bark(); 
} 
NPE?
l1 cannot contain 
null elements 
Null type mismatch: required '@NonNull String' 
but the provided value is null 
Srikanth Sankaran, Stephan Herrmann: Eclipse and Java 8 - EclipseCon Europe 2013 # 41 
8 
void bad(List<String> unknown, List<@Nullable String> withNulls) { 
@NonNull List<@NonNull String> l1 = new ArrayList<>(); 
l1.add(null); 
String first = l1.get(0); 
if (first == null) return; 
l1 = unknown; 
l1 = withNulls; 
String canNull = withNulls.get(0); 
System.out.println(canNull.toUpperCase()); 
} 
@NonNull List<@Nullable String> l2 = new ArrayList<>(); 
l2 can contain 
null elements 
Annotated Generics 
void good() { 
@NonNull List<@NonNull String> l1 = new ArrayList<>(); 
l1.add("Hello"); 
for (String elem : l1) 
System.out.println(elem.toUpperCase()); 
l2.add(null); 
for (String unknown : l2) 
if (unknown != null) 
System.out.println(unknown.toUpperCase()); 
} 
Null type mismatch: required '@NonNull String' 
but the provided value is null
l1 cannot contain 
null elements 
Null comparison always yields false: 
The variable first cannot be null at this location 
Srikanth Sankaran, Stephan Herrmann: Eclipse and Java 8 - EclipseCon Europe 2013 # 42 
8 
void bad(List<String> unknown, List<@Nullable String> withNulls) { 
@NonNull List<@NonNull String> l1 = new ArrayList<>(); 
l1.add(null); 
String first = l1.get(0); 
if (first == null) return; 
l1 = unknown; 
l1 = withNulls; 
String canNull = withNulls.get(0); 
System.out.println(canNull.toUpperCase()); 
} 
@NonNull List<@Nullable String> l2 = new ArrayList<>(); 
l2 can contain 
null elements 
Annotated Generics 
void good() { 
@NonNull List<@NonNull String> l1 = new ArrayList<>(); 
l1.add("Hello"); 
for (String elem : l1) 
System.out.println(elem.toUpperCase()); 
l2.add(null); 
for (String unknown : l2) 
if (unknown != null) 
System.out.println(unknown.toUpperCase()); 
} 
Null comparison always yields false: 
The variable first cannot be null at this location
l1 cannot contain 
null elements 
Null type safety (type annotations): The expression of type 'List<String>' 
needs unchecked conversion to conform to '@NonNull List<@NonNull String>' 
Srikanth Sankaran, Stephan Herrmann: Eclipse and Java 8 - EclipseCon Europe 2013 # 43 
8 
void bad(List<String> unknown, List<@Nullable String> withNulls) { 
@NonNull List<@NonNull String> l1 = new ArrayList<>(); 
l1.add(null); 
String first = l1.get(0); 
if (first == null) return; 
l1 = unknown; 
l1 = withNulls; 
String canNull = withNulls.get(0); 
System.out.println(canNull.toUpperCase()); 
} 
@NonNull List<@Nullable String> l2 = new ArrayList<>(); 
l2 can contain 
null elements 
Annotated Generics 
void good() { 
@NonNull List<@NonNull String> l1 = new ArrayList<>(); 
l1.add("Hello"); 
for (String elem : l1) 
System.out.println(elem.toUpperCase()); 
l2.add(null); 
for (String unknown : l2) 
if (unknown != null) 
System.out.println(unknown.toUpperCase()); 
} 
Null type safety (type annotations): The expression of type 'List<String>' 
needs unchecked conversion to conform to '@NonNull List<@NonNull String>'
l1 cannot contain 
null elements 
Null type mismatch (type annotations): 
required '@NonNull List<@NonNull String>' 
but this expression has type 'List<@Nullable String>' 
Srikanth Sankaran, Stephan Herrmann: Eclipse and Java 8 - EclipseCon Europe 2013 # 44 
8 
void bad(List<String> unknown, List<@Nullable String> withNulls) { 
@NonNull List<@NonNull String> l1 = new ArrayList<>(); 
l1.add(null); 
String first = l1.get(0); 
if (first == null) return; 
l1 = unknown; 
l1 = withNulls; 
String canNull = withNulls.get(0); 
System.out.println(canNull.toUpperCase()); 
} 
@NonNull List<@Nullable String> l2 = new ArrayList<>(); 
l2 can contain 
null elements 
Annotated Generics 
void good() { 
@NonNull List<@NonNull String> l1 = new ArrayList<>(); 
l1.add("Hello"); 
for (String elem : l1) 
System.out.println(elem.toUpperCase()); 
l2.add(null); 
for (String unknown : l2) 
if (unknown != null) 
System.out.println(unknown.toUpperCase()); 
} 
Null type mismatch (type annotations): 
required '@NonNull List<@NonNull String>' 
but this expression has type 'List<@Nullable String>'
l1 cannot contain 
null elements 
Potential null pointer access: 
The variable canNull may be null at this location 
Srikanth Sankaran, Stephan Herrmann: Eclipse and Java 8 - EclipseCon Europe 2013 # 45 
8 
void bad(List<String> unknown, List<@Nullable String> withNulls) { 
@NonNull List<@NonNull String> l1 = new ArrayList<>(); 
l1.add(null); 
String first = l1.get(0); 
if (first == null) return; 
l1 = unknown; 
l1 = withNulls; 
String canNull = withNulls.get(0); 
System.out.println(canNull.toUpperCase()); 
} 
@NonNull List<@Nullable String> l2 = new ArrayList<>(); 
l2 can contain 
null elements 
Annotated Generics 
void good() { 
@NonNull List<@NonNull String> l1 = new ArrayList<>(); 
l1.add("Hello"); 
for (String elem : l1) 
System.out.println(elem.toUpperCase()); 
l2.add(null); 
for (String unknown : l2) 
if (unknown != null) 
System.out.println(unknown.toUpperCase()); 
} 
Potential null pointer access: 
The variable canNull may be null at this location
8 Framework Development (1) 
● So you want to be smart: 
public interface ILib<E> { 
@NonNull E work(@Nullable E e); 
} 
● But will clients like it? 
public class LibImpl implements ILib<String> { 
@Override 
public @Nullable String work(@NonNull String e) { return null; } 
● If thou usest annotations know thy co/contravariance 
– @Nullable parameter is irreversible 
– Design bug in guava's Predicate / Function 
Srikanth Sankaran, Stephan Herrmann: Eclipse and Java 8 - EclipseCon Europe 2013 # 47 
}
8 Framework Development (2) 
● Avoid overspecification, make interfaces null-generic: 
public interface Function<I,O> { 
O apply(I in); 
} 
● Let clients decide 
class ToString implements Function<@NonNull Object, @Nullable String> { 
@Override 
public @Nullable String apply(@NonNull Object o) { return o.toString(); } 
No NPE! 
Srikanth Sankaran, Stephan Herrmann: Eclipse and Java 8 - EclipseCon Europe 2013 # 48 
} 
● Putting the pieces together 
<@NonNull I, @Nullable O> Collection<O> map1(Collection<I> in, Function<I, O> f) { … } 
<@Nullable I, @NonNull O> Collection<O> map2(Collection<I> in, Function<I, O> f) { … } 
... 
List<@NonNull Object> in = new ArrayList<>(); 
Collection<@Nullable String> out = map1(in, new ToString());
8 Caveat: Arrays 
void test (@NonNull String [] stringsOrNulls) { 
System.out.println(stringsOrNulls[0]); 
● Semantics are changing from Java 7 to Java 8 
Srikanth Sankaran, Stephan Herrmann: Eclipse and Java 8 - EclipseCon Europe 2013 # 49 
} 
array of nonnull elements the array can still be null Þ NPE 
void test (String @NonNull [] stringsOrNulls) { 
System.out.println(stringsOrNulls[0]); 
} 
nonnull array NPE- safe (but may print “null”)
Srikanth Sankaran, Stephan Herrmann: Eclipse and Java 8 - EclipseCon Europe 2013 # 50 
8 
Status: Annotated Type Analysis 
● Implemented EA: @NonNull, @Nullable 
– Much analysis already performed 
● known & unknown bugs 
– Editor may show more bogus errors 
● Project > Clean and Problems View 
● Planned: @Uninterned 
● Proposed: @UiEffect, @Ui … 
– by [Colin S. Gordon, Werner Dietl, Michael D. Ernst, and Dan Grossman] 
– SWT: bye, bye, “Invalid thread access”
8 Status: Type Annotations 
● Supported by 
– Compiler 
– DOM AST 
– APT 
● Work in progress 
– respect during 
CCttrrll CCttrrll 1 CAtlrtl T 
● Not yet supported by 
– Java model 
Srikanth Sankaran, Stephan Herrmann: Eclipse and Java 8 - EclipseCon Europe 2013 # 51
8 TypeBinding Backstage Story 
aka “Symbol” 
● Type bindings are “interned” 
– OK to use == 
● Broken by encoding type annotations in type bindings 
● Solution 
– Find/replace == comparisons for T <: TypeBinding 
– Tweak our compiler to do this 
– Plan: publish the tweak, controlled by @Uninterned 
Srikanth Sankaran, Stephan Herrmann: Eclipse and Java 8 - EclipseCon Europe 2013 # 52
Srikanth Sankaran, Stephan Herrmann: Eclipse and Java 8 - EclipseCon Europe 2013 # 53 
8 
Dramatis personæ 
● Jay Arthanareeswaran 
● Anirban Chakarborty 
● Manoj Palat 
● Shankha Banerjee 
● Manju Mathew 
● Noopur Gupta 
● Deepak Azad 
● Srikanth Sankaran 
● Olivier Thomann 
● Andy Clement 
● Michael Rennie 
● Jesper S. Møller 
● Walter Harley 
● Stephan Herrmann 
● Dani Megert 
● Markus Keller 
● Early Access: 
– http://wiki.eclipse.org/JDT_Core/Java8 
● We invite you to test - defect reports welcome !

Weitere ähnliche Inhalte

Was ist angesagt?

Scaladays 2010 - The Scala IDE for Eclipse - Retrospect and Prospect for 2.8 ...
Scaladays 2010 - The Scala IDE for Eclipse - Retrospect and Prospect for 2.8 ...Scaladays 2010 - The Scala IDE for Eclipse - Retrospect and Prospect for 2.8 ...
Scaladays 2010 - The Scala IDE for Eclipse - Retrospect and Prospect for 2.8 ...
Miles Sabin
 
Lecture 3 java basics
Lecture 3 java basicsLecture 3 java basics
Lecture 3 java basics
the_wumberlog
 
C#3.0 & Vb 9.0 New Features
C#3.0 & Vb 9.0 New FeaturesC#3.0 & Vb 9.0 New Features
C#3.0 & Vb 9.0 New Features
techfreak
 
A-Brief-Introduction-To-JAVA8_By_Srimanta_Sahu
A-Brief-Introduction-To-JAVA8_By_Srimanta_SahuA-Brief-Introduction-To-JAVA8_By_Srimanta_Sahu
A-Brief-Introduction-To-JAVA8_By_Srimanta_Sahu
Srimanta Sahu
 

Was ist angesagt? (20)

Eclipse and Java 8 - Eclipse Day India 2013
Eclipse and Java 8 - Eclipse Day India 2013Eclipse and Java 8 - Eclipse Day India 2013
Eclipse and Java 8 - Eclipse Day India 2013
 
Scaladays 2010 - The Scala IDE for Eclipse - Retrospect and Prospect for 2.8 ...
Scaladays 2010 - The Scala IDE for Eclipse - Retrospect and Prospect for 2.8 ...Scaladays 2010 - The Scala IDE for Eclipse - Retrospect and Prospect for 2.8 ...
Scaladays 2010 - The Scala IDE for Eclipse - Retrospect and Prospect for 2.8 ...
 
Tech Days 2015: A quick tour of Ada 2012
Tech Days 2015: A quick tour of Ada 2012Tech Days 2015: A quick tour of Ada 2012
Tech Days 2015: A quick tour of Ada 2012
 
Functional Programming in Java
Functional Programming in JavaFunctional Programming in Java
Functional Programming in Java
 
java 8 new features
java 8 new features java 8 new features
java 8 new features
 
PHP 5
PHP 5PHP 5
PHP 5
 
Programming Paradigm & Languages
Programming Paradigm & LanguagesProgramming Paradigm & Languages
Programming Paradigm & Languages
 
C progrmming
C progrmmingC progrmming
C progrmming
 
Lecture 3 java basics
Lecture 3 java basicsLecture 3 java basics
Lecture 3 java basics
 
C#3.0 & Vb 9.0 New Features
C#3.0 & Vb 9.0 New FeaturesC#3.0 & Vb 9.0 New Features
C#3.0 & Vb 9.0 New Features
 
Refactoring to Java 8 (Devoxx UK)
Refactoring to Java 8 (Devoxx UK)Refactoring to Java 8 (Devoxx UK)
Refactoring to Java 8 (Devoxx UK)
 
Java Training in Noida Delhi NCR BY Ducat
Java Training in Noida Delhi NCR BY DucatJava Training in Noida Delhi NCR BY Ducat
Java Training in Noida Delhi NCR BY Ducat
 
Oracle Objects And Transactions
Oracle Objects And TransactionsOracle Objects And Transactions
Oracle Objects And Transactions
 
A-Brief-Introduction-To-JAVA8_By_Srimanta_Sahu
A-Brief-Introduction-To-JAVA8_By_Srimanta_SahuA-Brief-Introduction-To-JAVA8_By_Srimanta_Sahu
A-Brief-Introduction-To-JAVA8_By_Srimanta_Sahu
 
Java
JavaJava
Java
 
JDD 2017: Kotlin for Java developers (Tomasz Kleszczyński)
JDD 2017: Kotlin for Java developers (Tomasz Kleszczyński)JDD 2017: Kotlin for Java developers (Tomasz Kleszczyński)
JDD 2017: Kotlin for Java developers (Tomasz Kleszczyński)
 
Lambdas
LambdasLambdas
Lambdas
 
Introduction to functional programming, with Elixir
Introduction to functional programming,  with ElixirIntroduction to functional programming,  with Elixir
Introduction to functional programming, with Elixir
 
Why functional programming in C# & F#
Why functional programming in C# & F#Why functional programming in C# & F#
Why functional programming in C# & F#
 
java token
java tokenjava token
java token
 

Ähnlich wie JDT embraces lambda expressions

Design and Implementation of the Security Graph Language
Design and Implementation of the Security Graph LanguageDesign and Implementation of the Security Graph Language
Design and Implementation of the Security Graph Language
Asankhaya Sharma
 
Apache Spark MLlib 2.0 Preview: Data Science and Production
Apache Spark MLlib 2.0 Preview: Data Science and ProductionApache Spark MLlib 2.0 Preview: Data Science and Production
Apache Spark MLlib 2.0 Preview: Data Science and Production
Databricks
 
New c sharp3_features_(linq)_part_iv
New c sharp3_features_(linq)_part_ivNew c sharp3_features_(linq)_part_iv
New c sharp3_features_(linq)_part_iv
Nico Ludwig
 

Ähnlich wie JDT embraces lambda expressions (20)

New c sharp3_features_(linq)_part_ii
New c sharp3_features_(linq)_part_iiNew c sharp3_features_(linq)_part_ii
New c sharp3_features_(linq)_part_ii
 
Functional programming in java 8 by harmeet singh
Functional programming in java 8 by harmeet singhFunctional programming in java 8 by harmeet singh
Functional programming in java 8 by harmeet singh
 
Java 8 Lambda
Java 8 LambdaJava 8 Lambda
Java 8 Lambda
 
Anatomy of spark catalyst
Anatomy of spark catalystAnatomy of spark catalyst
Anatomy of spark catalyst
 
Alive and Well with Java 8
Alive and Well with Java 8Alive and Well with Java 8
Alive and Well with Java 8
 
Design and Implementation of the Security Graph Language
Design and Implementation of the Security Graph LanguageDesign and Implementation of the Security Graph Language
Design and Implementation of the Security Graph Language
 
Functional Programming in JavaScript & ESNext
Functional Programming in JavaScript & ESNextFunctional Programming in JavaScript & ESNext
Functional Programming in JavaScript & ESNext
 
Java 12 - New features in action
Java 12 -   New features in actionJava 12 -   New features in action
Java 12 - New features in action
 
Jf12 lambdas injava8-1
Jf12 lambdas injava8-1Jf12 lambdas injava8-1
Jf12 lambdas injava8-1
 
Apache Big Data Europe 2016
Apache Big Data Europe 2016Apache Big Data Europe 2016
Apache Big Data Europe 2016
 
Compiler Design
Compiler Design Compiler Design
Compiler Design
 
Learn To Code: Introduction to java
Learn To Code: Introduction to javaLearn To Code: Introduction to java
Learn To Code: Introduction to java
 
Apache Spark MLlib 2.0 Preview: Data Science and Production
Apache Spark MLlib 2.0 Preview: Data Science and ProductionApache Spark MLlib 2.0 Preview: Data Science and Production
Apache Spark MLlib 2.0 Preview: Data Science and Production
 
Let's PHP in a better way! - Coding Recommendations.
Let's PHP in a better way! - Coding Recommendations.Let's PHP in a better way! - Coding Recommendations.
Let's PHP in a better way! - Coding Recommendations.
 
Data Science Salon: A Journey of Deploying a Data Science Engine to Production
Data Science Salon: A Journey of Deploying a Data Science Engine to ProductionData Science Salon: A Journey of Deploying a Data Science Engine to Production
Data Science Salon: A Journey of Deploying a Data Science Engine to Production
 
History of java'
History of java'History of java'
History of java'
 
New c sharp3_features_(linq)_part_iv
New c sharp3_features_(linq)_part_ivNew c sharp3_features_(linq)_part_iv
New c sharp3_features_(linq)_part_iv
 
Ml also helps generic compiler ?
Ml also helps generic compiler ?Ml also helps generic compiler ?
Ml also helps generic compiler ?
 
Introduction to Spark ML Pipelines Workshop
Introduction to Spark ML Pipelines WorkshopIntroduction to Spark ML Pipelines Workshop
Introduction to Spark ML Pipelines Workshop
 
New c sharp3_features_(linq)_part_iv
New c sharp3_features_(linq)_part_ivNew c sharp3_features_(linq)_part_iv
New c sharp3_features_(linq)_part_iv
 

Mehr von Eclipse Day India

Please Behave Yourself: BDD and automating Eclipse RCP applications using JBe...
Please Behave Yourself: BDD and automating Eclipse RCP applications using JBe...Please Behave Yourself: BDD and automating Eclipse RCP applications using JBe...
Please Behave Yourself: BDD and automating Eclipse RCP applications using JBe...
Eclipse Day India
 
Supporting Java™ 9 in Eclipse - A critical perspective - Stephan Herrmann
Supporting Java™ 9 in Eclipse - A critical perspective - Stephan HerrmannSupporting Java™ 9 in Eclipse - A critical perspective - Stephan Herrmann
Supporting Java™ 9 in Eclipse - A critical perspective - Stephan Herrmann
Eclipse Day India
 

Mehr von Eclipse Day India (20)

Java Performance Testing for Everyone - Shelley Lambert
Java Performance Testing for Everyone - Shelley LambertJava Performance Testing for Everyone - Shelley Lambert
Java Performance Testing for Everyone - Shelley Lambert
 
Eclipse IDE Tips and Tricks - Lakshmi Priya Shanmugam
Eclipse IDE Tips and Tricks - Lakshmi Priya ShanmugamEclipse IDE Tips and Tricks - Lakshmi Priya Shanmugam
Eclipse IDE Tips and Tricks - Lakshmi Priya Shanmugam
 
Pattern Matching in Java - Srikanth Sankaran
Pattern Matching in Java - Srikanth SankaranPattern Matching in Java - Srikanth Sankaran
Pattern Matching in Java - Srikanth Sankaran
 
Machine Learning for Java Developers - Nasser Ebrahim
Machine Learning for Java Developers - Nasser EbrahimMachine Learning for Java Developers - Nasser Ebrahim
Machine Learning for Java Developers - Nasser Ebrahim
 
Scaling Eclipse on HiDPI Monitors - Niraj Modi
Scaling Eclipse on HiDPI Monitors - Niraj ModiScaling Eclipse on HiDPI Monitors - Niraj Modi
Scaling Eclipse on HiDPI Monitors - Niraj Modi
 
Please Behave Yourself: BDD and automating Eclipse RCP applications using JBe...
Please Behave Yourself: BDD and automating Eclipse RCP applications using JBe...Please Behave Yourself: BDD and automating Eclipse RCP applications using JBe...
Please Behave Yourself: BDD and automating Eclipse RCP applications using JBe...
 
Supporting Java™ 9 in Eclipse - A critical perspective - Stephan Herrmann
Supporting Java™ 9 in Eclipse - A critical perspective - Stephan HerrmannSupporting Java™ 9 in Eclipse - A critical perspective - Stephan Herrmann
Supporting Java™ 9 in Eclipse - A critical perspective - Stephan Herrmann
 
Eclipse Day India 2015 - Rest with Java (jax rs) and jersey
Eclipse Day India 2015 - Rest with Java (jax rs) and jerseyEclipse Day India 2015 - Rest with Java (jax rs) and jersey
Eclipse Day India 2015 - Rest with Java (jax rs) and jersey
 
Eclipse Day India 2015 - Java bytecode analysis and JIT
Eclipse Day India 2015 - Java bytecode analysis and JITEclipse Day India 2015 - Java bytecode analysis and JIT
Eclipse Day India 2015 - Java bytecode analysis and JIT
 
Eclipse Day India 2015 - Java 8 Overview
Eclipse Day India 2015 - Java 8 OverviewEclipse Day India 2015 - Java 8 Overview
Eclipse Day India 2015 - Java 8 Overview
 
Eclipse Day India 2015 - Java 9
Eclipse Day India 2015 - Java 9Eclipse Day India 2015 - Java 9
Eclipse Day India 2015 - Java 9
 
Eclipse Day India 2015 - Keynote - Stephan Herrmann
Eclipse Day India 2015 - Keynote - Stephan HerrmannEclipse Day India 2015 - Keynote - Stephan Herrmann
Eclipse Day India 2015 - Keynote - Stephan Herrmann
 
Eclipse Day India 2015 - Eclipse RCP testing using Jubula based automation
Eclipse Day India 2015 - Eclipse RCP testing using Jubula based automationEclipse Day India 2015 - Eclipse RCP testing using Jubula based automation
Eclipse Day India 2015 - Eclipse RCP testing using Jubula based automation
 
Eclipse Day India 2015 - Oomph
Eclipse Day India 2015 - OomphEclipse Day India 2015 - Oomph
Eclipse Day India 2015 - Oomph
 
Eclipse Day India 2015 - Keynote (Mike Milinkovich)
Eclipse Day India 2015 - Keynote (Mike Milinkovich)Eclipse Day India 2015 - Keynote (Mike Milinkovich)
Eclipse Day India 2015 - Keynote (Mike Milinkovich)
 
Eclipse Day India 2015 - Unleashing the Java 8 Tooling in Eclipse
Eclipse Day India 2015 - Unleashing the Java 8 Tooling in EclipseEclipse Day India 2015 - Unleashing the Java 8 Tooling in Eclipse
Eclipse Day India 2015 - Unleashing the Java 8 Tooling in Eclipse
 
IDS and Bluemix
IDS and BluemixIDS and Bluemix
IDS and Bluemix
 
SWT - Technical Deep Dive
SWT - Technical Deep DiveSWT - Technical Deep Dive
SWT - Technical Deep Dive
 
PDE builds or Maven
PDE builds or MavenPDE builds or Maven
PDE builds or Maven
 
Orion - IDE on the cloud
Orion - IDE on the cloudOrion - IDE on the cloud
Orion - IDE on the cloud
 

Kürzlich hochgeladen

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 

Kürzlich hochgeladen (20)

Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelNavi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
A Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusA Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source Milvus
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 

JDT embraces lambda expressions

  • 1. 8 8 Srikanth Sankaran IBM India Stephan Herrmann GK Software BETA_JAVA8
  • 2. 8 Eclipse and Java™ 8 ● Java 8 features available in early access builds: – JSR308 - Type Annotations. – JEP120 - Repeating Annotations. – JEP118 - Method Parameter Reflection. – JSR269 - Pluggable Annotation Processor API & javax.lang.model API enhancements for Java 8. – Support for “code carrying” interface methods. ● Partial support for – Lambda Expressions & Method/Constructor references – Overload resolution & Type inference changes still evolving ● had been blocked by incomplete specification until late September Srikanth Sankaran, Stephan Herrmann: Eclipse and Java 8 - EclipseCon Europe 2013 # 3
  • 3. 8 Eclipse and Java™ 8 ● IDE features available in early access builds: – Type annotations based static null analysis ● substantially complete, open issues exist – Code completion, code selection/navigation, search engine ● for completed features – Code formatter and code “reconciler” – IDE enablement support: ● AST API's, programmatic AST rewriting API support etc ● Full fledged IDE support work in high gear: – Early access build refreshes very soon. ● JDT team: – 12 committers + 1 contributor + 1 GSOC student + 2 past committers on consulting role. Java8 Java8 Srikanth Sankaran, Stephan Herrmann: Eclipse and Java 8 - EclipseCon Europe 2013 # 4
  • 4. 8 New Kinds of Methods ● defender methods ● virtual extension methods ● static interface methods ● default methods Srikanth Sankaran, Stephan Herrmann: Eclipse and Java 8 - EclipseCon Europe 2013 # 6
  • 5. 8 New Kinds of Methods Those are only 2 kinds :) ● Code carrying methods in interfaces: – static methods ● non-OO programming even in interfaces – default methods previous names: ● defender methods ● virtual extension methods Srikanth Sankaran, Stephan Herrmann: Eclipse and Java 8 - EclipseCon Europe 2013 # 7
  • 6. 8 Default Methods - Intention ● Intention – support evolution of interfaces in libraries: add methods to an interface w/o breaking implementors – still legal: Srikanth Sankaran, Stephan Herrmann: Eclipse and Java 8 - EclipseCon Europe 2013 # 8
  • 7. Srikanth Sankaran, Stephan Herrmann: Eclipse and Java 8 - EclipseCon Europe 2013 # 9 8 Default Methods - Consequences ● Consequences – Java 8 has multiple inheritance, sort-of – but little help for corner cases Root m() Both Left D m() Right D m() resolve:
  • 8. Srikanth Sankaran, Stephan Herrmann: Eclipse and Java 8 - EclipseCon Europe 2013 # 10 8 Default Methods - Consequences ● Consequences – Java 8 has multiple inheritance, sort-of – but little help for corner cases Both Left D m() Right m() The default method m() inherited from Left conflicts with another method inherited from Right Advice: Don't!
  • 9. 8 Default Methods - Compatibility ● Designed for compatibility during evolution ● For the price of compatibility – Recipe for disaster: ● implement java.util.List → compile against JRE 7 → OK ● upgrade to JRE 8 but compile as 1.7 The type MyList<E> must implement the inherited abstract method Collection<E>.stream() – Undefined compiler behavior ● 1.7 compiler cannot handle default methods (from .class) ● ecj and javac differ – no plans to assimilate ● depending on compiler implementation details, not specification Srikanth Sankaran, Stephan Herrmann: Eclipse and Java 8 - EclipseCon Europe 2013 # 12
  • 10. 8 Default Methods - Compatibility ● Designed for compatibility during evolution ● For the price of compatibility – Recipe for disaster: ● implement java.util.List → compile against JRE 7 → OK ● upgrade to JRE 8 but compile as 1.7 The type MyList<E> must implement the inherited abstract method Collection<E>.stream() – Undefined compiler behavior ● 1.7 compiler cannot handle default methods (from .class) ● ecj and javac differ – no plans to assimilate ● depending on compiler implementation details, not specification Advice: Don't mix -source 1.7 & JRE8! Srikanth Sankaran, Stephan Herrmann: Eclipse and Java 8 - EclipseCon Europe 2013 # 13
  • 11. Srikanth Sankaran, Stephan Herrmann: Eclipse and Java 8 - EclipseCon Europe 2013 # 14 8 λ (JSR 335)
  • 12. Srikanth Sankaran, Stephan Herrmann: Eclipse and Java 8 - EclipseCon Europe 2013 # 15 8 Introducing Lambda Expressions ● Lambda Expressions, Closures, Anonymous Methods – what is in a name ? ● A function + “captured state” (may have non-locals) – accessible even when outside of defining lexical scope ● Theoretical framework dates back several decades: – Lambda (1930's – Church, Lisp – 1958) – Closure (1960's - Landin, Scheme - 1975) – Common idiomatic device in functional programming. ● Paradigm of passing a "code block as data"
  • 13. Srikanth Sankaran, Stephan Herrmann: Eclipse and Java 8 - EclipseCon Europe 2013 # 16 8 Java 7 Style of “Code as Data” ● Via anonymous classes
  • 14. Srikanth Sankaran, Stephan Herrmann: Eclipse and Java 8 - EclipseCon Europe 2013 # 17 8 Java 7 Style of “Code as Data” ● Via anonymous classes So if lambda expressions help model "code as data" are they just old wine in a new bottle? No!
  • 15. 8 Lambda Expressions ● Eliminate thorny issues with anonymous classes – Verbose/clunky Syntax – Scoping ● anonymous classes introduce their own scopes ● interplay between names in enclosing scope ↔ inherited names – Capture ● can only capture explicitly final outer locals ● necessarily capture enclosing class instance (memory leaks) ● Lambdas – address the above, designed to be flexible & optimizable – much more pleasing & efficient to program with call backs – With default methods, enabled Collections overhaul. Srikanth Sankaran, Stephan Herrmann: Eclipse and Java 8 - EclipseCon Europe 2013 # 18
  • 16. 8 λ Syntax and Examples Srikanth Sankaran, Stephan Herrmann: Eclipse and Java 8 - EclipseCon Europe 2013 # 20
  • 17. 8 Target typing ● Lambda expression is basically – an anonymous method – with possibly "captured" enclosing state ● At the grammar level, lambdas are expressions (duh!) – Can occur anywhere an expression can – Introduces severe complications in parsing. ● Legally however, – Lambda needs a context that defines a "target type" – Everywhere else will have to rejected by the compiler. – Assignment, initialization, casts, method/ctor invocation, lambda expression body, conditional expressions and return statements are contexts that provide target type. Srikanth Sankaran, Stephan Herrmann: Eclipse and Java 8 - EclipseCon Europe 2013 # 21
  • 18. 8 Target Typing Examples Srikanth Sankaran, Stephan Herrmann: Eclipse and Java 8 - EclipseCon Europe 2013 # 22
  • 19. 8 Functional Interfaces ● The target type must be a functional interface ● Functional interface – Declares a single abstract method – Some methods don't count: ● Default & static methods - they are not abstract ● May freely redeclare java.lang.Object's methods – May be tagged with @FunctionalInterface to express intent ● So the lambda object implements the functional interface ● Another way of constructing objects – apart from the SE7 ways (new, reflection, serialization, clone ...) Srikanth Sankaran, Stephan Herrmann: Eclipse and Java 8 - EclipseCon Europe 2013 # 23
  • 20. 8 Lambda Syntax examples – Parameter types ● explicit, or ● inferred from target functional interface methods – Body ● expression, or ● block. Srikanth Sankaran, Stephan Herrmann: Eclipse and Java 8 - EclipseCon Europe 2013 # 24 – () ● can be omitted if a singleton parameter
  • 21. 8 Lambda Syntax examples – Parameter types ● explicit, or Trade-off between conciseness of expression ● inferred from target functional interface methods – Body ● expression, or ● block. Srikanth Sankaran, Stephan Herrmann: Eclipse and Java 8 - EclipseCon Europe 2013 # 25 – () ● can be omitted if a singleton parameter vs clarity of code - in the hands of the programmer
  • 22. 8 Variable capture ● A lambda can refer to enclosing method's locals – Captured state constitutes the "referencing environment" – Compiler can infer finality – Captured state is not mutable. ● Properly: capture of "immutable outer local's value" – Mutability of outer locals implies race conditions – Mutability would necessarily impose serial execution. – Lambda creation & invocation could be far apart. ● may be in different threads, may be after enclosing method return ● This "constraint" actually enables parallelism. ● Enclosing class instance captured only if referred to. Srikanth Sankaran, Stephan Herrmann: Eclipse and Java 8 - EclipseCon Europe 2013 # 26
  • 23. 8 Method References ● Similar to lambda expressions – Require a target type that must be a functional interface – Serve as instances of the functional interface – Rather than providing a method body, they refer to an existing method – Several variants offered. Srikanth Sankaran, Stephan Herrmann: Eclipse and Java 8 - EclipseCon Europe 2013 # 27
  • 24. Srikanth Sankaran, Stephan Herrmann: Eclipse and Java 8 - EclipseCon Europe 2013 # 28 8 Lambda code generation ● Not lowered into anonymous classes - i.e not just syntactic sugar ● What does the following print ? ● Lambda object creation under the hood: – Handled by "Lambda metafactory" API in JRE. – Compilers generates suitable calls to the metafactory. – Enables various optimizations.
  • 25. 8 Putting it all together ● Here is a nice example from SOTL[*] [*] http://cr.openjdk.java.net/~briangoetz/lambda/lambda-state-final.html Srikanth Sankaran, Stephan Herrmann: Eclipse and Java 8 - EclipseCon Europe 2013 # 29
  • 26. JDT features in the works 8 Srikanth Sankaran, Stephan Herrmann: Eclipse and Java 8 - EclipseCon Europe 2013 # 30
  • 27. JDT features in the works 8 Srikanth Sankaran, Stephan Herrmann: Eclipse and Java 8 - EclipseCon Europe 2013 # 31
  • 28. JDT features in the works 8 Srikanth Sankaran, Stephan Herrmann: Eclipse and Java 8 - EclipseCon Europe 2013 # 32
  • 29. JDT features in the works 8 Srikanth Sankaran, Stephan Herrmann: Eclipse and Java 8 - EclipseCon Europe 2013 # 33
  • 30. Srikanth Sankaran, Stephan Herrmann: Eclipse and Java 8 - EclipseCon Europe 2013 # 35 8 @ (JSR 308)
  • 31. 8 Annotations in more Places ● Java 5: annotate declarations – ElementType: packages, classes, fields, methods, locals … ● Java 8: annotate types – ElementType.TYPE_USE – ElementType.TYPE_PARAMETER So what? Srikanth Sankaran, Stephan Herrmann: Eclipse and Java 8 - EclipseCon Europe 2013 # 36
  • 32. 8 Why Care About Types? ● Type = Constraints on values To detect anomalies – missing capability – incompatible assignment – undeclared capability ● Constraint checking avoids errors – No Such Method / Field ● Basic statically typed OO – ClassCastException ● Generics – ??ExcepMtioyn pet example: NullPointerException Srikanth Sankaran, Stephan Herrmann: Eclipse and Java 8 - EclipseCon Europe 2013 # 37
  • 33. Let the Type System 8 Handle Nullity ● Ideally: Java would force explicit choice – String definitely a String, never null – String? either a String or null – Type system ensures: no dereferencing of null ● Nullity as a language feature? – Heavy weight, incompatible change – Language change for each new constraint? Srikanth Sankaran, Stephan Herrmann: Eclipse and Java 8 - EclipseCon Europe 2013 # 38
  • 34. 8 Pluggable Type System ● Make it easier to add new constraints – Only one new syntax for all kinds of constraints ● Make it easier to add new type checkers – Checker Framework (Michael Ernst – U of Washington) ● Examples – @NonNull – @Interned equals(== , equals) – @Immutable value cannot change (Java 5 ?) – @ReadOnly value cannot change via this reference – @UI code requires to run on the SWT UI thread Srikanth Sankaran, Stephan Herrmann: Eclipse and Java 8 - EclipseCon Europe 2013 # 39
  • 35. Can't Java 5 Do All This? @Target(ElementType.PARAMETER) @interface NonNull5 {} void java5(@NonNull5 String arg); arg is qualified to be non-null @Target(ElementType.TYPE_USE) @interface NonNull8 {} void java8(@NonNull8 String arg); String is qualified to be non-null String is qualified to be non-null @Target(ElementType.METHOD) @interface NonNull5 {} @NonNull5 String java5(); java5 is qualified to be non-null Srikanth Sankaran, Stephan Herrmann: Eclipse and Java 8 - EclipseCon Europe 2013 # 40 8 @Target(ElementType.TYPE_USE) @interface NonNull8 {} @NonNull8 String java8(); ● We've been lying about the method result – but we can't lie about everything, e.g.: void printFirstDog(@NonNull List<Dog> dogs) { dogs.get(0).bark(); } NPE?
  • 36. l1 cannot contain null elements Null type mismatch: required '@NonNull String' but the provided value is null Srikanth Sankaran, Stephan Herrmann: Eclipse and Java 8 - EclipseCon Europe 2013 # 41 8 void bad(List<String> unknown, List<@Nullable String> withNulls) { @NonNull List<@NonNull String> l1 = new ArrayList<>(); l1.add(null); String first = l1.get(0); if (first == null) return; l1 = unknown; l1 = withNulls; String canNull = withNulls.get(0); System.out.println(canNull.toUpperCase()); } @NonNull List<@Nullable String> l2 = new ArrayList<>(); l2 can contain null elements Annotated Generics void good() { @NonNull List<@NonNull String> l1 = new ArrayList<>(); l1.add("Hello"); for (String elem : l1) System.out.println(elem.toUpperCase()); l2.add(null); for (String unknown : l2) if (unknown != null) System.out.println(unknown.toUpperCase()); } Null type mismatch: required '@NonNull String' but the provided value is null
  • 37. l1 cannot contain null elements Null comparison always yields false: The variable first cannot be null at this location Srikanth Sankaran, Stephan Herrmann: Eclipse and Java 8 - EclipseCon Europe 2013 # 42 8 void bad(List<String> unknown, List<@Nullable String> withNulls) { @NonNull List<@NonNull String> l1 = new ArrayList<>(); l1.add(null); String first = l1.get(0); if (first == null) return; l1 = unknown; l1 = withNulls; String canNull = withNulls.get(0); System.out.println(canNull.toUpperCase()); } @NonNull List<@Nullable String> l2 = new ArrayList<>(); l2 can contain null elements Annotated Generics void good() { @NonNull List<@NonNull String> l1 = new ArrayList<>(); l1.add("Hello"); for (String elem : l1) System.out.println(elem.toUpperCase()); l2.add(null); for (String unknown : l2) if (unknown != null) System.out.println(unknown.toUpperCase()); } Null comparison always yields false: The variable first cannot be null at this location
  • 38. l1 cannot contain null elements Null type safety (type annotations): The expression of type 'List<String>' needs unchecked conversion to conform to '@NonNull List<@NonNull String>' Srikanth Sankaran, Stephan Herrmann: Eclipse and Java 8 - EclipseCon Europe 2013 # 43 8 void bad(List<String> unknown, List<@Nullable String> withNulls) { @NonNull List<@NonNull String> l1 = new ArrayList<>(); l1.add(null); String first = l1.get(0); if (first == null) return; l1 = unknown; l1 = withNulls; String canNull = withNulls.get(0); System.out.println(canNull.toUpperCase()); } @NonNull List<@Nullable String> l2 = new ArrayList<>(); l2 can contain null elements Annotated Generics void good() { @NonNull List<@NonNull String> l1 = new ArrayList<>(); l1.add("Hello"); for (String elem : l1) System.out.println(elem.toUpperCase()); l2.add(null); for (String unknown : l2) if (unknown != null) System.out.println(unknown.toUpperCase()); } Null type safety (type annotations): The expression of type 'List<String>' needs unchecked conversion to conform to '@NonNull List<@NonNull String>'
  • 39. l1 cannot contain null elements Null type mismatch (type annotations): required '@NonNull List<@NonNull String>' but this expression has type 'List<@Nullable String>' Srikanth Sankaran, Stephan Herrmann: Eclipse and Java 8 - EclipseCon Europe 2013 # 44 8 void bad(List<String> unknown, List<@Nullable String> withNulls) { @NonNull List<@NonNull String> l1 = new ArrayList<>(); l1.add(null); String first = l1.get(0); if (first == null) return; l1 = unknown; l1 = withNulls; String canNull = withNulls.get(0); System.out.println(canNull.toUpperCase()); } @NonNull List<@Nullable String> l2 = new ArrayList<>(); l2 can contain null elements Annotated Generics void good() { @NonNull List<@NonNull String> l1 = new ArrayList<>(); l1.add("Hello"); for (String elem : l1) System.out.println(elem.toUpperCase()); l2.add(null); for (String unknown : l2) if (unknown != null) System.out.println(unknown.toUpperCase()); } Null type mismatch (type annotations): required '@NonNull List<@NonNull String>' but this expression has type 'List<@Nullable String>'
  • 40. l1 cannot contain null elements Potential null pointer access: The variable canNull may be null at this location Srikanth Sankaran, Stephan Herrmann: Eclipse and Java 8 - EclipseCon Europe 2013 # 45 8 void bad(List<String> unknown, List<@Nullable String> withNulls) { @NonNull List<@NonNull String> l1 = new ArrayList<>(); l1.add(null); String first = l1.get(0); if (first == null) return; l1 = unknown; l1 = withNulls; String canNull = withNulls.get(0); System.out.println(canNull.toUpperCase()); } @NonNull List<@Nullable String> l2 = new ArrayList<>(); l2 can contain null elements Annotated Generics void good() { @NonNull List<@NonNull String> l1 = new ArrayList<>(); l1.add("Hello"); for (String elem : l1) System.out.println(elem.toUpperCase()); l2.add(null); for (String unknown : l2) if (unknown != null) System.out.println(unknown.toUpperCase()); } Potential null pointer access: The variable canNull may be null at this location
  • 41. 8 Framework Development (1) ● So you want to be smart: public interface ILib<E> { @NonNull E work(@Nullable E e); } ● But will clients like it? public class LibImpl implements ILib<String> { @Override public @Nullable String work(@NonNull String e) { return null; } ● If thou usest annotations know thy co/contravariance – @Nullable parameter is irreversible – Design bug in guava's Predicate / Function Srikanth Sankaran, Stephan Herrmann: Eclipse and Java 8 - EclipseCon Europe 2013 # 47 }
  • 42. 8 Framework Development (2) ● Avoid overspecification, make interfaces null-generic: public interface Function<I,O> { O apply(I in); } ● Let clients decide class ToString implements Function<@NonNull Object, @Nullable String> { @Override public @Nullable String apply(@NonNull Object o) { return o.toString(); } No NPE! Srikanth Sankaran, Stephan Herrmann: Eclipse and Java 8 - EclipseCon Europe 2013 # 48 } ● Putting the pieces together <@NonNull I, @Nullable O> Collection<O> map1(Collection<I> in, Function<I, O> f) { … } <@Nullable I, @NonNull O> Collection<O> map2(Collection<I> in, Function<I, O> f) { … } ... List<@NonNull Object> in = new ArrayList<>(); Collection<@Nullable String> out = map1(in, new ToString());
  • 43. 8 Caveat: Arrays void test (@NonNull String [] stringsOrNulls) { System.out.println(stringsOrNulls[0]); ● Semantics are changing from Java 7 to Java 8 Srikanth Sankaran, Stephan Herrmann: Eclipse and Java 8 - EclipseCon Europe 2013 # 49 } array of nonnull elements the array can still be null Þ NPE void test (String @NonNull [] stringsOrNulls) { System.out.println(stringsOrNulls[0]); } nonnull array NPE- safe (but may print “null”)
  • 44. Srikanth Sankaran, Stephan Herrmann: Eclipse and Java 8 - EclipseCon Europe 2013 # 50 8 Status: Annotated Type Analysis ● Implemented EA: @NonNull, @Nullable – Much analysis already performed ● known & unknown bugs – Editor may show more bogus errors ● Project > Clean and Problems View ● Planned: @Uninterned ● Proposed: @UiEffect, @Ui … – by [Colin S. Gordon, Werner Dietl, Michael D. Ernst, and Dan Grossman] – SWT: bye, bye, “Invalid thread access”
  • 45. 8 Status: Type Annotations ● Supported by – Compiler – DOM AST – APT ● Work in progress – respect during CCttrrll CCttrrll 1 CAtlrtl T ● Not yet supported by – Java model Srikanth Sankaran, Stephan Herrmann: Eclipse and Java 8 - EclipseCon Europe 2013 # 51
  • 46. 8 TypeBinding Backstage Story aka “Symbol” ● Type bindings are “interned” – OK to use == ● Broken by encoding type annotations in type bindings ● Solution – Find/replace == comparisons for T <: TypeBinding – Tweak our compiler to do this – Plan: publish the tweak, controlled by @Uninterned Srikanth Sankaran, Stephan Herrmann: Eclipse and Java 8 - EclipseCon Europe 2013 # 52
  • 47. Srikanth Sankaran, Stephan Herrmann: Eclipse and Java 8 - EclipseCon Europe 2013 # 53 8 Dramatis personæ ● Jay Arthanareeswaran ● Anirban Chakarborty ● Manoj Palat ● Shankha Banerjee ● Manju Mathew ● Noopur Gupta ● Deepak Azad ● Srikanth Sankaran ● Olivier Thomann ● Andy Clement ● Michael Rennie ● Jesper S. Møller ● Walter Harley ● Stephan Herrmann ● Dani Megert ● Markus Keller ● Early Access: – http://wiki.eclipse.org/JDT_Core/Java8 ● We invite you to test - defect reports welcome !