Java Magazine, March/April 2016
ORACLE COM JAVAMAGAZINE MARCH APRIL 2016 61 jvm languages on types is that you avoid code duplication when augmenting several possibly unrelated types Generating Adapter Classes While Golo has few issues when calling Java APIs there are cases when these APIs expect arguments to be objects that extend certain interfaces or base classes Golo provides an API to generate adapter classes An adapter class extends a base class implements a set of interfaces and overrides and implements methods The Adapter API is available through the gololang Adapters import statement and then it can be used to create objects such as the following one let obj Adapter extends java lang Object interfaces java io Serializable java util Enumeration implements hasMoreElements this true implements nextElement this 100 overrides toString this super Strange newInstance println obj getClass println obj getClass getInterfaces toString println obj hasMoreElements println obj nextElement println obj toString Executing the code above prints class Golo Adapter 0 interface gololang GoloAdapter interface java io Serializable interface java util Enumeration true 100 Strange The API performs soundness verifications for example it checks that all interface methods are being implemented and dynamically generates the adapter class as JVM bytecode Instead of method names it is also possible to use so that a single function can implement many methods or override all methods The Golo documentation has examples of using adapters for dynamically generating proxies Calling Golo from Java Calling Golo from Java is usually very easy There are two options direct invocations and code evaluations Direct invocations The first option is to compile Golo source files and add the generated bytecode plus the Golo runtime JAR file and dependencies to the Java application classpath From the point of view of a Java class a Golo module is a class with static methods Given the following module module my great Module struct Point x y function a n n 1 function b a b a b The javap decompiler reveals the following public methods javap my great Module Compiled from compil golo public class my great Module public static java lang String imports public static java lang String augmentations
You must have JavaScript enabled to view digital editions.