{"id":3021,"date":"2020-04-30T20:39:53","date_gmt":"2020-04-30T15:09:53","guid":{"rendered":"https:\/\/www.h2kinfosys.com\/blog\/?p=3021"},"modified":"2020-05-01T16:25:51","modified_gmt":"2020-05-01T10:55:51","slug":"reflection-framework-in-java","status":"publish","type":"post","link":"https:\/\/www.h2kinfosys.com\/blog\/reflection-framework-in-java\/","title":{"rendered":"Reflection Framework in Java"},"content":{"rendered":"\n<p>Reflection concept in Java is used to inspect and change the <a href=\"https:\/\/www.h2kinfosys.com\/blog\/design-a-class-in-java\/\">behavior of classes<\/a>, methods, interfaces, and constructors during compile time.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>All the classes required for Reflection are available under package java.lang.reflect<\/li><li>This API is mainly used in IDE like Eclipse, Netbeans, etc., <a href=\"https:\/\/www.h2kinfosys.com\/blog\/introduction-automation-testing\/\">debugging tools<\/a> like JUnit, etc.<\/li><li>It gives the information about the class to which the object belongs and also the methods that are accessible by the object.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Advantages:<\/strong><\/h2>\n\n\n\n<ol class=\"wp-block-list\"><li>Reflection is used in Debugging and Testing tools to determine the private members of a class.<\/li><li>External and user-defined classes can also be used with the help of instance creations using their full names.<\/li><\/ol>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Drawbacks:<\/strong><\/h2>\n\n\n\n<ol class=\"wp-block-list\"><li><a href=\"https:\/\/www.oracle.com\/technical-resources\/articles\/java\/javareflection.html\" rel=\"nofollow noopener\" target=\"_blank\">Reflective code<\/a> changes the behavior with the updation in a platform and thereby breaking the abstraction.<\/li><li>It slows the performance and hence should be avoided when there is recursion in the code.<\/li><li>It requires runtime permissions, which may not be available if the system is working under a security manager, which may also cause the failure of the system.<\/li><li>With the help of Reflection, we can access the private members of the class, which can cause security issues.<\/li><li>Reflection code is less flexible and hard to maintain.<\/li><\/ol>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Reflection is used in&nbsp;<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Class:<\/strong> getClass() method is used to retrieve the name of the class to which the object belongs.<\/li><li><strong>Method:&nbsp; <\/strong>getMethod() returns all the public methods of the class to which the object belongs.&nbsp;<\/li><li><strong>Constructor:&nbsp; <\/strong>getConstructor() returns all the public constructors of the class to which the object belongs.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>How to create the object of a class<\/strong><\/h2>\n\n\n\n<p>There are three methods to <a href=\"https:\/\/www.h2kinfosys.com\/blog\/design-a-class-in-java\/\">create the objects of a class<\/a>:<\/p>\n\n\n\n<p><strong>1. Using forName() <\/strong>: It takes string as an argument.<\/p>\n\n\n\n<p>Eg. Class C = Class.forName(\u201cCat\u201d);<\/p>\n\n\n\n<p><strong>2. Using getClass()<\/strong> : It uses the object of a class to create object of its own class.<\/p>\n\n\n\n<p>Eg. Animal A = new Animal();<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Class C = A.getClass();<\/p>\n\n\n\n<p><strong>3. Using .class:<\/strong> Objects can also be instantiated using .class extension.<\/p>\n\n\n\n<p>Eg. Class C = Animal.class;<\/p>\n\n\n\n<p><strong>GetInterfaces() : <\/strong>It is used to collect the information about the constructors of a class. Return type is array.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">interface Animal {\n&nbsp;&nbsp;&nbsp;public void display();\n}\ninterface Mammal {\n&nbsp;&nbsp;&nbsp;public void makeSound();\n}\nclass Dog implements Animal, Mammal {\n&nbsp;&nbsp;&nbsp;public void display() {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(\"I am a dog.\");\n&nbsp;&nbsp;&nbsp;}\n&nbsp;&nbsp;&nbsp;public void makeSound() {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(\"Bark bark\");\n&nbsp;&nbsp;&nbsp;}\n}\nclass ReflectionDemo {\n&nbsp;&nbsp;public static void main(String[] args) {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;try {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/ create an object of Dog class\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Dog d1 = new Dog();\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Class obj = d1.getClass();\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>Class[] objInterface = obj.getInterfaces();<\/strong>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for(Class c : objInterface) {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/ print the name of interfaces\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(\"Interface Name: \" + c.getName());\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;catch(Exception e) {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e.printStackTrace();\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}\n&nbsp;&nbsp;&nbsp;}\n}\n<\/pre>\n\n\n\n<p><strong>GetSuperclass(): <\/strong>This method gives the information about all the constructors of a class.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">interface Animal {\n&nbsp;&nbsp;&nbsp;public void display();\n}\npublic class Dog implements Animal {\n&nbsp;&nbsp;&nbsp;public void display() {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(\"I am a dog.\");\n&nbsp;&nbsp;&nbsp;}\n}\nclass ReflectionDemo {\n&nbsp;&nbsp;&nbsp;public static void main(String[] args) {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;try {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Dog d1 = new Dog();\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Class obj = d1.getClass();\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int modifier = obj.getModifiers();\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(\"Modifier: \" + Modifier.toString(modifier));\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>Class superClass = obj.getSuperclass();<\/strong>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(\"Superclass: \" + superClass.getName());\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;catch(Exception e) {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e.printStackTrace();\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}\n&nbsp;&nbsp;&nbsp;}\n}\n\n<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Methods used in accessing fields:<\/strong><\/h2>\n\n\n\n<ol class=\"wp-block-list\"><li>getFields(): It returns all the public fields of the class along with its super class.<\/li><li>getModifiers():It returns the modifier of the class in the form of integer.<\/li><li>Get(ClassObject): It returns the value of the field.<\/li><li>setAccessible(Boolean): It makes the private fields of a class accessible.<\/li><\/ol>\n\n\n\n<pre class=\"wp-block-preformatted\">class Dog {\n&nbsp;&nbsp;public String type;\n}\nclass ReflectionDemo {\n&nbsp;&nbsp;public static void main(String[] args) {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;try{\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Dog d1 = new Dog();\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Class obj = d1.getClass();\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Field field1 = obj.getField(\"type\");\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;field1.set(d1, \"labrador\");\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String typeValue = (String)field1.get(d1);\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(\"type: \" + typeValue\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int mod1 = field1.getModifiers();\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String modifier1 = Modifier.toString(mod1);\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(\"Modifier: \" + modifier1);\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(\" \");\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;catch(Exception e) {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e.printStackTrace();\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}\n&nbsp;&nbsp;}\n}\n<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Methods used in accessing methods of a class:<\/strong><\/h2>\n\n\n\n<ol class=\"wp-block-list\"><li>getMethods():It returns all the public methods of the class along with its super class.<\/li><li>getModifiers():It returns the modifier of the method of a class in the form of integer.<\/li><li>getName():It will return the name of the method.<\/li><li>getReturnType(): It returns the type of a method.<\/li><\/ol>\n\n\n\n<pre class=\"wp-block-preformatted\">class Dog {\n&nbsp;&nbsp;&nbsp;public void display() {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(\"I am a dog.\");\n&nbsp;&nbsp;&nbsp;}\n&nbsp;&nbsp;&nbsp;protected void eat() {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(\"I eat dog food.\");\n&nbsp;&nbsp;&nbsp;}\n&nbsp;&nbsp;&nbsp;private void makeSound() {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(\"Bark Bark\");\n&nbsp;&nbsp;&nbsp;}\n}\nclass ReflectionDemo {\n&nbsp;&nbsp;&nbsp;public static void main(String[] args) {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;try {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Dog d1 = new Dog();\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Class obj = d1.getClass();\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Method[] methods = obj.getDeclaredMethods();\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for(Method m : methods) {&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(\"Method Name: \" + m.getName());&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int modifier = m.getModifiers();\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(\"Modifier: \" + Modifier.toString(modifier));&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(\"Return Types: \" + m.getReturnType());\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(\" \");\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;catch(Exception e) {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e.printStackTrace();\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}\n&nbsp;&nbsp;&nbsp;}\n}\n\n<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Methods used in accessing constructors of a class:<\/strong><\/h2>\n\n\n\n<ol class=\"wp-block-list\"><li>getConstructors():It returns all the public constructors of the class along with its super class.<\/li><li>getModifiers():It returns the modifier of the constructor of a class in the form of integer.<\/li><li>getName():It returns the name of the constructor.<\/li><li>getParameterCount(): It returns the number of parameters used in a constructor.<\/li><\/ol>\n\n\n\n<pre class=\"wp-block-preformatted\">class Dog {\n\n&nbsp;&nbsp;&nbsp;public Dog() {&nbsp;\n&nbsp;&nbsp;&nbsp;}\n&nbsp;&nbsp;&nbsp;public Dog(int age) {&nbsp;\n&nbsp;&nbsp;&nbsp;}\n&nbsp;&nbsp;&nbsp;private Dog(String sound, String type) {\n&nbsp;&nbsp;&nbsp;}\n}\nclass ReflectionDemo {\n&nbsp;&nbsp;&nbsp;public static void main(String[] args) {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;try {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Dog d1 = new Dog();\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Class obj = d1.getClass();\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Constructor[] constructors = obj.getDeclaredConstructors(); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(Constructor c : constructors) {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(\"Constructor Name: \" + c.getName());\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int modifier = c.getModifiers();\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(\"Modifier: \" + Modifier.toString(modifier));\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(\"Parameters: \" + c.getParameterCount());\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;catch(Exception e) {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e.printStackTrace();\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}\n&nbsp;&nbsp;&nbsp;&nbsp;}\n<\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Reflection concept in Java is used to inspect and change the behavior of classes, methods, interfaces, and constructors during compile time. All the classes required for Reflection are available under package java.lang.reflect This API is mainly used in IDE like Eclipse, Netbeans, etc., debugging tools like JUnit, etc. It gives the information about the class [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3027,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[42],"tags":[449,58,650],"class_list":["post-3021","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java-tutorials","tag-advantages","tag-java","tag-reflections-framework"],"_links":{"self":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/3021","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/comments?post=3021"}],"version-history":[{"count":0,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/3021\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media\/3027"}],"wp:attachment":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media?parent=3021"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/categories?post=3021"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/tags?post=3021"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}