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

Use Class.getDeclaredField instead of Class.getField #302

Open
victornoel opened this issue Jun 14, 2022 · 2 comments
Open

Use Class.getDeclaredField instead of Class.getField #302

victornoel opened this issue Jun 14, 2022 · 2 comments
Labels

Comments

@victornoel
Copy link

Hi,

I encountered that issue with v3 but I believe the problem exists also in v4.

Basically in

Field field = clazz.getField(fieldName);
you call Class.getField and then try to make it visible via Field.setAccessible, but this is incorrect by design as Class.getField only return public fields!

Instead you should be using Class.getDeclaredField.

In practice, it means that it is always impossible to access private fields from templates.

In v3, the issue was in ASTExpr but I guess this won't be fixed there? :)

@parrt
Copy link
Member

parrt commented Jun 18, 2022

Hmm...good point. Looks like that won't get inherited fields though:

https://stackoverflow.com/questions/16966629/what-is-the-difference-between-getfields-and-getdeclaredfields-in-java-reflectio

@parrt parrt added the type:bug label Jun 18, 2022
@victornoel
Copy link
Author

@parrt right, I didn't know that. It actually makes sense because diverse class in an object hierarchy can have the same private fields... and then it means that maybe it's actually a bad idea to rely on the private fields/methods of an object then?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants