moremore0812 / cqengine

Automatically exported from code.google.com/p/cqengine
0 stars 0 forks source link

ReflectiveAttribute only works for public Members #18

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. create a class with private members, accessible via getter and setters
2. Use the reflective Attribute to preform a search.
3. java.lang.NoSuchFieldException is thrown.

What is the expected output? What do you see instead?
Successful search on private member.

What version of the product are you using? On what operating system?
1.1.0

Please provide any additional information below.
The problem is caused by using getField("fieldName") on the object rather than 
getDeclaredField("fieldName"). 

I have included a patch to fix this issue.

Original issue reported on code.google.com by knitel...@gmail.com on 16 Aug 2013 at 9:03

Attachments:

GoogleCodeExporter commented 9 years ago
Fixed. This has also been included in CQEngine 1.1.0, now released which should 
sync to Maven central in the next few hours. 

Original comment by ni...@npgall.com on 16 Aug 2013 at 10:39

GoogleCodeExporter commented 9 years ago
great :)

Original comment by knitel...@gmail.com on 17 Aug 2013 at 1:40

GoogleCodeExporter commented 9 years ago
I am not quite sure why you opted for the approach of only using 
getDeclaredFields if an Exception is thrown.

The only reason that I can come up with is that getDeclaredFields does not 
iterate up the Class hierarchy.

You will find that most models in java have private members, so using an 
exception to handle this is very expensive.

If you want to make sure that you can access all members even up the class 
hierarchy I would do this manually, I have included a patch that includes a 
utility class witch will handle this.

Original comment by knitel...@gmail.com on 17 Aug 2013 at 2:37

Attachments:

GoogleCodeExporter commented 9 years ago
Yes the approach was to not break existing support for inherited fields, 
limited as it was.

I've re-implemented it, released as CQEngine 1.1.1. It should support private 
fields in superclasses now too.

BTW this code has no bearing on query performance, it's just initialization of 
the attribute. The field is cached thereafter. I think there are fewer object 
allocations via the getField + catch exception approach, than via getFields() 
so I stuck with that approach, although I haven't benchmarked it. 

Hopefully this will accommodate most use cases.

Original comment by ni...@npgall.com on 18 Aug 2013 at 9:57