-2

I want to get the java.lang.reflect.Field of (Eg. Object.field1), but my program is going to be obfuscated. I can't use the name of the field, but I just want to get it by that reference. I also don't want to cycle through the array of fields and find the one I want.

Sorry if I worded things wrong, I don't know what I'm supposed to call that.

Linny
  • 1
  • 1
  • 1
    So, what _do_ you know about the field? What do you mean "by reference"? – Sweeper Aug 07 '21 at 03:40
  • I have access to an object, and I want to get the field from the object. Like MyObject.myField - I want to get that – Linny Aug 07 '21 at 03:53
  • Please update **the question** with any clarifications. – Stephen C Aug 07 '21 at 05:02
  • Some obfuscators can support simple reflections, changing then a name to a "correct" one too. But sounds a bit risky to use, to relay on obudscator logic to not miss your reflective call if its anything more complicated than someClass.getDeclaredField – GotoFinal Aug 24 '21 at 17:36

1 Answers1

2

You have painted yourself into a corner.

There is no way to get a field (or Field) using reflection if you don't know the field's obfuscated name AND you don't want to (or can't) cycle through all of the fields to identify the correct one.

I have a couple of suggestions:

  1. Alter the obfuscation rules; e.g. don't obfuscate this class, or the field names for this class, or this specific field name.

  2. Add a method for accessing this field so that you don't need to use reflection.

You could also figure out what the obfuscated name of the field is going to be and hard-wire it into your reflective code, but this is a bad idea. Small changes to your code are liable to make the obfuscated field name change. Then your code breaks.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216