2. Agenda
1. Xtend introduction
2. Active Annotations
3. Extension methods
4. With operator, Lambda expressions
5. Switch expressions
3. Xtend ?
● Xtend is a statically typed programming language
● It is an “extension” to Java and NOT a replacement
● It fully supports all the Java idioms
● It allows usage of existing Java API in much nicer way without any
interoperability issues
● Provides additional features like - Type Inference, Extension methods,
Lambda expressions, Active annotations...
● Latest release - Xtend 2.9.0.
4. Xtend ?
● It is implemented in Xtext and is a proof of concept of how involved a
language implemented in Xtext can be.
6. Active Annotations
● Code written using Xtend gets translated to Java
● The translation process can be “altered” using Active annotations
● Mainly used to generate boilerplate Java code; that otherwise is coded
manually.
7. Active annotation (Syntax)
● Annotation which is itself annotated with Active
@Active(SWTBotProcessor)
annotation SWTBot { }
● @Active takes a Annotation Processor as the parameter
● It can either be declared in Java or Xtend.
12. Existing Active annotation
● org.eclipse.xtend.lib.annotations
○ @Accessor - Adds getter and or setter methods for your fields
○ @Data - Turns an annotated class into Value object class. Generates - final fields, getter
methods, constructor with parameters, equals, hashCode and toString methods
○ @FinalFieldsConstructor - Creates a constructor that takes a parameter for each final
field of a class.
○ @EqualsHashCode - Creates default implementation for equals and hashCode.
14. Extension methods
● Allows you to add additional methods to an existing type without modification
"hello".toFirstUpper() // calls
StringExtensions.toFirstUpper("hello")
● Based on a simple syntactic trick: The method is called with the first
argument as its receiver
entity.save(comment) // persistenceManager.save(entity,
comment);
● Resulting code is much more readable
19. with operator (=>/double-arrow operator)
● Allows you to bind objects to the scope of lambda expression so that you can
perform some operations on it
new Button(parent, style) =>
[text = “Hello World” enabled = false]
● The operator => passes the left expression as a parameter to the lambda on
the right. The result of this operation is the object itself.
24. Java 8 vs. Xtend (subtle differences)
shapes.forEach(
s -> { s.setColor(RED);}
);
1. Represented using arrow ->
2. The ( ) braces are mandatory. Lambda
needs to be passed inside ( ) braces
3. The body of the lambda needs to be inside
curly braces { } and semicolons are
mandatory
4. Parameter names (s) is mandatory.
shapes.forEach[color = RED]
1. Represented using square brackets [ ]
2. Not mandatory. However, you can use the
( ) braces
3. Lambda is a block expressions. No
semicolons
4. Parameter name is optional. Implicit
variable “it” is used.
27. Switch expressions
● Not limited to certain values. It can be used for any object reference
● For case expression of type boolean, the case matches if the expression
evaluates to true, for other types main expression is compared using
Object.equals (Object)