Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I troubleshoot a ArrayStoreException

I have read many posts about ArrayStoreExceptions, most of them fall into one of these categories:

  • basic misunderstanding (putting a base class into a array declaration of the child class)
  • similar misunderstandings while getting this error converting from lists to arrays
  • "covariant" issues surrounding arrays vs generics

Mine doesn't fall into any of these categories. There is one other case, the OP suspected a bug of some sort that only showed up on Unix. I think I'm having that case and I'm not sure how to get any closer to the issue.

I've broken the code down as much as I can, it looks like this:

System.out.println("Declaring RecordValues array");
RecordValues[] rv = new RecordValues[3];
System.out.println("This array is meant for "+rv.getClass().getComponentType());
System.out.println("Adding user defaults which is: "+userdefaults.getClass().getName());
System.out.println("Its parent is: "+userdefaults.getClass().getSuperclass().getName());
rv[0] = userdefaults;
System.out.println("Adding templvalues which is: "+tmplvalues.getClass().getName());
rv[1] = tmplvalues;
System.out.println("Adding sessionvalues which is: "+tmplvalues.getClass().getName());
rv[2] = sessionvalues;

And it outputs:

[ant:createwo] Declaring RecordValues array
[ant:createwo] This array is meant for class org.kp.mbe.arscli.datamap.RecordValues
[ant:createwo] Adding user defaults which is: org.kp.mbe.arscli.datamap.UserValues
[ant:createwo] Its parent is: org.kp.mbe.arscli.datamap.RecordValues
:createwo FAILED <-- 
...
Caused by: java.lang.ArrayStoreException: org.mbe.arscli.datamap.UserValues

UserValues is a sub-type of RecordValues. I've created some simple tests using the classes in question but can't replicate the error. Also (here's the really weird part), this has been working for over six months. I've made changes in other parts of the tool but nothing anywhere near these libraries. It suddenly just popped up.

I'm wondering what else might cause the ArrayStoreException. I'm 100% sure that UserValues is a sub-class of RecordValues yet, as soon as I try to add it to the array, it fails. Host is 64bit RH linux, OpenJDK java version 1.7.0.45.

I know what the ArrayStoreException is telling me, my question is how can I take this a step further in troubleshooting? If UserValues IS A RecordValues, where do I go from here??

like image 797
AndyJ Avatar asked Jun 12 '14 19:06

AndyJ


1 Answers

After posting, I gave up on the array and changed this to a List which, while still failing, gave a much more concrete problem:

Caused by: java.lang.LinkageError: loader constraint violation: when
resolving method
"org.mbe.arscli.datamap.ValueResolver.resolve(Ljava/util/List;)Lorg/mbe/arscli/datamap/RecordValues;"
the class loader (instance of org/apache/tools/ant/loader/AntClassLoader5) of the current class,
org/build/ant/CreateRemedyWo, and the class loader (instance of java/net/URLClassLoader) for resolved class,
org/mbe/arscli/datamap/ValueResolver, have different Class objects for the type )Lorg/mbe/arscli/datamap/RecordValues; used in the
signature 

As @Taylor guessed, this was being caused by multiple classloaders via gradle. Although I hadn't changed this code in a long time, I had recently changed the dependency declarations in the build file. Fortunately, the error I got from the List was more precise than the error from the Array!

like image 180
AndyJ Avatar answered Oct 20 '22 16:10

AndyJ