This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2022-04-06
Channels
- # announcements (33)
- # babashka (13)
- # babashka-sci-dev (23)
- # beginners (94)
- # calva (105)
- # cider (37)
- # circleci (1)
- # clj-kondo (45)
- # cljs-dev (59)
- # cljsrn (2)
- # clojure (145)
- # clojure-czech (1)
- # clojure-europe (19)
- # clojure-nl (18)
- # clojure-norway (13)
- # clojure-portugal (1)
- # clojure-uk (5)
- # clojurescript (19)
- # community-development (2)
- # conjure (3)
- # copenhagen-clojurians (1)
- # core-async (34)
- # data-science (6)
- # datomic (25)
- # emacs (43)
- # fulcro (19)
- # graalvm (7)
- # graalvm-mobile (12)
- # graphql (10)
- # honeysql (3)
- # hyperfiddle (3)
- # improve-getting-started (2)
- # interop (20)
- # kaocha (3)
- # lsp (16)
- # meander (7)
- # off-topic (22)
- # other-languages (14)
- # portal (15)
- # releases (1)
- # rewrite-clj (2)
- # ring (1)
- # shadow-cljs (119)
- # spacemacs (19)
- # sql (65)
- # testing (4)
- # tools-deps (11)
- # xtdb (29)
I'm trying to do this code in Clojure: https://github.com/robbieh/AWTForeignXWindow/blob/main/src/main/java/nundrum/net/TryReflect.java
Frame frame = new Frame("test");
frame.setVisible(true);
frame.setVisible(false);
ComponentAccessor acc = AWTAccessor.getComponentAccessor();
ComponentPeer peer = acc.getPeer(frame);
Class pc = peer.getClass();
try {
Method gs = pc.getMethod("getShell",null); gs.setAccessible(true);
Object o = gs.invoke(peer);
out.format("Got ID %d", (long) o);
}
catch (NoSuchMethodException x) { x.printStackTrace(); }
catch (IllegalAccessException x) { x.printStackTrace(); }
catch (InvocationTargetException x) { x.printStackTrace(); }
(def f (new Frame "AWT test"))
(. f setSize 400 400)
(. f setLayout (new GridLayout 3 1))
(class f) ;;java.awt.Frame
(supers (class f));; #{java.awt.Container java.io.Serializable java.awt.Window java.awt.image.ImageObserver java.awt.Component java.awt.MenuContainer java.lang.Object javax.accessibility.Accessible}
(r/reflect (.getAccessibleContext f) )
(def acc (AWTAccessor/getComponentAccessor))
(.getBounds acc f);; #object[java.awt.Rectangle 0x40af415a "java.awt.Rectangle[x=1280,y=0,width=400,height=400]"]
(def peer (.getPeer acc f) )
(class peer);; sun.awt.X11.XFramePeer
(def pc (.getClass peer))
(def gsm (.getMethod pc "getShell" nil) );; #object[java.lang.reflect.Method 0x45a836a "public long sun.awt.X11.XDecoratedPeer.getShell()"]
(.invoke gsm peer)
;;Execution error (IllegalArgumentException) at jnatest.core/eval9293 (form-init8734797063564353548.clj:157).
;;No matching method invoke found taking 1 args for class java.lang.reflect.Method
;;Clojure: class java.lang.IllegalArgumentException
I suspect (def gsm (.getMethod pc "getShell" nil) )
- that nil there should be an empty Class array (into-array Class [])
and then on the invoke, should be an Object array of the peer
So there's a state
field on this object.
https://github.com/openjdk/client/blob/master/src/java.desktop/unix/classes/sun/awt/X11/XFramePeer.java#L47
(.getField pc "state")
;;Clojure: class java.lang.NoSuchFieldException
Is there something as obviously wrong there?well that class isn't public so that won't be accessible via reflection
it is possible (under some constraints) to make it accessible by calling (.setAccessible field true)
that's subject to the Java security policy, and support has changed several times over the JDK's lifetime, and in Java 9+ you probably start to run into module visibility issues
but it might work! :)
I guess you're not even seeing the field though
which Java version are you on? they've really started locking these sun classes down hard as of Java 16
Oh yeah you want declared here