0

I need a function that will return the names of all the private data members in my class as strings (perhaps in an array or list?), where each string is the name of a private, non final data member in my class. The non final condition is optional, but it would be nice.

1) Is this even possible? I think there is a way to retrieve all method names in a class, so I think this is possible as well.

2) I know I am asking for a hand out, but how do I do this?

EDIT

I have NO idea where to begin.

It seems java.lang.reflect is a good place to begin. I have started researching there.

Josh
  • 1,309
  • 2
  • 17
  • 37

3 Answers3

2

This should do the trick. Basically you got in a List all the fields of your class, and you remove the one who are not private. :

public static void main(String [] args){
    List<Field> list = new ArrayList<>(Arrays.asList(A.class.getDeclaredFields()));

    for(Iterator<Field> i = list.iterator(); i.hasNext();){
        Field f = i.next();
        if(f.getModifiers() != Modifier.PRIVATE)
            i.remove();
    }
    for(Field f : list)
        System.out.println(f.getName());
}

Output :

fieldOne
fieldTwo

Class A :

class A {
    private String fieldOne;
    private String fieldTwo;

    private final String fieldFinal = null;

    public char c;
    public static int staticField;
    protected Long protectedField;
    public String field;
}
user2336315
  • 15,697
  • 10
  • 46
  • 64
  • You should use `((f.getModifiers() & Modifier.PRIVATE) != Modifier.PRIVATE)` -- in addition to private, the field might also be final, static, etc. – Brian S Dec 11 '13 at 17:38
  • @BrianS How do you explain that fieldFinal doesn't appear in the resulting List ? With your code, I get fieldFinal as well in my resulting list. – user2336315 Dec 11 '13 at 17:40
  • Sorry, my comment was based on the documentation for `gotModifiers()` and the `Modifier` class, not running your code. `getModifiers()` says "Returns the Java language modifiers for the field...The Modifier class should be used to decode the modifiers." and the `Modifier` class says "The sets of modifiers are represented as integers with distinct bit positions representing different modifiers." (In other words, a bitfield.) I'll look into it. – Brian S Dec 11 '13 at 17:45
  • Ah, of course. The problem would be me misreading the original question, which also asked for _non-final_. My interpretation of `getModifiers()` appears to be correct. In that case, you'd want something like: `int mods = f.getModifiers(); if (!((mods & Modifier.PRIVATE) == Modifier.PRIVATE && (mods & Modifier.FINAL) != Modifier.FINAL)) { i.remove(); }` -- remove elements from the iterator which don't meet the criteria of (is private and is not final). Your original code will exclude any member which isn't _just_ private and nothing else (such as `private static`). – Brian S Dec 11 '13 at 17:52
1
Object someObject = getItSomehow();
for (Field field : someObject.getClass().getDeclaredFields()) {
  field.setAccessible(true); // You might want to set modifier to public first.
  Object value = field.get(someObject); 
  if (value != null) {
    System.out.println(field.getName() + "=" + value);
  }
}
M21B8
  • 1,867
  • 10
  • 20
0

You can access all public methods by Class.getDeclaredMethods() but in order to access private method you have to know the names of private methods.

To access private methods:

 Method privateMethod = MyObj.class.
    getDeclaredMethod("myPrivateMethod", null); //return private method named "myPrivateMethod"

 privateMethod.setAccessible(true); //turn off access check for reflection only

 Object o = privateMethod.invoke(MyObj, null); //call private method
Ashish Bindal
  • 995
  • 1
  • 8
  • 16
  • I am not trying to invoke private **methods**. Please read the question more carefully. – Josh Dec 11 '13 at 17:39
  • if you are writing new class, you can define the private method names in the list as class member variable. But there is no way you can get the list of private methods. – Ashish Bindal Dec 11 '13 at 17:42