How can I say using reflection if the class is final

Suppose I have a class:

public final class Foo

and the mirrored Class clz link that belongs to this class.

How can I say (using clz ) that Foo is final ?

+6
source share
3 answers

Using Class#getModifiers :

 Modifier.isFinal(clz.getModifiers()) 

Class modifiers (or fields or methods) are represented as packed int bits in the reflection API. Each possible modifier has its own bit mask, and the Modifier class helps mask these bits.

You can check the following modems:

  • abstract
  • final
  • interface
  • native
  • private
  • protected
  • public
  • static
  • strictfp
  • synchronized
  • transient
  • volatile
+16
source
 Modifier.isFinal(clz.getModifiers()) 
+5
source

You use Class.getModifiers() , ideally using Modifier to interpret the return value in a readable way:

 if (Modifier.isFinal(clz.getModifiers()) 
+2
source

All Articles