• Like
  • Save
Syntactic Salt and Sugar Presentation
Upcoming SlideShare
Loading in...5
×
 

Syntactic Salt and Sugar Presentation

on

  • 898 views

Presentation given at Jazoon 2012 on syntactical elements of new Java language enhancements, as well as covering DSL's, aspects and annotations.

Presentation given at Jazoon 2012 on syntactical elements of new Java language enhancements, as well as covering DSL's, aspects and annotations.

Statistics

Views

Total Views
898
Views on SlideShare
870
Embed Views
28

Actions

Likes
0
Downloads
3
Comments
0

3 Embeds 28

http://eventifier.co 26
https://si0.twimg.com 1
http://eventifier.com 1

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Syntactic Salt and Sugar Presentation Syntactic Salt and Sugar Presentation Presentation Transcript

    • Syntactic Salt and SugarJames GouldAlex HolmesVerisign205
    • Alex 2
    • Jim 3
    • Introduction 4
    • Format Discuss 5 Syntactic Elements> > Lead discussion of each Element Introduce newer Elements – – – Identify Good Identify Bad Participation is encouraged!> 5
    • AGENDA> DSL> Project Lambda> AOP 2> CDI> Grab Bag 6
    • AGENDA> DSL> Project Lambda 2> AOP> CDI> Grab Bag 7
    • omain specific languageNoun: a computer programming language of limited expressiveness focused on a particular domain.
    • Rich languages
    • Domain specific language
    • DSL examples ELECT * FROM STATION WHERE LAT_N > 39.7;
    • Domain model
    • Grammatical Elements of a DSL operators + verbs nouns separators deposit account () {} [] . -> => = withdraw amount < > ! ~ ? : == ++ || -- *= <<< % ^ ; , %= && >= -1 >>>= // #
    • Internal DSL An internal DSL is a DSL represented within the syntax of a general-purpose language. Its a stylized use of that language for a domain-specific Excerpt from Martin Fowler’s book “Domain purpose. Specific Languages”, published by Addison- Wesley
    • Java internal DSL 15
    • Java internal DSL
    • External DSL An external DSL is a domain- specific language represented in a separate language to the main programming language its working with. This language may use a custom syntax, or it may Excerpt from Martin Fowler’s book “Domain follow the syntax of another Specific Languages”, published by Addison- Wesley representation such as XML.
    • External DSL for ATM d -> :a(0.95) :chk(12345) What do “d”, “a” and “chk” represent? What’s with the obfuscated syntax? Where is the language documented? YALTL – Yet another language to learn
    • Just call me Sherlock
    • Re-worked DSL grammar deposit amount 0.95 into checking 12345
    • DSL Sugar diomatic way to communicate with domain experts an be used by non-programmers elf-documenting 21
    • DSL Salt SL syntax can quickly become salty o you really need a DSL? upporting a DSL is labor-intensive 22
    • AGENDA> DSL> Project Lambda 2> AOP> CDI> Grab Bag 23
    • Project Lambda : Introduction> Included in Java 8> Defined in JSR-335 – Lambda Expressions – Default Methods 24
    • Project Lambda : Lambda Expressionspublic class Calculator { ublic class Calculator { static interface Calculate { int calc(int op1, int op2); static interface Calculate { } int calc(int op1, int op2); public static void main(String... args) { } Calculate addition = (op1, op2) -> op1 + op2; public static void main(String... args) { System.out.println("Addition = " + addition.calc(10, 20)); Calculate addition = new Calculate() { Calculate multiply = (op1, op2) -> op1 * op2; public int calc(int op1, int op2) { System.out.println("Multiply = " + multiply.calc(10, 20)); return op1 + op2;}}; }} System.out.println("Addition = " + addition.calc(10, 20)); 25
    • Project Lambda : Type Targeting> How “Calculate addition = (op1, op2) -> op1 + op2;” works? – Type expression is inferred by type expected in context – Calculate is a Functional Interface – Expression compatible to Function Interface  Interface is Functional Interface  Expression has same number of parameters  Expression return is compatible  Expression exception thrown is allowed by Interface> Lambda’s are strongly typed 26
    • Project Lambda : Type Targeting Sample ublic class HelloWorld { public static void main(String... args) throws Exception { Callable<Void> helloCall = () -> {System.out.println("Hello World!"); return null;}; Runnable helloRun = () -> {System.out.println("Hello World!");}; 27 helloCall.call();
    • Lambda’s and Collections raditional iteration: ewritten for lambda’s: or (Car c : cars) { ars.forEach(c -> c.setColor(RED)); c.setColor(RED); arallelized: ars.parallel() .forEach(c -> c.setColor(RED)); 28
    • Project Lambda : Lambda Expression Sugar nonymous addition = (op1, op2)in aop1 + op2; Calculate Inner Classes done -> clean way Calculate addition = new Calculate() { public int calc(int op1, int op2) { return op1 + op2; }}; atches feature of other languages C#, C++, Ruby, Python, JavaScript, … Take advantage of multicore processors Mark Reinhold – “the real reason is multicore processors; the best way to handle them is with 29 Lambda”
    • Project Lambda : Lambda Expression Salt ark Reinhold – “Some would say adding Lambda expressions is just to keep up with the cool kids, and there’s some truth in that” ava as a Object Oriented language o you want Anonymous Inner Classes on steroids? ack of code clarity 30
    • Project Lambda : Default Methods nterface A { void execute() default { System.out.println("A.execute()"); } lass ClassA implements A { lassA classA = new ClassA(); 31
    • Project Lambda : Default Methods nterface A { void execute() default { System.out.println("A.execute()"); } nterface B extends A { void execute() default { System.out.println(“B.execute()"); 32 }
    • Project Lambda : Default Methods nterface A { void execute() default { System.out.println("A.execute()"); } nterface B { void execute() default { System.out.println(“B.execute()"); } lass ClassHuh implements A, B { 33 void execute() {
    • Project Lambda : Default Methods and Lambda nterface A { void execute() default { System.out.println("A.execute()"); } nterface B extends A { void execute() default { System.out.println(“B.execute()"); } void execute2(); 34 nterface C extends A, B {}
    • Project Lambda : Default Methods Sugar inally able to add code to interfaces! 35
    • Project Lambda : Default Methods Salt dding multiple inheritance to Java ixing Default Methods and Lambda Expressions adds more confusion 36
    • AGENDA> DSL> Project Lambda 2> AOP> CDI> Grab Bag 37
    • AOP 2 : Introduction spect Oriented Programming (AOP) nnotation Oriented Programming (AOP) 2 OP 38
    • AOP 2 : Aspect Oriented Programming> When should aspects be used? – By container? By container? – Extraneous functions? Extraneous functions? – Semantics of the language?  A = B? = B? 39
    • AOP 2 : Annotation Oriented Programming> Annotations designed in similar goals as AOP – Special markers to classes, methods, and fields – Processed by libraries and tools> Everything seems to be annotated now! 40
    • AOP 2 : Annotation Oriented Programming Sample@Data RequiredArgsConstructorpublic class Sample { private final int age; @NonNull private String name; EqualsAndHashCode} ToString ublic class Sample {Sample sample = new Sample(21, “James”); @Getter private final int age;assert ( sample.getAge() == 21);assert ( sample.getName().equals(“James”));sample.setName(“Jim”);@Setter private String name; @NonNull @GetterSystem.out.println(“sample = “ + sample);“sample = Sample(age=21, name=Jim) 41
    • AOP 2 : Annotations Sugar Communication with compiler @Override, @Deprecated, @SuppressWarnings Communication with frameworks Java Persistence API, Spring 42
    • AOP 2 : Annotations Salt Configuration Added Dependencies Used for code generation 43
    • 2 2AOP : AOP> Annotations and aspects together is a natural fit – Annotations provide meta-data – Aspects to drive cross-cutting logic based on Annotations 44
    • AOP 2 : Java EE Interceptors ublic class PrintInterceptor { @AroundInvoke public Object execute(InvocationContext ctx) throws Exception { System.out.println("Intercepting method " + ctx.getMethod().getDeclaringClass().getName() + ":" + ctx.getMethod().getName()); Object result = ctx.proceed(); 45 return result;
    • AOP 2 : What is This? Procedure(name = "Account.deposit") ublic void deposit ( @In(Types.NUMERIC) final Long accountId, @In(Types.NUMERIC) final Long locationId, @In(Types.NUMERIC) final BigDecimal amount, @In(Types.TIMESTAMP) final Date transactionDate, @Out(Types.NUMERIC) Ref<BigDecimal> balance) { 46
    • AGENDA> DSL> Project Lambda 2> AOP> CDI> Grab Bag 47
    • Changing Landscape of Dependency Injection
    • Huh?
    • JSR-330 + JSR-299 Compared
    • JSR-330 Annotations• @Inject• @Named• @Provider• @Qualified• @Scope• @Singleton
    • Basic Injection
    • Named Injections
    • Qualifiers
    • Pop Quiz Question: Do both garage instances refer to the same object? Answer: No, the default scoping in JSR-330 is “@Dependent” 55
    • Scopes
    • JSR-299 – Context Dependency Injection• Uses JSR-330 as foundation• Adds Java EE-specific extensions • Producers • Decorators • Interceptor enhancements
    • JSR-299 – Additional Scopes built-in scopes: RequestScoped SessionScoped ApplicationScoped ConversationScoped
    • Pop Quiz
    • CDI Extensions
    • CDI Sugar tandards for Dependency Injection nnotation-driven injection, decorators, interceptors xtensible SPI 62
    • CDI Salt hy are there 2 standards? ack of Java SE DI support o dynamic DI injection 63
    • AGENDA> DSL> Project Lambda 2> AOP> CDI> Grab Bag 64
    • Binary Literals and Underscores
    • Handling multiple exceptions
    • Multi-Catch
    • Resource cleaning
    • Try-with-resources
    • Strings in switch
    • Diamonds (are a geek’s best friend)
    • Java 7 Language Enhancements: Sugar or Salt? 72
    • James Gould verisigninc.comVerisign jgould@verisign.comAlex Holmes verisign.comVerisign alholmes@verisign.com