开发者

Debugging clojure functions which are invoked in java code (eclipse)

开发者 https://www.devze.com 2023-03-31 17:42 出处:网络
At the following link Calling clojure from java , it is illustrated how to write a piece of clojure code, whose functionality we can then invoke directly in java source code. To reproduce, we have a c

At the following link Calling clojure from java , it is illustrated how to write a piece of clojure code, whose functionality we can then invoke directly in java source code. To reproduce, we have a clojure project called tiny, which within it has a tiny.clj source file. The tiny.clj file contains the following code :

(ns tiny
  (:gen-class
    :name com.domain.tiny
    :methods [#^{:static true} [binomial [int int] double]]))

(defn binomial
  "Calculate the binomial coefficient."
  [n k]
  (let [a (inc n)]
    (loop [b 1
           c 1]
      (if (> b k)
        c
        (recur (inc b) (* (/ (- a b) b) c))))))

(defn -binomial
  "A Java-callable wrapper around the 'binomial' function."
  [n k]
  (binomial n k))

(defn -main []
  (println (str "(binomial 5 3): " (binomial 5 3)))
  (println (str "(binomial 10042 111): " (binomial 10042 111)))
)

This is then exported into a ttt.jar file, which is then added to the "Referenced Libraries" of the tinyJava project (which is a java project). Inside the tinyJava project there is a Main.java file, which has the following code :

import com.domain.tiny;

public class Main {

    public static void main(String[] args) {

开发者_开发技巧        int j;
        j = (int)tiny.binomial(5, 3);
        System.out.println("(binomial 5 3): " + j);
        System.out.println("(binomial 10042, 111): " + tiny.binomial(10042, 111));

    }

}

The output is then :

(binomial 5 3): 10
(binomial 10042, 111): 4.9068389575068143E263

My question is, at the following point in Main.java :

j = (int)tiny.binomial(5, 3);

is it possible to step into the clojure source code? I have tried and it did not work.

Thanks

[EDIT] : the way I added the jar to the Referenced Libraries was as follows :

1)Rightclick on project tinyJava & choose properties

2)Choose : Java Build Path

3)Choose : Libraries

4)Click : "Add External Jars"

5)Then go to the location of ttt.jar

[EDIT 2] : For a scala project it seems that one can achieve this in a slightly different manner (see below), whereby one links the projects rather than explicitly exporting a jar file.


For example this seems to be quite easy to do with Scala (note the following article also discusses interop : How do you call Scala objects from Java?).

What I did was as follows :

1)Set up a Scala project called firstScala, and add a file MyPrinter.scala with the following code :

class MyPrinter{ 
  def printTerm() {
        print("hello");
  }
}

2)Then I created a Java project called firstScalaJava, to which I then added a file called Main.java with the following code :

class Main {
    public static void main(String args[]) {
        MyPrinter myPrint = new MyPrinter();
        myPrint.printTerm();
    }
}

3)Then I right clicked on firstScalaJava, selected Properties, chose Java Build Path, chose Projects tab, clicked "Add", and then selected the firstScala project.

4)Then if I put a breakpoint at "myPrint.printTerm();" and clicked F5 when the debugger has stopped at this point, the debugger automatically walks into the Scala source file.

The nice thing about this is that I did not need to export any jar files or anything like that. This is appealing because upon changing the Scala code one does not need to do anything (eclipse automatically rebuilds the scala project when one attempts to run the java project thereafter), whereas if I changed the Clojure code I would need to re-export the jar etc... (note that linking the projects in the manner described above does not work for the clojure case)

0

精彩评论

暂无评论...
验证码 换一张
取 消