Java Reflection (ii): detecting modifiers and types of classes

Source: Internet
Author: User
Tags modifiers

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.