Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generic arrays results in ClassCastException, as the value is actually a Collection #492

Closed
Ph3n1x opened this issue May 23, 2018 · 0 comments · Fixed by #568
Closed

Generic arrays results in ClassCastException, as the value is actually a Collection #492

Ph3n1x opened this issue May 23, 2018 · 0 comments · Fixed by #568
Assignees

Comments

@Ph3n1x
Copy link

Ph3n1x commented May 23, 2018

Expected Behavior

The loaded entity should have (depending on the specified generic type) a

  • byte[]
  • int[]
  • String[]
  • etc.

Current Behavior

The loaded entity has an

  • ArrayList (instead of byte[])
  • java.util.Collections$UnmodifiableRandomAccessList (instead of int[])
  • java.util.Collections$UnmodifiableRandomAccessList (instead of String[])

As soon as you call the getter for getting the array, a ClassCastException is raised, as an ArrayList and UnmodifiableRandomAccessList are not the requested arrays

Possible Solution

Check the generic type and do not simply save a List. As a workaround: do not use arrays as generic types, but Lists.

Steps to Reproduce (for bugs)

Here is the link to the very basic example: https://github.com/Ph3n1x/neo4j-sdn-ogm-issue-report-template/tree/master/issue-492

  1. Execute the tests. All three (byte[], int[], String[]) will all fail, because of a ClassCastException. See also the CHECKME inside the testcase

Context

In my real world project, my base class is something like BaseField and I have many classes extending it, like BytesField extends BaseField<byte[]>, StringField extends BaseField<String> and so on.
When I try to access the stored value of my fields with some sort of arrays beeing the generic type T, I get the described ClassCastException

Your Environment

@michael-simons michael-simons self-assigned this Dec 10, 2018
michael-simons added a commit that referenced this issue Dec 10, 2018
The current implementation fails in some scenarios. Including the full type tools seems overkill and as it stands, it has only tested with JDK 9 and still uses Unsafe.

Rewriting GenericTools would look pretty similar for our needs.
michael-simons added a commit that referenced this issue Dec 10, 2018
Take all declared fields of the hierarchy into consideration when building a FieldsInfo for concrete classes. Resolve type of generics fields in subtype.
michael-simons added a commit that referenced this issue Dec 10, 2018
This bug is probably unrelated.
michael-simons added a commit that referenced this issue Dec 11, 2018
Take all declared fields of the hierarchy into consideration when building a FieldsInfo for concrete classes. Resolve type of generics fields in subtype.
michael-simons added a commit that referenced this issue Dec 11, 2018
This bug is probably unrelated.
michael-simons added a commit that referenced this issue Dec 11, 2018
Take all declared fields of the hierarchy into consideration when building a FieldsInfo for concrete classes. Resolve type of generics fields in subtype.

This change also includes the tests from the original bug report.
michael-simons added a commit that referenced this issue Dec 11, 2018
This bug is probably unrelated.
michael-simons added a commit that referenced this issue Dec 11, 2018
The current implementation fails in some scenarios. Including the full type tools seems overkill and as it stands, it has only tested with JDK 9 and still uses Unsafe.

Rewriting GenericTools would look pretty similar for our needs.
michael-simons added a commit that referenced this issue Dec 11, 2018
Take all declared fields of the hierarchy into consideration when building a FieldsInfo for concrete classes. Resolve type of generics fields in subtype.

This change also includes the tests from the original bug report.
michael-simons added a commit that referenced this issue Dec 11, 2018
This bug is probably unrelated.
michael-simons added a commit that referenced this issue Dec 11, 2018
michael-simons added a commit that referenced this issue Dec 11, 2018
…ypes.

This is a backport of

* 687ae80
* e70981b
* 181f3b6

and fixes the type detection of generic fields in concrete subclasses.
It includes shaded parts of TypeResolver/Typetools as well.
michael-simons added a commit that referenced this issue Dec 11, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants