<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>Clojure Q&amp;A - Recent questions tagged gen-class</title>
<link>https://ask.clojure.org/index.php/tag/gen-class</link>
<description></description>
<item>
<title>Why gen-class does not generate default interface methods?</title>
<link>https://ask.clojure.org/index.php/13178/why-gen-class-does-not-generate-default-interface-methods</link>
<description>&lt;p&gt;When using gen-class, the generated .class doesn't have the default methods from java interfaces.&lt;/p&gt;
&lt;p&gt;I created &lt;a rel=&quot;nofollow&quot; href=&quot;https://github.com/ericdallo/clojure-sample/tree/clojure-gen-class-default-method&quot;&gt;this repro&lt;/a&gt;, you can confirm a exception is throw when trying to call the method from the generated class from gen-class:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;clj -T:build jar&lt;/code&gt; to generate the classes at target/classes&lt;/li&gt;
&lt;li&gt;&lt;code&gt;java -cp &lt;/code&gt;clj -Spath&lt;code&gt;  client/main/Main.java&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;A exception is throw:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;Exception in thread &quot;main&quot; java.lang.UnsupportedOperationException: otherthing (clojure-sample.foo/-otherthing not defined?)
    at clojure_sample.Foo.otherthing(Unknown Source)
    at client.main.Main.main(Main.java:9)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The not ideal workaround is to add the default method in the namespace implementing what is done in the default method inside the java interface.&lt;/p&gt;
</description>
<category>Compiler</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/13178/why-gen-class-does-not-generate-default-interface-methods</guid>
<pubDate>Mon, 21 Aug 2023 17:30:20 +0000</pubDate>
</item>
<item>
<title>Fails to load gen-classed class with pf4j from uberjar</title>
<link>https://ask.clojure.org/index.php/10093/fails-to-load-gen-classed-class-with-pf4j-from-uberjar</link>
<description>&lt;p&gt;The problem here might be more related to the pf4j default classloader but still:&lt;/p&gt;
&lt;p&gt;I have a clojure plugin generating a class &lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(gen-class
 :name myplugin.SomeExtension
 :implements [plugin.SomeExtensionPoint]
 :prefix &quot;-&quot;
 :impl-ns my-plugin.some-extension)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The plugin jar  is loaded in my clojure application using pf4j. When it is compiled with &lt;code&gt;lein jar&lt;/code&gt; the class loads (but then I naturally have to put any plugin dependencies in the apps &lt;code&gt;project.clj&lt;/code&gt;). When compiled with &lt;code&gt;lein uberjar&lt;/code&gt; it fails with:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt; Execution error (IllegalStateException) at myplugin.SomeExtension/&amp;lt;clinit&amp;gt; (REPL:-1).
; Attempting to call unbound fn: #'clojure.core/refer
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;with top of call stack&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;jdk.internal.reflect.NativeConstructorAccessorImpl/newInstance0 (NativeConstructorAccessorImpl.java:-2)
jdk.internal.reflect.NativeConstructorAccessorImpl/newInstance (NativeConstructorAccessorImpl.java:62)
jdk.internal.reflect.DelegatingConstructorAccessorImpl/newInstance (DelegatingConstructorAccessorImpl.java:45)
java.lang.reflect.Constructor/newInstance (Constructor.java:490)
java.lang.Class/newInstance (Class.java:584)
org.pf4j.DefaultExtensionFactory/create (DefaultExtensionFactory.java:38)
org.pf4j.ExtensionWrapper/getExtension (ExtensionWrapper.java:37)
org.pf4j.AbstractPluginManager/getExtensions (AbstractPluginManager.java:971)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I've struggled a bit but I am unsure about exactly what happens here and how to solve it?&lt;br&gt;
Any ideas are most welcome!&lt;/p&gt;
&lt;p&gt;(and the uberjar profile uses &lt;code&gt;{:aot :all :omit-source true}&lt;/code&gt;)&lt;/p&gt;
</description>
<category>Java Interop</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/10093/fails-to-load-gen-classed-class-with-pf4j-from-uberjar</guid>
<pubDate>Tue, 26 Jan 2021 16:23:57 +0000</pubDate>
</item>
<item>
<title>How to invoke native C-functions in Clojure via JNR-FFI?</title>
<link>https://ask.clojure.org/index.php/9915/how-to-invoke-native-c-functions-in-clojure-via-jnr-ffi</link>
<description>&lt;p&gt;Hi Everyone,&lt;/p&gt;
&lt;p&gt;I want to call native functions in a C-Library from Clojure via &lt;a rel=&quot;nofollow&quot; href=&quot;https://github.com/jnr/jnr-ffi&quot;&gt;JNR-FFI&lt;/a&gt; without having to write Java code.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(defproject mypkg
	:dependencies [[org.clojure/clojure &quot;1.10.1&quot;]
				   [com.github.jnr/jnr-ffi &quot;2.2.0&quot;]])
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Based on the jnr-ffi examples &lt;a rel=&quot;nofollow&quot; href=&quot;https://github.com/jnr/jnr-ffi-examples/blob/master/getpid/src/main/java/getpid/Getpid.java&quot;&gt;here&lt;/a&gt; and &lt;a rel=&quot;nofollow&quot; href=&quot;https://www.programcreek.com/java-api-examples/?api=jnr.ffi.LibraryLoader&quot;&gt;here&lt;/a&gt; a Java wrapper might look like&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;package blahblah;

