A class can be declared by a number of modifiers that can affect its run-time behavior:
- Access modifier: public,protected,private
- Modifiers that require overloading: abstract
- Limit to only one instance of: static
- Block Value modification: final
- Enforcing strict floating-point behavior: STRICTFP
- Annotations
not all modifiers can be used on all classes. For example, final cannot modify interfaces, enumerations cannot be abstract. Java.lang.reflect.Modifier contains declarations for all possible modifiers, and it also contains methods for encoding the set of modifiers returned by Class.getmodifiers (). The following class shows how to get the modifiers for a class. Because the class itself implements Java.lang.reflect.AnnotatedElement, it can also query runtime annotations.
Import Java.lang.annotation.annotation;import Java.lang.reflect.modifier;import Java.lang.reflect.type;import Java.lang.reflect.typevariable;import Java.util.arrays;import Java.util.arraylist;import Java.util.List;import Static Java.lang.system.out;public class Classdeclarationspy {public static void main (String ... args) {try {class& lt;? > c = class.forname (Args[0]); Out.format ("class:%n%s%n%n", C.getcanonicalname ()); Out.format ("modifiers:%n%s%n%n", Modifier.tostring (C.getmodifiers ())); Out.format ("Type parameters:%n"); typevariable[] TV = C.gettypeparameters (); if (tv.length! = 0) {Out.format (""); for (typevariable T:tv) Out.format ("%s", T.getname ()); Out.format ("%n%n"); } else {Out.format ("--No Type Parameters--%n%n"); } out.format ("implemented interfaces:%n"); type[] Intfs = c.getgenericinterfaces (); if (intfs.length! = 0) {for (Type intf:intfs) Out.format ("%s%n", intf.tostring ()); Out.format ("%n"); } else {Out.foRmat ("--No implemented Interfaces--%n%n"); } out.format ("Inheritance path:%n"); list<class> L = new arraylist<class> (); Printancestor (c, L); if (l.size ()! = 0) {for (class<?> cl:l) Out.format ("%s%n", Cl.getcanonicalname ()); Out.format ("%n"); } else {Out.format ("--No Super Classes--%n%n"); } out.format ("annotations:%n"); annotation[] ann = C.getannotations (); if (ann.length! = 0) {for (Annotation A:ann) Out.format ("%s%n", a.tostring ()); Out.format ("%n"); } else {Out.format ("--No Annotations--%n%n"); }//Production code should handle this exception more gracefully} catch (ClassNotFoundException x) {X.printsta Cktrace ();} } private static void Printancestor (Class<?> C, list<class> l) {class<?> ancestor = C.getsuperclass () ; if (ancestor! = null) {L.add (ancestor); Printancestor (ancestor, L); } }}
A running result:
$ java classdeclarationspy java.util.concurrent.ConcurrentNavigableMapClass: Java.util.concurrent.ConcurrentNavigableMapModifiers: Public abstract InterfaceType Parameters: K vimplemented Interfaces: java.util.concurrent.concurrentmap<k, v> java.util.navigablemap<k, V >inheritance Path:-- no Super Classes--annotations: --No Annotations--
Again like,
$ java classdeclarationspy java.io.InterruptedIOExceptionClass: java.io.InterruptedIOExceptionModifiers: publictype Parameters: --no Type Parameters--implemented Interfaces: --No implemented Interfaces-- Inheritance Path: java.io.IOException java.lang.Exception java.lang.Throwable Java.lang.ObjectAnnotations: --No Annotations--
Java Reflection (ii): detecting modifiers and types of classes