java 混合 clojure,java与maven和eclipse中的clojure混合

i created a sample polyglot program. i have a sensor and a robot implemented in java and AI implemented in clojure. and i can't connect maven properly

--src/main/java/clojuretest

|

DistanceSensor.java

AI.clj (uses DistanceSensor)

Robot.java (uses AI)

DistanceSensor.java:

package clojuretest;

public class DistanceSensor {

public int getValue() {return 5;}

}

AI.clj:

(ns clojuretest.AI

(:gen-class :methods [[isObstacleAhead [] boolean]]))

(defn -isObstacleAhead [this] (< (.getValue (clojuretest.DistanceSensor.)) 10))

Robot.java:

package clojuretest;

public class Robot {

public boolean shouldStop() {

return new AI().isObstacleAhead();

}

}

i can even manually force maven to compile it:

mvn clean clojure:compile produces error - no DistanceSensor class (but for some reason creates AI.class). so then

mvn compile sees AI.class and compiles everything correctly and tests pass. but what can i do to make mvn clean compile pass? how should my pom.xml look like? also what can i do to make eclipse stop complaining about non existing AI.class?

解决方案

I think :gen-class is usually a code smell, as is trying to instantiate such a class from Java code with new AI().

Here's an alternative approach that can solve this problem of cyclic dependencies:

Define AI as a Java interface in your Java code

Write a Clojure function to create an instance conforming to the interface using reify

Dynamically invoke the Clojure function from Java (e.g. using the technique outlined in this blog post)

You now have an instance of the AI interface that you can use however you like in Java

The advantage is this approach is that everything will work smoothly, in particular:

The Java code base can be compiled independently of the Clojure code

The Clojure code base can access all the defined Java classes and interfaces

You don't need any special IDE / Maven config. In fact, you can treat is as just a regular Java app that happens to include clojure.jar as a dependency.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值