Presentation is loading. Please wait.

Presentation is loading. Please wait.

Reflection Reflection is the ability of a program to examine and modify the structure and behavior of an object at runtime.

Similar presentations


Presentation on theme: "Reflection Reflection is the ability of a program to examine and modify the structure and behavior of an object at runtime."— Presentation transcript:

1 Reflection Reflection is the ability of a program to examine and modify the structure and behavior of an object at runtime.

2 Examine an object’s class at runtime 01 package myreflection; 02 import java.lang.reflect.Method; 03 04 public class ReflectionHelloWorld { 05 public static void main(String[] args){ 06 Foo f = new Foo(); 07 System.out.println(f.getClass().getName()); 08 } 09} 10 11class Foo { 12 public void print() { 13 System.out.println("abc"); 14 } 15} Output myreflection.Foo

3 Invoke method on unknown object 01package myreflection; 02import java.lang.reflect.Method; 03 public class ReflectionHelloWorld { 04 public static void main(String[] args){ 05 Foo f = new Foo(); 06 Method method; 07 try { 08 method = f.getClass().getMethod("print", new Class [0]); 09 method.invoke(f); 10 } catch (Exception e) { 11 e.printStackTrace(); 12 } } } 13 14class Foo { 15 public void print() { 16 System.out.println("abc"); 17 } } Output abc

4 Create object from Class instance 01 package myreflection; 02 public class ReflectionHelloWorld { 03 public static void main(String[] args){ 04 //create instance of "Class" 05 Class c = null; 06 try{ 07 c=Class.forName("myreflection.Foo"); 08 }catch(Exception e){ 09 e.printStackTrace(); 10 } 11 //create instance of "Foo" 12 Foo f = null; 13 try { 14 f = (Foo) c.newInstance(); 15 } catch (Exception e) { 16 e.printStackTrace(); 17 } 18 19 f.print(); 20 } 21 } 22 23 class Foo { 24 public void print() { 25 System.out.println("abc"); 26 } 27 }

5 Get constructor and create instance 01 package myreflection; 02 import java.lang.reflect.Constructor; 03 public class ReflectionHelloWorld { 04 public static void main(String[] args){ 05 //create instance of "Class" 06 Class c = null; 07 try{ 08 c=Class.forName("myreflection.Foo"); 09 }catch(Exception e){ 10 e.printStackTrace(); 11 } 12 //create instance of "Foo" 13 Foo f1 = null; 14 Foo f2 = null; 15 //get all constructors 16 Constructor cons[] = c.getConstructors(); 17 try { 18 f1 = (Foo) cons[0].newInstance(); 19 f2 = (Foo) cons[1].newInstance("abc"); 20 } catch (Exception e) { 21 e.printStackTrace(); 22 } 23 f1.print(); 24 f2.print(); 25 } 26 } 27 class Foo { 28 String s; 29 public Foo(){} 30 31 public Foo(String s){ 32 this.s=s; 33 } 34 35 public void print() { 36 System.out.println(s); 37 } 38 } Output null abc

6 Change array size though reflection 01 package myreflection; 02 import java.lang.reflect.Array; 03 04 public class ReflectionHelloWorld { 05 public static void main(String[] args) { 06 int[] intArray = { 1, 2, 3, 4, 5 }; 07 int[] newIntArray = (int[]) changeArraySize(intArray, 10); 08 print(newIntArray); 09 String[] atr = { "a", "b", "c", "d", "e" }; 10 String[] str1 = (String[]) changeArraySize(atr, 10); 11 print(str1); 12 } 13 14 // change array size 15 public static Object changeArraySize(Object obj, int len) { 16 Class arr = obj.getClass().getComponentType(); 17 Object newArray = Array.newInstance(arr, len); 18 19 //do array copy 20 int co = Array.getLength(obj); 21 System.arraycopy(obj, 0, newArray, 0, co); 22 return newArray; 23 } 24 // print 25 public static void print(Object obj) { 26 Class c = obj.getClass(); 27 if (!c.isArray()) { 28 return; 29 } 30 31 System.out.println("\nArray length: " + Array.getLength(obj)); 32 33 for (int i = 0; i < Array.getLength(obj); i++) { 34 System.out.print(Array.get(obj, i) + " "); 35 } 36 } 37 } Output Array length: 10 1 2 3 4 5 0 0 0 0 0 Array length: 10 a b c d e null null null null null


Download ppt "Reflection Reflection is the ability of a program to examine and modify the structure and behavior of an object at runtime."

Similar presentations


Ads by Google