Clojure java class

Source: Internet
Author: User

Clojure java class

Why? Since clojure can call java classes, but sometimes these java classes require you to implement a subclass or pass a custom java object as a parameter, You need to compile clojure code into java code.

Do you still remember the gen-class mentioned earlier? In (ns ...), use (: gen-class), in (ns ..) (gen-class)

The following is an example. The content of the file MoveDailyAction. clj is as follows:

 

(ns kafka2hdfs.MoveDailyAction  (:import [org.apache.hadoop.fs FileSystem Path]           [java.io.IOException]           [java.text DateFormat SimpleDateFormat]           [java.util.Date]))(gen-class :name kafka2hdfs.MoveDailyAction :implements [org.apache.storm.hdfs.common.rotation.RotationAction] :state dest :init init :constructors {[String][]})(defn -init  [dest]  [[] dest])

This class is called MoveDailyAction and implements the RotationAction interface. There is an immutable public member variable dest, and an init member function will be called within the constructor. The constructor does not need to be implemented. You only need to declare the parameter form.

 

To automatically load and compile the class when running lein repl in the lein project. in clj: Add in aot. Note core. clj depends on MoveDailyAction. class, so you need to put MoveDailyAction in the front to compile

 

:aot [kafka2hdfs.MoveDailyAction kafka2hdfs.core]

 

 

Here we can see that the use of aot is to compile clojure code into a java class in advance and then use it elsewhere.

Run the following code test in repl:

 

kafka2hdfs.core=> (def o (kafka2hdfs.MoveDailyAction. ab))#'kafka2hdfs.core/okafka2hdfs.core=> (.dest o)ab


 

Parameters accepted by the constructor can be read later.

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.