vendredi 23 septembre 2016

How to access private inner class using reflection?

How to insert or retrive values from this map? How to access private inner class using reflection?

public class Sample {
    private ConcurrentHashMap<String, TestPojo> testHash;

    private class TestPojo {
        ...
    }

    public ConcurrentHashMap<String, TestPojo> getTestHash() {
        return testHash;
    }
}


Class<?> noparams[] = {};
Method m = MainObj.getInstance().getSample().getClass().getDeclaredMethod("getTestHash", noparams);
ConcurrentHashMap<String, TestPojo> testMap = (ConcurrentHashMap<String, TestPojo>) m.invoke(MainObj.getInstance().getSample()); //Cannot use private class object here

//Tried the following as well:
ConcurrentHashMap<String, Sample> testMap = (cast) m.invoke(obj); //parent class of private inner class





Aucun commentaire:

Enregistrer un commentaire