1

I am using an open source framework (libGDX to be exact), however I want to edit internal behavior of one class and use the rest of the framework as is. The problem is, that I cannot access protected members of that class when I extend it. I do not know if reflection is the best thing for game. The class is used in the other classes of the framework and I want to use them too.

EDIT: found out that the members were only protected (original question included private members), but I was not able to access them outside the package

Nezbeda
  • 131
  • 2
  • 12
  • If its open source, why not contribute to it to improve it or fork rather than going something insane? – Darren Forsythe Sep 03 '17 at 19:36
  • with my internet speed and cpu, I wanted to ask before I compile that framework by myself – Nezbeda Sep 03 '17 at 19:44
  • 1
    A better question is why exactly do you want to patch the library. If it's just about accessing some private fields - well, reflection can do this for you. But I'd first consider why are those fields private in first place. If they were not intended for public usage, it might be a good idea to not do this. Developers will feel completely free to change private implementation details as they wish so your clever hack may stop working in the next version of the library. – lexicore Sep 03 '17 at 19:52
  • Someone has the same intention as you, and suggested on [libgdx issue](https://github.com/libgdx/libgdx/issues/4883) to change default to `protected` for more convenient accessibility. If you like, you might want to give some input there. This is by no mean I'm in totally with one way or another. – haxpor Sep 07 '17 at 06:41
  • @GhostCat added my solution to your answer – Nezbeda Sep 12 '17 at 19:29
  • I appreciate the quick comeback :-) – GhostCat Sep 12 '17 at 19:30

1 Answers1

3

Three options:

  • you suggest a patch to the community behind that library. Try to convince the owners to change their code - so that your problem can be addressed in reasonable ways
  • you create your own private fork. It is open source!
  • yes, you might be able to use reflection. This one comes last because reflection is easy to get wrong and breaks as soon as somebody starts using security managers - or when the library owner make changes such as renaming classes or fields. See here for more detailed information.
  • use inheritance, you have to create package inside your project matching the target class package name. Create child class there so you can access protected members. I think it is obvious which option would be better for you - although initially it means more work to convince people that your idea has merit.

Alternatively you might ask yourself if your idea is really a good idea. Maybe you just don't understand how to use that library properly? But we can't help with that given your current input...

Nezbeda
  • 131
  • 2
  • 12
GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • so there aren't any other options, instead compiling a whole framework?(Can't convince them to change the code, because these changes would not be universal as theirs code is) – Nezbeda Sep 03 '17 at 19:42
  • 1
    @MichalLebeda Compiling the whole framework is the least problem you have on this path. – lexicore Sep 03 '17 at 19:53