Problem with java reflection

Hello,

In my java code I use reflection:

field.setAccessible(true).

When I test my code locally it works but when I deploy my code on Backendless and execute the code this exception is thrown:

access denied (“java.lang.reflect.ReflectPermission” “suppressAccessChecks”)

Could you help me to understand why ?

Thank you

Hello @Seb777

Unfortunately, we are limiting reflection access. But I have created an internal ticket BKNDLSS-30269 to find a solution for your problem. We will let you know the result in this thread.

Regards,
Inna

Hi, @Seb777 .
I’ve wrote my example with reflective access to the class fields/methods. Both to the my custom classes (my custom logic which i compile) and to the classes from the libs dir.
Everything works fine. Maybe it is related to the specific class/field/method.

Could you, please, describe your case more precisely.

Hi @oleg-vyalyh

Reflection works well. It’s just that field.setAccessible(true) throws an exception when the field is private. To solve my problem I had two possibilities. Set my field to public or use the related getter method. I take the last option and it works. So now it’s ok for me :slight_smile:

Don’t you have the same problem with private fields?

Thank you for your help.

hello @Seb777

no, we actually haven’t, because we always use getter or setter to access the fields.

But it would be interesting to look at your sample code (simple example of custom service which isn’t work).

Thank you @sergey.kuk and @oleg-vyalyh

Here is an example of what causes an exception:

for(Field field : object.getClass().getDeclaredFields()){
  field.setAccessible(true); // This throws an exception for a private field.
  try {
      Object value = field.get(object);
  } catch (IllegalAccessException e) {
      e.printStackTrace();
  }
}

Hi @Seb777 ,

I think my colleague was more interested in the name of the class which causes that error. Reflection should work fine with your own classes which you defined in your data model.
Could you please provide fully qualified class name and, in case if it is your own class, it’s code?

Regards, Andriy

Hi @Andriy_Konoz

Sorry I no longer have the code that was causing the problem because I did it differently. But if I encounter the problem again I will post my code here.

Thank you.