Java interoperability library
This library adds enhanced Java interoperability functions to Ersatz Picolisp.Code is inspired somewhat by Clojure Java interop conventions that are beaten to the ground until they somewhat resemble PicoLisp conventions.
Also, the error messages for calling java are more detailed:
: (javaImport java.math.BigInteger)
: (.divide> BigInteger.ONE BigInteger.ZERO)
!? (5 $65586123 $1446983876 $1412601264)
java.lang.reflect.InvocationTargetException -> java.lang.reflect.InvocationTargetException -> java.lang.ArithmeticException: BigInteger divide by zero
:
Repository:
http://code.google.com/r/srborlongan-picolisp/source/listUsage:
- (javaImport java.lang.String ...) or (javaImport (java.lang String ...) ...)
- # Adds the static and instance methods of String to the namespace
- # Either use full class names or lists that contain the package prefix first, then the rest of the full class names
- (javaWhat String)
- # Shows the static (@ClassName.@Method) and instance (.@Method>) methods associated with the short class name
- -> ((String.join String.package String.format String.copyValueOf String.valueOf String.new String.CASE_INSENSITIVE_ORDER String.class .concat> .contains> .endsWith> .codePointBefore> .regionMatches> .codePointAt> .toUpperCase> .matches> .compareToIgnoreCase> .replace> .getChars> .contentEquals> .intern> .substring> .getBytes> .indexOf> .toLowerCase> .replaceAll> .codePointCount> .offsetByCodePoints> .trim> .replaceFirst> .equalsIgnoreCase> .isEmpty> .toCharArray> .split> .lastIndexOf> .hash32> .startsWith>))
- @ShortClassName.@StaticMemberName & .@InstanceMethodName> & .@InstanceFieldName:
- # Aforementioned functions for invoking static and instance members (fields and methods).
- # Instance method invokers may also be used as Java-8-style method references if invoked with T and the instance object
- Example:
- (javaImport java.io.PrintStream (java.lang System String) )
- System.out # static field
- (System.gc) # static method invocation
- (.toString> System.out) # instance method invocation
- (.println> System.out "PicoLisp") # instance method invocation
- (.println> T System.out) # instance method reference
- ((.println> T System.out) (.toString> System.out) ) # instance method reference invocation
- # This is equivalent to
- # Consumer<String> println = System.out::println;
- # println.apply(System.out.toString());
- # Factoring println into a function allows Java-8 and Ersatz users to map functions with associated instances and other Lispy stuff.
- @ShortClassName.package & @ShortClassName.class & @ShortClassName.new
- # Aforementioned globals and function for the class's package, class, and instatiator.
- # Instantiator functions are currently indiscriminate; calling System.new will merit a NoSuchMethodException.
- # Instantiator functions are intended to be nigh-universal; calling String.new will merit a new String, while calling Runnable.new with the needed function body will merit a new Runnable.
- Example:
- (javaImport (java.lang System Runnable Thread) )
- System.package # package
- System.class # class
- (String.new "PicoLisp") # constructor invocation
- (Runnable.new "run" () (prinl (String.new "PicoLisp") ) ) # interface instatiation via 'interface function
- (Runnable.new () (prinl (String.new "PicoLisp") ) ) # Java-8 style functional interface instatiation
- (Runnable.new (prinl (String.new "PicoLisp") ) ) # Shortcut for no-arg Java-8 style functional interface instatiation
- (.start> (Thread.new (Runnable.new (prinl (String.new "PicoLisp") ) ) ) ) # Instantiator abuse
- java+
- # Enhancement to java which allows the invocation of most methods that need javac for invocation, like Collection.emptyArray().iterator().hasNext()
- # Used by every static and instance method defined via javaImport
- Example:
- (javaImport (java.util Collection Iterator Collections) )
- (.hasNext> (.iterator> (Collections.emptySet) ) )
- # Note that in vanilla Ersatz invoking iterator on an EmptySet, or hasNext on an Set.Iterator both throw an IllegalAccessException (can not access a member of class)
http://picolisp.com/wiki/?javaiterop
12mar13 | srborlongan |