public class BlahNative {
	private static int[] intDummy;
	private static double[] doubleDummy;

	public BlahNative() {
	}

	public static native void native_a(char var0, char var1, int var2, int var3, int var4, double[] var7, int var8, int var9);

	public static native void native_b(char var0, char var1, int var2, int var3, int var4, double[] var7, int var8, int var9);

	static {
		lib = LibraryLoader
            .create(LibSodium.class)
            .search(&quot;/usr/local/lib&quot;)
            .search(&quot;/opt/local/lib&quot;)
            .search(&quot;/usr/lib&quot;)
            .search(&quot;/lib&quot;)
            .load(LIBRARY_NAME);
        initializeLibrary(lib);
    }
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I presume my Clojure namespace should use the &lt;a rel=&quot;nofollow&quot; href=&quot;http://clojure-doc.org/articles/language/interop.html&quot;&gt;gen-class&lt;/a&gt; feature for implementing Java classes.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(ns mypkg.native
	(:import [jnr.ffi LibraryLoader])
	(:import [jnr.ffi.annotations IgnoreError])
	(:import [jnr.ffi.provider FFIProvider])
    (:gen-class))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then,&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;p&gt;How do I write a &lt;a rel=&quot;nofollow&quot; href=&quot;https://www.baeldung.com/java-native&quot;&gt;native&lt;/a&gt; method in Clojure? In other words, do I even need to specify the method type (as &lt;code&gt;native&lt;/code&gt;) when using Clojure's Java interop features?&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;After loading the native library I do not know how to access the C-functions in the library. How do I do it?&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;To avoid reloading the library for every Clojure wrapper function of each C-functions, can I write a method like&lt;/p&gt;
&lt;p&gt; &lt;code&gt;(defn lib (. LibraryLoader load LIBRARY_NAME))&lt;/code&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;and then access the native functions in it?&lt;/p&gt;
&lt;p&gt;I am neither a C nor a Java expert so any guidance will be much appreciated.&lt;/p&gt;
&lt;p&gt;Thanks.&lt;/p&gt;
</description>
<category>Java Interop</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/9915/how-to-invoke-native-c-functions-in-clojure-via-jnr-ffi</guid>
<pubDate>Fri, 04 Dec 2020 13:57:56 +0000</pubDate>
</item>
<item>
<title>Is it possible to gen-class with a private final field?</title>
<link>https://ask.clojure.org/index.php/8195/is-it-possible-to-gen-class-with-a-private-final-field</link>
<description>&lt;p&gt;How to create a Java class with a private final field in Clojure?&lt;/p&gt;
&lt;p&gt;ClojureDocs for &lt;a rel=&quot;nofollow&quot; href=&quot;https://clojuredocs.org/clojure.core/gen-class&quot;&gt;gen-class&lt;/a&gt; says that &lt;code&gt;state&lt;/code&gt; field will be public&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;:state name - if supplied, a public final instance field with the given name will be&lt;br&gt;
created. &lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;So, in other words, do we have a way to create a class and instantiate a java object with the encapsulated state? &lt;/p&gt;
</description>
<category>Compiler</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/8195/is-it-possible-to-gen-class-with-a-private-final-field</guid>
<pubDate>Fri, 26 Jul 2019 01:07:51 +0000</pubDate>
</item>
</channel>
</rss>