Check type of primitive field

I'm trying to determine the type of a field on an object. I don't know the type of the object when it is passed to me but I need to find fields which are `long`s. It is easy enough to distinguish the boxed `Long`s but the primitive `long` seems more difficult. I *can* make sure that the objects passed to me only have `Longs`, not the primitives, but I'd rather not. So what I have is: for (Field f : o.getClass().getDeclaredFields()) { Class clazz = f.getType(); if (clazz.equals(Long.class)) { // found one -- I don't get here for primitive longs } } A hacky way, which seems to work, is this: for (Field f : o.getClass().getDeclaredFields()) { Class clazz = f.getType(); if (clazz.equals(Long.class) || clazz.getName().equals("long")) { // found one } } I'd really like a cleaner way to do this if there is one. If there is no better way then I think that requiring the objects I receive to only use `Long` (not `long`) would be a better API. Any ideas?

以上就是Check type of primitive field的详细内容,更多请关注web前端其它相关文章!

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

评论 抢沙发

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

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

联系我们

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

支付宝扫一扫打赏

微信扫一扫打赏