1

I can't get to work java reflection in Spring boot with Controller and JdbcTemplate.

Default controller looks like:

public class DefaultController {

private static final Logger logger = LoggerFactory.getLogger(DefaultController.class);

public JsonResponseDataModel selectOneAuto(Long id, Class<?> repository, HttpServletResponse response){

    final JsonResponseDataModel result = new JsonResponseDataModel();
    System.out.println("The name of class is " + repository.getName());

    Method[] methods = repository.getMethods();
    for (Method method : methods) {
        System.out.println("Method: " + method.getName());
    }

    try {
        //Method method = repository.getClass().getMethod("selectOne", Long.class);
        Method method = repository.getClass().getDeclaredMethod("selectOne", Long.class);
        method.invoke(repository, id);

        logger.info("selectOneAuto : id={} ", id);
    } catch (EmptyResultDataAccessException e) {
        result.setEmptyResultDataAccessException("id", id.toString());
    } catch (DataAccessException e) {            
        e.printStackTrace();            
    } catch (NoSuchMethodException e) {            
        e.printStackTrace();            
    } catch (IllegalAccessException e) {            
        e.printStackTrace();            
    } catch (InvocationTargetException e) {            
        e.printStackTrace();            
    }
    return result;
}

}

Inside CompanyRepository class is defined selectOne method with Long input:

@Transactional(readOnly=true)
     public CompanyModel selectOne(Long id) {
     CompanyModel result = null;
     final String sql = "SELECT * FROM company WHERE id=?";
     return jdbcTemplate.queryForObject(sql, new Object[]{id}, new CompanyRowMapper());
}

When I create a new class "CompanyController extends DefaultController" and call method selectOneAuto:

selectOneAuto(id, new CompanyRepository().getClass(), response);

Then it ends with error on line "Method method = repository.getClass().getDeclaredMethod("selectOne", Long.class);"

"Java.lang.NoSuchMethodException: java.lang.Class.selectOne(java.lang.Long)"

But the for loop inside "selectOneAuto" method outputs method named "selectOne". What is wrong here?

0

1 Answer 1

1

Your code attempts to call the method on an instance of Class.

method.invoke(repository, id);

repository object is an instance of Class since you are passing new CompanyRepository().getClass() as parameter.

The second point to be noted is repository is an instance of Class already so there is no need to call getClass() on this object.

You should obtain the method object using the following code :

Method method = repository.getDeclaredMethod("selectOne", Long.class);

And then this should work :

CompanyRepository repsitoryObj = new CompanyRepository();
method.invoke(repsitoryObj, id);

Or a better and cleaner way is to simply change the type of your repository parameter as CompanyRepository your method will look as follows:

public JsonResponseDataModel selectOneAuto(Long id, CompanyRepository repository, HttpServletResponse response){

    final JsonResponseDataModel result = new JsonResponseDataModel();
    System.out.println("The name of class is " + repository.getClass().getName());

    Method[] methods = repository.getClass().getMethods();
    for (Method method : methods) {
    System.out.println("Method: " + method.getName());
    }

    try {
    //Method method = repository.getClass().getMethod("selectOne", Long.class);
    Method method = repository.getClass().getDeclaredMethod("selectOne", Long.class);
    method.invoke(repository, id);

    logger.info("selectOneAuto : id={} ", id);
    } catch (EmptyResultDataAccessException e) {
    result.setEmptyResultDataAccessException("id", id.toString());
    } catch (DataAccessException e) {            
    e.printStackTrace();            
    } catch (NoSuchMethodException e) {            
    e.printStackTrace();            
    } catch (IllegalAccessException e) {            
    e.printStackTrace();            
    } catch (InvocationTargetException e) {            
    e.printStackTrace();            
    }
    return result;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.