EsotericSoftware / reflectasm

High performance Java reflection
BSD 3-Clause "New" or "Revised" License
1.53k stars 222 forks source link

could you support private members access right? #63

Closed kevin-wang001 closed 6 years ago

kevin-wang001 commented 6 years ago

Could you support private members access right? Then, this framework will be more popular, and it's will be wilder used!

Using asm generates access code like : public class XXXMethodAccess extends MethodAccess { public Object invoke(Object paramObject, int methodIndex, Object[] paramArrayOfObject) { XXX xxx = (XXX) paramObject; try { switch (methodIndex) { case 0: xxx.method0((String[]) paramArrayOfObject[0]); return null; case 1: return xxx.method1((String) paramArrayOfObject[0]); } } catch (java.lang.IllegalAccessError e) { Method method = methods.get(methodIndex); method.setAccessible(true); try { return method.invoke(object, args); } catch (Exception e) { throw new IllegalStateException("invoke method error:" + method.getName(), e); } } throw new IllegalArgumentException("Method not found: " + methodNames[paramInt]); } }

NathanSweet commented 6 years ago

You can write your own fallback if ReflectASM isn't able to access a field. Note your code above won't work in later versions of Java where setAccessible is not allowed.