1

I am trying to extend the https://github.com/tennaito/rsql-jpa library to support EnumSet fields.

I have everything working with regard to the actual query against the database, if I hardcode the ArgumentParser to convert EnumSet properties values to a specific query.

My issue is that given a query enumsetfield=IN=(enumvalue) the ArgumentParser gets a JavaType: class java.util.EnumSet and the string enumvalue.

How do I get the element type from the JavaType? I also have access to a org.hibernate.jpa.criteria.path.SingularAttributePath if that could possible contain the elementType.

Torben Pi Jensen
  • 850
  • 9
  • 27
  • Using reflection to get the generic type at runtime could be a solution. On the [Get generic type of class at runtime](https://stackoverflow.com/questions/3403909/get-generic-type-of-class-at-runtime) thread there are some details about how this could be implemented –  Mar 18 '19 at 14:12
  • Yeah, I was almost going in that direction, but that would require me to replace the EnumSets everywhere in the solution, and custom mappers. – Torben Pi Jensen Mar 18 '19 at 20:09

2 Answers2

1

You might be able to use reflection to obtain the non-public field in the EnumSet class that stores the element class. For example, for OpenJDK 8, here is the source code for EnumSet, and the field for the element class is called elementType. However, this would be implementation-specific to your class library implementation and not portable.

newacct
  • 119,665
  • 29
  • 163
  • 224
  • And while this is useful and used by many frameworks, it will break with JDK 16 (has given warnings earlier but now will throw exception). – StaxMan Apr 15 '21 at 00:26
0

I ended up finding the information needed on the SingularAttributePath: path.attribute.member.signature which is in the form: Ljava/util/EnumSet<Lmy.project.enum;>; - this is not optimal, I had to rewrite the rsql-jpa a lot, and I ended up realizing that an IN query makes little sense with regard to comparing two collections. So the query now is enumsetfield==enumvalue

Torben Pi Jensen
  • 850
  • 9
  • 27