Syntactic Salt and Sugar Presentation

73
Syntactic Salt and Sugar James Gould Alex Holmes Verisign 205

Transcript of Syntactic Salt and Sugar Presentation

Page 1: Syntactic Salt and Sugar Presentation

Syntactic Salt and Sugar

James Gould

Alex Holmes

Verisign

205

Page 2: Syntactic Salt and Sugar Presentation

Alex

2

Page 3: Syntactic Salt and Sugar Presentation

Jim

3

Page 4: Syntactic Salt and Sugar Presentation

Introduction

4

Page 5: Syntactic Salt and Sugar Presentation

Format> Discuss 5 Syntactic Elements

> Lead discussion of each Element

– Introduce newer Elements

– Identify Good

– Identify Bad

> Participation is encouraged!

5

Page 6: Syntactic Salt and Sugar Presentation

6

AGENDA

> DSL

> Project Lambda

> AOP

> CDI

> Grab Bag

2

Page 7: Syntactic Salt and Sugar Presentation

7

AGENDA

> DSL

> Project Lambda

> AOP

> CDI

> Grab Bag

2

Page 8: Syntactic Salt and Sugar Presentation

omain specific language

Noun: a computer programming language of limited expressiveness focused on a particular domain.

Page 9: Syntactic Salt and Sugar Presentation

Rich languages

Page 10: Syntactic Salt and Sugar Presentation

Domain specific language

Page 11: Syntactic Salt and Sugar Presentation

DSL examples

ELECT * FROM STATION WHERE LAT_N > 39.7;

Page 12: Syntactic Salt and Sugar Presentation

Domain model

Page 13: Syntactic Salt and Sugar Presentation

Grammatical Elements of a DSL

deposit

withdraw

account

amount

verbs nounsoperators +

separators

() {} [] . -> => =

< > ! ~ ? : ==

++ || -- *=

<<< % ^ ; ,

%= && >= -1

>>>= // #

Page 14: Syntactic Salt and Sugar Presentation

Internal DSL

An internal DSL is a DSL

represented within the syntax of

a general-purpose language.

It's a stylized use of that

language for a domain-specific

purpose.

Excerpt from Martin Fowler’s book “Domain Specific Languages”, published by Addison-Wesley

Page 15: Syntactic Salt and Sugar Presentation

Java internal DSL

15

Page 16: Syntactic Salt and Sugar Presentation

Java internal DSL

Page 17: Syntactic Salt and Sugar Presentation

External DSL

An external DSL is a domain-

specific language represented in a

separate language to the main

programming language it's

working with. This language may

use a custom syntax, or it may

follow the syntax of another

representation such as XML.

Excerpt from Martin Fowler’s book “Domain Specific Languages”, published by Addison-Wesley

Page 18: Syntactic Salt and Sugar Presentation

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

Page 19: Syntactic Salt and Sugar Presentation

Just call me Sherlock

Page 20: Syntactic Salt and Sugar Presentation

Re-worked DSL grammar

deposit amount 0.95 into checking 12345

Page 21: Syntactic Salt and Sugar Presentation

DSL Sugar

21

diomatic way to communicate with domain experts

an be used by non-programmers

elf-documenting

Page 22: Syntactic Salt and Sugar Presentation

DSL Salt

22

SL syntax can quickly become salty

o you really need a DSL?

upporting a DSL is labor-intensive

Page 23: Syntactic Salt and Sugar Presentation

23

AGENDA

> DSL

> Project Lambda

> AOP

> CDI

> Grab Bag

2

Page 24: Syntactic Salt and Sugar Presentation

24

Project Lambda : Introduction

> Included in Java 8

> Defined in JSR-335

– Lambda Expressions

– Default Methods

Page 25: Syntactic Salt and Sugar Presentation

Project Lambda : Lambda Expressions

25

