Xtend User Guide Eclipse

Xtend User Guide Eclipse-Free PDF

  • Date:18 Mar 2020
  • Views:55
  • Downloads:1
  • Pages:67
  • Size:308.40 KB

Share Pdf : Xtend User Guide Eclipse

Download and Preview : Xtend User Guide Eclipse

Report CopyRight/DMCA Form For : Xtend User Guide Eclipse


I Getting Started 5,1 Introduction 6,2 Hello World 7. 3 The Movies Example 9,3 1 The Data 9,3 2 Parsing The Data 10. 3 3 Answering Some Questions 11, 3 3 1 Question 1 What Is The Number Of Action Movies 11. 3 3 2 Question 2 What Is The Year The Best Movie From The 80 s. Was Released 12, 3 3 3 Question 3 What Is The The Sum Of All Votes Of The Top Two. II Reference Documentation 14,4 Java Interoperability 15.
4 1 Type Inference 15,4 2 Conversion Rules 15,4 3 Interoperability with Java 16. 5 Classes and Members 17,5 1 Package Declaration 17. 5 2 Imports 17,5 3 Class Declaration 18,5 4 Constructors 19. 5 5 Fields 19,5 6 Methods 20,5 6 1 Abstract Methods 20. 5 6 2 Overriding Methods 21,5 6 3 Declared Exceptions 21.
5 6 4 Inferred Return Types 21,5 6 5 Generic Methods 22. 5 6 6 Dispatch Methods 22,5 7 Annotations 26,5 8 Extension Methods 26. 5 8 1 Extensions from the Library 27,5 8 2 Local Extension Methods 28. 5 8 3 Extension Imports 28,5 8 4 Extension Provider 29. 5 9 Interface Declaration 29,5 10 Annotation Type Declaration 30.
5 11 Enum Type Declaration 30,6 Expressions 31,6 1 Literals 31. 6 1 1 String Literals 31,6 1 2 Character Literals 32. 6 1 3 Number Literals 32,6 1 4 Boolean Literals 32. 6 1 5 Null Literal 33,6 1 6 Type Literals 33,6 1 7 Collection Literals 33. 6 1 8 Arrays 34,6 2 Type Casts 34,6 3 Infix Operators and Operator Overloading 35.
6 3 1 Short Circuit Boolean Operators 37,6 3 2 Defined Operators in The Library 37. 6 3 3 Assignments 40,6 4 Blocks 41,6 5 Variable Declarations 42. 6 5 1 Typing 42,6 6 Field Access and Method Invocation 42. 6 6 1 Property Access 43,6 6 2 Implicit Variables this and it 43. 6 6 3 Static Access 43,6 6 4 Null Safe Feature Call 44.
6 7 Constructor Call 44,6 8 Lambda Expressions 44,6 8 1 Typing 47. 6 9 If Expression 47,6 10 Switch Expression 48,6 10 1 Type guards 49. 6 11 For Loop 49,6 12 While Loop 50,6 13 Do While Loop 50. 6 14 Return Expression 50,6 15 Throwing Exceptions 51. 6 16 Try Catch Finally 51,6 17 Template Expressions 52.
6 17 1 Conditions in Templates 53,6 17 2 Loops in Templates 53. 6 17 3 Typing 54,6 17 4 White Space Handling 54,7 Active Annotations 57. 7 1 Annotation Processor 57,7 1 1 Phase 1 Register Globals 58. 7 1 2 Phase 2 Transformation 58,7 1 3 Phase 3 Code Generation 60. 7 2 On Expressions and Statements 60,7 2 1 Generating Blackbox Java Code 60.
7 2 2 Assigning Expressions 61,7 3 Custom Compiler Checks 62. 7 4 Class Path Setup and Testing 62,7 4 1 Testing 63. 7 4 2 Wrap Up 64,7 5 Existing Active Annotations 64. 7 6 Property 65,7 7 Data 65,Getting Started,1 Introduction. Xtend is a statically typed programming language which translates to comprehensible. Java source code Syntactically and semantically Xtend has its roots in the Java pro. gramming language but improves on many aspects, Extension methods 5 8 enhance closed types with new functionality.
Lambda Expressions 6 8 concise syntax for anonymous function literals. ActiveAnnotations 7 annotation processing on steroids. Operator overloading 6 3 make your libraries even more expressive. Powerful switch expressions 6 10 type based switching with implicit casts. Multiple dispatch 5 6 6 a k a polymorphic method invocation. Template expressions 6 17 with intelligent white space handling. No statements 6 everything is an expression, Properties 6 6 1 shorthands for accessing and defining getters and setter. Type inference you rarely need to write down type signatures anymore. Full support for Java generics including all conformance and conversion rules. Translates to Java not bytecode understand what is going on and use your code. for platforms such as Android or GWT, Unlike other JVM languages Xtend has zero interoperability issues 4 3 with Java. Everything you write interacts with Java exactly as expected At the same time Xtend. is much more concise readable and expressive Xtend s small library is just a thin layer. that provides useful utilities and extensions on top of the Java Development Kit JDK. Of course you can call Xtend methods from Java too in a completely transparent way. Furthermore Xtend provides a modern Eclipse based IDE closely integrated with the. Eclipse Java Development Tools JDT including features like call hierarchies rename. refactoring debugging and many more,2 Hello World, The first thing you want to see in any language is a Hello World example In Xtend. that reads as,class HelloWorld,def static void main String args. println Hello World, You see that Xtend looks quite similar to Java At a first glance the main difference.
seems to be the def keyword to declare a method Also like in Java it is mandatory. to define a class and a main method as the entry point for an application Admittedly. Hello World programs are not a particular strength of Xtend The real expressiveness. is unleashed when you do real things as you will learn in a second. An Xtend class resides in a plain Eclipse Java project As soon as the SDK is installed. Eclipse will automatically translate all the classes to Java source code By default you. will find it in a source folder xtend gen The hello world example is translated to the. following Java code,Generated Java Source Code,import org eclipse xtext xbase lib InputOutput. public class HelloWorld,public static void main final String args. InputOutput String println Hello World, The only surprising fact in the generated Java code may be the referenced library. class InputOutput It is part of the runtime library and a nice utility that is quite handy. when used in expressions, You can put an Xtend class into a source folder of any Java project within Eclipse or. any Maven project If the project is not yet configured properly Eclipse will complain. about the missing library The xtend lib has to be on the class path The IDE will. provide a quick fix to add it, The next thing you might want to do is materializing one of the example projects into.
your workspace Right click anywhere in the Navigator view in Eclipse and select New. In the upcoming dialog you will find two examples for Xtend. Xtend Introductory Examples contains a couple of example code snippets illustrat. ing certain aspects and strengths of Xtend For instance it shows how to build an. API which allows to write code like this,assertEquals 42 km h 40 000 m 2 km 60 min. Also the movies example 3 explained in detail in the next section 3 is included. Xtend Solutions For Euler contains solutions to some of the problems you will find. at Project Euler These examples are leveraging the whole expressive power of. Xtend For instance Euler Problem 1 can be solved with this expression. 1 999 filter i i 3 0 i 5 0 reduce i1 i2 i1 i2,3 The Movies Example. The movies example is included in the example project Xtend Introductory Examples. src examples6 Movies xtend and is about reading a file with data about movies and. doing some analysis on it,3 1 The Data, The movie database is a plain text file data csv with data sets describing movies Here. is an example data set, Naked Lunch 1991 6 9 16578 Biography Comedy Drama Fantasy. The values are separated by two spaces The columns are. 4 numberOfVotes,5 categories, Let us define a data type Movie representing a data set.
Data class Movie,String title,double rating,long numberOfVotes. Set String categories, A movie is a POJO with a strongly typed field for each column in the data sets The. Data 7 7 annotation will turn the class into an immutable value class that is it will. a getter method for each field,a hashCode equals implementation. implementation of Object toString, a constructor accepting values for all fields in the declared order. 3 2 Parsing The Data, Let us now add another class to the same file and initialize a field called movies with a.
list of movies For the initialization we parse the text file and turn the data records into. import java io FileReader,import java util Set, import static extension com google common io CharStreams. class Movies, val movies new FileReader data csv readLines map line. val segments line split iterator,return new Movie,segments next. Integer parseInt segments next,Double parseDouble segments next. Long parseLong segments next,segments toSet, A field s type 5 5 can be inferred from the expression on the right hand side That.
is called local type inference and is supported everywhere in Xtend We want the field. to be final so we declare it as a value using the keyword val. The initialization on the right hand side first creates a new FileReader Then the. method readLines is invoked on that instance But if you have a look at FileReader. you will not find such a method In fact readLines is a static method from Google. Guava s CharStreams which was imported as an extension 5 8 3 Extensions allow us. to use this readable syntax, import static extension com google common io CharStreams. CharStreams readLines Reader returns a List String on which we call another ex. tension method map This one is defined in the runtime library ListExtensions map. and is automatically imported and therefore available on all lists The map extension. expects a function as a parameter It basically invokes that function for each value in the. list and returns another list containing the results of the function invocations Actually. this mapping is performed lazily so if you never access the values of the result list the. mapping function is never executed, Function objects are created using lambda expressions 6 8 the code in squared. brackets Within the lambda we process a single line from the text file and turn it. into a movie by splitting the string using two whitespace characters as the separator. On the result of the split operation the method iterator is invoked As you might. know String split String returns a string array String which Xtend auto converts to. a list 4 2 when we call Iterable iterator on it,val segments line split iterator. Now we use the iterator to create an instance of Movie for each String that it yields. The data type conversion e g String to int is done by calling static methods 6 6 3. from the wrapper types The rest of the Iterable is turned into a set of categories. Therefore the extension method IteratorExtensions toSet Iterator T is invoked on. the iterator to consume its remaining values,return new Movie. segments next,Integer parseInt segments next,Double parseDouble segments next.
Long parseLong segments next,segments toSet,3 3 Answering Some Questions. Now that we have parsed the text file into a List Movie we are ready to execute. some queries against it We use JUnit to make the individual queries executable and to. confirm their results, 3 3 1 Question 1 What Is The Number Of Action Movies. Test def numberOfActionMovies,assertEquals 828,movies filter categories contains Action size. First the movies are filtered The lambda expression checks whether the current. movie s categories contain the entry Action Note that unlike the lambda we used to. turn the lines in the file into movies we have not declared a parameter name this time. We could have written, movies filter movie movie categories contains Action size. but since we left out the name and the vertical bar the variable is automatically named. it it is an implicit variable 6 6 2 It s uses are similar to the implicit variable this. We can write either,movies filter it categories contains Action size.
or even more compact,movies filter categories contains Action size. Eventually we call size on the resulting iterable which is an extension method too It. is defined in the utility class IterableExtensions. 3 3 2 Question 2 What Is The Year The Best Movie From The 80 s Was. Test def void yearOfBestMovieFrom80s,assertEquals 1989. movies filter 1980 1989 contains year sortBy rating last year. Here we filter for all movies whose year is included in the range from 1980 to 1989 the. 80 s The operator is again an extension defined in IntegerExtensions and returns an. instance of IntegerRange Operator overloading is explained in section 6 3. The resulting iterable is sorted IterableExtensions sortBy by the rating of the movies. Since it is sorted in ascending order we take the last movie from the list and return its. We could have sorted descending and take the head of the list as well. movies filter 1980 1989 contains year sortBy rating head year. Another possible solution would be to reverse the order of the sorted list. movies filter 1980 1989 contains year sortBy rating reverseView head year. Note that first sorting and then taking the last or first is slightly more expensive than. needed We could have used the method reduce instead to find the best movie which. would be more efficient Maybe you want to try it on your own. The calls to movie year as well as movie categories in the previous example in fact. access the corresponding getter methods 6 6 1, 3 3 3 Question 3 What Is The The Sum Of All Votes Of The Top Two. Test def void sumOfVotesOfTop2, val long sum movies sortBy rating take 2 map numberOfVotes reduce a b a b. assertEquals 47 229L sum, First the movies are sorted by rating then we take the bes.
Getting Started 5 1 Introduction Xtend is a statically typed programming language which translates to comprehensible Java source code Syntactically and semantically Xtend has its roots in the Java pro gramming language but improves on many aspects Extension methods x5 8 enhance closed types with new functionality Lambda Expressions x6 8 concise syntax for anonymous function

Related Books