Recently, I’ve blogged about
10 Subtle Best Practices when Coding Java, and I have mentioned that you should start writing
SAMs (Single Abstract Method) now, in order to be prepared for Java 8. But there’s another language gem out there, which comes in handy every once in a while, and that’s
Eclipse Xtend. Xtend is a “dialect” of the Java language, compiling into Java source code, which then compiles into byte code.
Here’s a quickie showing how easily recursive file system operations can be done with
Xtend, Lambdas, and ThreadPools.
class Transform {
// This is the thread pool performing
// all the "hard" work
static ExecutorService ex;
def static void main(String[] args) {
// Initialise the thread pool with
// something meaningful
ex = Executors::newFixedThreadPool(4);
// Pass the root directory to the
// transform method
val in = new File(...);
// Recurse into the file transformation
transform(in);
}
def static transform(File in) {
// Calculate the target file name
val out = new File(...);
// Recurse into directories
if (in.directory) {
// Pass a FileFilter in the form of an
// Xtend lambda expression
for (file : in.listFiles[path |
!path.name.endsWith(".class")
&& !path.name.endsWith(".zip")
&& !path.name.endsWith(".jar")
]) {
transform(file);
}
}
else {
// Pass an Xtend lambda expression to
// the ExecutorService
ex.submit[ |
// Read and write could be implemented
// in Apache Commons IO
write(out, transform(read(in)));
];
}
}
def static transform(String content) {
// Do the actual string transformation
}
}
Granted, with Java 8, we’ll get lambdas as well, and that’s awesome. But Xtend has a couple of other nice features that can be seen above:
- Passing lambdas to a couple of JDK methods, such as File.listFiles() or ExecutorService.submit()
- Local variable type inference using val, var, or for
- Method return type inference using def
- Ability to omit parentheses when passing a lambda to a method
- Calling getters and setters by convention, e.g. path.name, instead of path.getName(), or in.directory, instead of in.isDirectory()
- You could also omit semi-colons, although I don’t personally think that’s a good idea.
Xtend is Java 8 already now, which can be very useful for scripts like the above
Like this:
Like Loading...
Published by lukaseder
I made jOOQ
View all posts by lukaseder
Scala
Yes, Scala will go a lot further than Xtend. From a Eclipse / Java developer’s perspective, Xtend can be a good choice for short-lived, simple things like the above script, though, as the Xtend-Java integration is much simpler than the Scala-Java integration.