lundi 5 septembre 2016

A method is protected, but I got the modifier of the method by reflection, the modifier is public. IDE is Android Studio

A class like this:

public class MethodSubject {

public String getString(String str) {
    return "hello";
}

protected void setStr(String str) {

}

int getID(int t) {
    return 100;
}

private void setID(int id) {

}

}

Then, I use reflection to get the method

Class<MethodSubject> clz=MethodSubject.class;
Method[] methods=clz.getDeclaredMethods();
for(Method method:methods){
   int modifier=method.getModifiers();
   if ((modifier& Modifier.PUBLIC)==1){
      Log.e("target,",method.toString());
   }
}

then, the log is:

public int ...MethodSubject.getID(int)
public String ...MethodSubject.getString(java.lang.String)
public void ...MethodSubject.setStr(java.lang.String)

why the modifier is changed?





Aucun commentaire:

Enregistrer un commentaire