ublic class Calculator {

static interface Calculate {

int calc(int op1, int op2);

}

public static void main(String... args) {

Calculate addition = new Calculate() {

public int calc(int op1, int op2) {

return op1 + op2;}};

System.out.println("Addition = " + addition.calc(10, 20));

Calculate multiply = new Calculate() {

public int calc(int op1, int op2) {

return op1 * op2;}};

System.out.println("Multiply = " + multiply.calc(10, 20));

}

public class Calculator {

static interface Calculate {

int calc(int op1, int op2);

}

public static void main(String... args) {

Calculate addition = (op1, op2) -> op1 + op2;

System.out.println("Addition = " + addition.calc(10, 20));

Calculate multiply = (op1, op2) -> op1 * op2;

System.out.println("Multiply = " + multiply.calc(10, 20));

}

}

Page 26: Syntactic Salt and Sugar Presentation

26

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

Page 27: Syntactic Salt and Sugar Presentation

Project Lambda : Type Targeting Sample

27

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!");};

helloCall.call();

helloRun.run();

}

Page 28: Syntactic Salt and Sugar Presentation

Lambda’s and Collections

raditional iteration:

or (Car c : cars) {

c.setColor(RED);

28

ewritten for lambda’s:

ars.forEach(c -> c.setColor(RED));

arallelized:

ars.parallel()

.forEach(c -> c.setColor(RED));

Page 29: Syntactic Salt and Sugar Presentation

Project Lambda : Lambda Expression Sugar

29

nonymous Inner Classes done in a 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 Lambda”

Calculate addition = (op1, op2) -> op1 + op2;

Page 30: Syntactic Salt and Sugar Presentation

Project Lambda : Lambda Expression Salt

30

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

Page 31: Syntactic Salt and Sugar Presentation

Project Lambda : Default Methods

nterface A {

void execute() default {

System.out.println("A.execute()");

}

lass ClassA implements A {

lassA classA = new ClassA();

lassA.execute();

A.execute()”

31

Page 32: Syntactic Salt and Sugar Presentation

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()");

}

nterface C extends A {}

lass ClassHuh implements C, B {}

new ClassHuh()).execute();

B.execute()”

32

Page 33: Syntactic Salt and Sugar Presentation

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 {

void execute() {

A.super.execute();

}

new ClassHuh()).execute();

rror: reference to execute is ambiguous

A.execute()”

33

Page 34: Syntactic Salt and Sugar Presentation

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();

nterface C extends A, B {}

lambdaC = () -> {System.out.println(“Lambda Expression”);};

ambdaC.execute();

ambdaC.execute2();

B.execute()”

Lambda Expression”

34

Page 35: Syntactic Salt and Sugar Presentation

Project Lambda : Default Methods Sugar

35

inally able to add code to interfaces!

Page 36: Syntactic Salt and Sugar Presentation

Project Lambda : Default Methods Salt

36

dding multiple inheritance to Java

ixing Default Methods and Lambda Expressions adds more confusion

Page 37: Syntactic Salt and Sugar Presentation

37

AGENDA

> DSL

> Project Lambda

> AOP

> CDI

> Grab Bag

2

Page 38: Syntactic Salt and Sugar Presentation

38

AOP : Introduction2

spect Oriented Programming (AOP)

OP

2

nnotation Oriented Programming (AOP)

Page 39: Syntactic Salt and Sugar Presentation

39

> When should aspects be used?

– By container?

– Extraneous functions?

– Semantics of the language? A = B?

> When should aspects be used?

By container?

Extraneous functions?

Semantics of the language? A = B?

AOP : Aspect Oriented Programming2

Page 40: Syntactic Salt and Sugar Presentation

40

> 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!

AOP : Annotation Oriented Programming2

Page 41: Syntactic Salt and Sugar Presentation

41

Sample sample = new Sample(21, “James”); assert ( sample.getAge() == 21);assert ( sample.getName().equals(“James”));sample.setName(“Jim”);System.out.println(“sample = “ + sample);

“sample = Sample(age=21, name=Jim)

RequiredArgsConstructor

EqualsAndHashCode

ToString

ublic class Sample {

@Getter private final int age;

@NonNull @Getter @Setter private String name;

@Datapublic class Sample { private final int age; @NonNull private String name;}

AOP : Annotation Oriented Programming Sample2

Page 42: Syntactic Salt and Sugar Presentation

42

Communication with compiler

@Override, @Deprecated, @SuppressWarnings

Communication with frameworks

Java Persistence API, Spring

AOP : Annotations Sugar2

Page 43: Syntactic Salt and Sugar Presentation

43

Configuration

Added Dependencies

Used for code generation

AOP : Annotations Salt2

Page 44: Syntactic Salt and Sugar Presentation

44

> Annotations and aspects together is a natural fit

– Annotations provide meta-data

– Aspects to drive cross-cutting logic based on Annotations

AOP : AOP 2 2

Page 45: Syntactic Salt and Sugar Presentation

45

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();

return result;

}

Interceptors(PrintInterceptor.class)

ublic void targetMethod() {

AOP : Java EE Interceptors2

Page 46: Syntactic Salt and Sugar Presentation

46

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) {

throw new IllegalStateException(

"This method invocation was not intercepted" );

AOP : What is This?2

Page 47: Syntactic Salt and Sugar Presentation

47

AGENDA

> DSL

> Project Lambda

> AOP

> CDI

> Grab Bag

2

Page 48: Syntactic Salt and Sugar Presentation

Changing Landscape of Dependency Injection

Page 49: Syntactic Salt and Sugar Presentation

Huh?

Page 50: Syntactic Salt and Sugar Presentation

JSR-330 + JSR-299 Compared

Page 51: Syntactic Salt and Sugar Presentation

JSR-330 Annotations

• @Inject

• @Named

• @Provider

• @Qualified

• @Scope

• @Singleton

Page 52: Syntactic Salt and Sugar Presentation

Basic Injection

Page 53: Syntactic Salt and Sugar Presentation

Named Injections

Page 54: Syntactic Salt and Sugar Presentation

Qualifiers

Page 55: Syntactic Salt and Sugar Presentation

Pop Quiz

55

Question: Do both garage instances refer to the same object?

Answer: No, the default scoping in JSR-330 is “@Dependent”

Page 56: Syntactic Salt and Sugar Presentation

Scopes

Page 57: Syntactic Salt and Sugar Presentation

JSR-299 – Context Dependency Injection

• Uses JSR-330 as foundation• Adds Java EE-specific extensions

• Producers

• Decorators

• Interceptor enhancements

Page 58: Syntactic Salt and Sugar Presentation

JSR-299 – Additional Scopes

built-in scopes:

RequestScoped

SessionScoped

ApplicationScoped

ConversationScoped

Page 59: Syntactic Salt and Sugar Presentation

Pop Quiz

Page 60: Syntactic Salt and Sugar Presentation
Page 61: Syntactic Salt and Sugar Presentation

CDI Extensions

Page 62: Syntactic Salt and Sugar Presentation

CDI Sugar

62

tandards for Dependency Injection

nnotation-driven injection, decorators, interceptors

xtensible SPI

Page 63: Syntactic Salt and Sugar Presentation

CDI Salt

63

hy are there 2 standards?

ack of Java SE DI support

o dynamic DI injection

Page 64: Syntactic Salt and Sugar Presentation

64

AGENDA

> DSL

> Project Lambda

> AOP

> CDI

> Grab Bag

2

Page 65: Syntactic Salt and Sugar Presentation

Binary Literals and Underscores

Page 66: Syntactic Salt and Sugar Presentation

Handling multiple exceptions

Page 67: Syntactic Salt and Sugar Presentation

Multi-Catch

Page 68: Syntactic Salt and Sugar Presentation

Resource cleaning

Page 69: Syntactic Salt and Sugar Presentation

Try-with-resources

Page 70: Syntactic Salt and Sugar Presentation

Strings in switch

Page 71: Syntactic Salt and Sugar Presentation

Diamonds (are a geek’s best friend)

Page 72: Syntactic Salt and Sugar Presentation

Java 7 Language Enhancements: Sugar or Salt?

72

Page 73: Syntactic Salt and Sugar Presentation

James Gould verisigninc.com

Verisign [email protected]

Alex Holmes verisign.com

Verisign [email protected]