Java Creating a new Class with Generics

I'm running into a problem with my program where given an object and an attribute name I want to return the method's return type. public static Class getAttributeType(Object object, String attributeName) { try { Method method = object.getClass().getMethod( "get" + StringUtils.capitalize(attributeName)); return method.getReturnType(); } catch (Exception e) { throw new RuntimeException("Unable to find the attribute:" + attributeName + " in class: " + object.getClass().getName()); } } This solution works great unless the return type of a method has a generic defined. For example if method prototype in question is List getBooks(), the code above would just return a List instead of a List. Is there any way for me to accomplish this? I can get the Generic type easily, I just don't know what to do with it once I have it. Thanks in advance
As a best-practice, I suggest you include the original exception 'e' as the source for the RuntimeException you throw. You will gain so much time having access to the original exception, the one that says what really went wrong. Good that you provide and additionnal message with the context though :-)

以上就是Java Creating a new Class with Generics的详细内容,更多请关注web前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » JavaScript 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准

联系我们

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