mardi 27 août 2019

Java reflection getDeclaredMethod throws NoSuchMethodException

I have a private method getListSonarMetricsFromRegistry declared in the Class SonarRestApiServiceImpl that I want to call, with Java reflection, but I get the exception:

java.lang.NoSuchMethodException: com.cma.kpibatch.rest.impl.SonarRestApiServiceImpl.getListSonarMetricsFromRegistry(java.util.HashMap)
at java.lang.Class.getDeclaredMethod(Class.java:2130)
at com.cma.kpibatch.service.rest.SonarRestApiServiceImplTest.testGetListSonarMetricsFromRegistry(SonarRestApiServiceImplTest.java:81)


I tried to use Java reflection, like below :

    @Test
    public void initTest() throws NoSuchMethodException, SecurityException, IllegalAccessException,
            IllegalArgumentException, InvocationTargetException {
        Map<Long, KpiMetric> tmp = new HashMap<>();
        Method method = sonarRestApi.getClass().getDeclaredMethod("getListSonarMetricsFromRegistry", tmp.getClass());
        method.setAccessible(true);
        List<String> list = (List<String>) method.invoke(sonarRestApi, registry.getKpiMetricMap());
    }


Here is the getListSonarMetricsFromRegistry method declaration:

//This method works correctly, it returns a List of String without error
private List<String> getListSonarMetricsFromRegistry(Map<Long, KpiMetric> map) {
    return //something
}

The similar questions provided by Stackoverflow did help, but I still have the same Exception.





Aucun commentaire:

Enregistrer un commentaire