2

I am trying to create a new SimpleLinkResolver in Clojure. This is the JavaDoc:

http://prismicio.github.io/java-kit/io/prismic/SimpleLinkResolver.html

My clojure code is:

(def lr (new io.prismic.SimpleLinkResolver))

but at the repl I get the following error:

CompilerException java.lang.InstantiationException, compiling:(form-init460449823042827832.clj:1:1)

I have no problem creating a java.util.Date:

(def d (new java.util.Date))
=> #'prismic-clojure.core/d
d
=> #inst"2018-03-17T10:30:36.016-00:00"

The above JavaDoc does say that SimpleLinkResolver is deprecated because the interface LinkResolver (http://prismicio.github.io/java-kit/io/prismic/LinkResolver.html) has default methods and so can be implemented directly. So I gave this a go to:

(def lr (new io.prismic.LinkResolver))
CompilerException java.lang.IllegalArgumentException: No matching ctor found for interface io.prismic.LinkResolver,

And I get this "no ctor" error - which I am guessing means the compiler can't find a constructor?

Questions:

  1. Why does the first effort produce an InstantiationException?
  2. Not being familiar with the Java-8 default methods, how would I create a new LinkResolver using its default methods?

Thanks

1 Answer 1

3

Why does the first effort produce an InstantiationException?

You can't instantiate an abstract class:

public abstract class SimpleLinkResolver

Not being familiar with the Java-8 default methods, how would I create a new LinkResolver using its default methods?

You'll need to implement LinkResolver interface, which can be done using Clojure's reify:

(def resolver
  (reify LinkResolver
    (^String resolve [this ^Fragment$DocumentLink link]
      "a string"))) ;; put actual impl. here
(.resolve resolver nil)
;; => "a string"

Note you need to type-hint the return value (and arguments) because .resolve() is an overloaded method.

Also, you typically see (Class.) dot-syntax rather than (new Class) to instantiate Java classes.

Sign up to request clarification or add additional context in comments.

1 Comment

thank you for your very informative answer. Much appreciated!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.