0

Using Groovy and JNA to implement GetWindowRect() for Windows environment. The code come from Hovercraft Full Of Eels' answer here. The only difference is I am using Groovy instead of Java.

So instead of

int[] rect = {0, 0, 0, 0};

My code has

int[] rect = [0, 0, 0, 0]

This results in a ClassCastException exception being thrown. So I tried the following ways of initializing rect as int[]:

def rect = [0, 0, 0, 0] as int[]
def rect = (int[]) [0, 0, 0, 0]
def rect = [0, 0, 0, 0].toArray(new int[0])

But none of them solve the casting issue. What am I doing wrong?

Here is the stack trace:

java.lang.ClassCastException: [I cannot be cast to [Ljava.lang.Object;

at com.sun.jna.Function.concatenateVarArgs(Function.java:763)
at com.sun.jna.Library$Handler.invoke(Library.java:207)
at com.sun.proxy.$Proxy21.GetWindowRect(Unknown Source)
at bnsf.create_trains.JNAWin32ApiInterface$JNAUser32$GetWindowRect$4.call(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:110)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:130)
at bnsf.create_trains.WindowsApp.getRect(WindowsApp.groovy:178)
at bnsf.create_trains.WindowsApp$getRect$0.callStatic(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallStatic(CallSiteArray.java:53)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callStatic(AbstractCallSite.java:191)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callStatic(AbstractCallSite.java:211)
at bnsf.create_trains.WindowsApp.kickoff_mainframe(WindowsApp.groovy:94)
at bnsf.TestAutoMain.TestAutoMainMethod(TestAutoMain.java:60)
Community
  • 1
  • 1
Ed Ramos
  • 1
  • 3
  • What's the actual exception? Can you add it to your question? Looks like you have an array of Integers, and it's trying to make them into an array of Objects somewhere... – tim_yates Mar 08 '17 at 17:53
  • Certainly, i've added the stacktrace. – Ed Ramos Mar 10 '17 at 15:45

1 Answers1

0

The answers you post generally work for me in groovysh, I tried

int[] rect = [0,0,0,0]
rect=[0,0,0,0]
rect=[0,0,0,0] as int[]

and they all worked, HOWEVER the example you quote uses:

int[] rect = {0, 0, 0, 0};

Which does not seem to work in groovy (And honestly I'm surprised it works in java but it does, you learn something new every day)

I'm guessing the difference is that groovy uses {} for closures and has better ways to initialize arrays so there are some forms of initialization it disallowed to avoid confusion.

Is there any chance you are using an old version of groovy or maybe the statically typed version?

Bill K
  • 62,186
  • 18
  • 105
  • 157
  • Our project uses Groovy version 2.4.1 which was the latest version when we first started. Is it safe to say that this is an external library issue since the groovy code worked in your environment? – Ed Ramos Mar 10 '17 at 15:51
  • I would try the assignments in your groovysh or groovyconsole,, if they still fail then it certainly feels like an environment issue. – Bill K Mar 10 '17 at 16:50