zoukankan      html  css  js  c++  java
  • Java Reflection API | Java.lang.Class

    ref: http://www.studytonight.com/java/reflection-api

    Reflection API

    reflection means ability of a software to analyze itself. In java, Reflection API provides facility to analyze and change runtime behavior of a class, at runtime

    For Example, using reflection at the runtime you can determine what method or modifiers a class supports

    What is reflect package ?

    java.lang.reflect package encapsulates serveral important interface and classes. These classes and interface define methods which are used for reflection

     class            description

    ----------------------------------------------------------------------------------------------------------

    Array            allow you to dynamically created and manipulate arrays

    Constructor         gives info abt constructor of a class

    Field              provides info abt field

    Method            procide info abt method

    Modifier           provide info abt class and member access modifier

    Proxy             supports dynamic proxy classes

    java.lang.Class is another important class used in Reflection API

    Uses of Reflection

    1. Developing IDE
    2. Debugging and Test tools

    3. Loading drivers and providing dynamic info

    Disadvantages of Reflection

    1. Low performance

    2. Security risk

    3. Violation of OOPs concept

    ************************* cut**************************

    Reflection Class

    java.lang.Class 

    Class is a final class injava.lang package which extends Object class. Instance of this class 

    represents classes and interfaces in a running java application. It is used to analyze and change dynamic behavior of a class at runtime

     

    Some Important Methods of java.lang.Class class

    This class defines several methods using which we can get info about methods, constructors, modifiers and members of a class at runtime

    forName()    

    1. this method takes fully qualified name of classes or nterfaces as its argument and return instance 

       of the class associated with it. 

    2. static Class<?> forName(String className)

    class Student{}
    class Test
    {
     public static void main(String args[])
     {
      Class c = Class.forName("Student");
      System.out.println(c.getName());
     }
    }
    output: Student


    getConstructors() and getDeclaredConstructors()

    1. getConstructors() returns array of Constructors object that represent all the public constructors of the invoking object. This method only returns public constructor. getDeclaredConstructors() will return all constructors

    Constructor< ?>[] getConstructors();
    Constructor< ?>[] getDeclaredConstructors();

    Example using getConstructors() and getDeclaredConstructors() method

    Class c = Class.forName("Student");
      Constructor< Student>[] ct = c.getConstructors();
      for(int i=0; i< ct.length; i++)
        { System.out.println(ct[i]); }
      Constructor< Student>[] cdt = c.getDeclaredConstructors();
      for(int i=0;i< cdt.length;i++)
        { System.out.println(cdt[i]);}
    
    output:
      public Student()
      public Student()
      Student(java.lang.String)
    
    

    getMethods() and getDeclaredMethods()

    getFields() and getDeclaredFields()

    Example using getFields() and getDeclaredFields() method




  • 相关阅读:
    MATLAB 模板匹配
    ACDSee15 教你如何轻松在图片上画圈圈、画箭头、写注释
    Qt 显示一个窗体,show()函数和exec()函数有什么区别?
    Qt 将窗体变为顶层窗体(activateWindow(); 和 raise() )
    Qt QSS样式化 菜单Qmenu&QAction
    Qt 获取文件夹中的文件夹名字
    Qt 删除文件夹或者文件
    欧洲终于承认“工业4.0”失败,互联网经济严重落后中美
    深入浅出数据结构
    浅谈城市大脑与智慧城市发展趋势
  • 原文地址:https://www.cnblogs.com/morningdew/p/5619385.html
Copyright © 2011-2022 走看看