• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Check if class is an inner class

 
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey,

I'm trying to find out some of the metadata of a class.

The Modifier class offers pretty much most of what I need, however, I am trying to find out of a class is a non-static inner class and the Modifier API does not seem to provide that kind of information.

I could check if the class name contains '$' and then use the Modifier#isStatic() method but that seems cumbersome to me. Is there a smoother way to do that ?

Thanks
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I found the solution.

Class has a method #getEnclosingClass(). This method will return null if the class in question is a top level class.

So, getEnclosingClass() != null && Modifiers.isStatic(class.getModifiers()) does the trick.
reply
    Bookmark Topic Watch Topic
  • New Topic