REFLECTION: Getting the field from a class without the name

414 Views Asked by At

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.

1

There are 1 best solutions below

0
Stephen C On

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.