For the reflection examples, lets consider this Dog class
package ref; /** * * @author Rukshan */ public class Dog { private String name; public String type; public Dog() { } private Dog(String type) { this.type=type; } public void setName(String name) { this.name=name; } public void setType(String type){ this.type=type; } public String getName(){ return this.name; } public String getType(){ return this.type; } private String Bark(String sound){ return "Barked "+sound; } }
Get the value of a field of object
public static void getField() { try { Dog dog = new Dog(); dog.setType("dog"); Class<?> dogClass = dog.getClass(); Field type = dogClass.getDeclaredField("type"); String value = (String) type.get(dog); System.out.println(value); } catch (NoSuchFieldException ex) { Logger.getLogger(Ref.class.getName()).log(Level.SEVERE, null, ex); } catch (SecurityException ex) { Logger.getLogger(Ref.class.getName()).log(Level.SEVERE, null, ex); } catch (IllegalArgumentException ex) { Logger.getLogger(Ref.class.getName()).log(Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { Logger.getLogger(Ref.class.getName()).log(Level.SEVERE, null, ex); } }
Get the value of protected field of object
public static void getProtectedField() { try { Dog dog = new Dog(); dog.setName("lucky"); Class<?> dogClass = dog.getClass(); Field name = dogClass.getDeclaredField("name"); name.setAccessible(true); String value = (String) name.get(dog); System.out.println(value); } catch (NoSuchFieldException ex) { Logger.getLogger(Ref.class.getName()).log(Level.SEVERE, null, ex); } catch (SecurityException ex) { Logger.getLogger(Ref.class.getName()).log(Level.SEVERE, null, ex); } catch (IllegalArgumentException ex) { Logger.getLogger(Ref.class.getName()).log(Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { Logger.getLogger(Ref.class.getName()).log(Level.SEVERE, null, ex); } }
Set the value of field in Object
public static void setField() { try { Dog dog = new Dog(); Class<?> dogClass = dog.getClass(); Field type = dogClass.getDeclaredField("type"); String value = "Dog"; type.set(dog, value); System.out.println(dog.getType()); } catch (NoSuchFieldException ex) { Logger.getLogger(Ref.class.getName()).log(Level.SEVERE, null, ex); } catch (SecurityException ex) { Logger.getLogger(Ref.class.getName()).log(Level.SEVERE, null, ex); } catch (IllegalArgumentException ex) { Logger.getLogger(Ref.class.getName()).log(Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { Logger.getLogger(Ref.class.getName()).log(Level.SEVERE, null, ex); } }
Set the value of protected field in object
public static void setProtectedField() { try { Dog dog = new Dog(); Class<?> dogClass = dog.getClass(); Field name = dogClass.getDeclaredField("name"); String value = "Lucky"; name.setAccessible(true); name.set(dog, value); System.out.println(dog.getName()); } catch (NoSuchFieldException ex) { Logger.getLogger(Ref.class.getName()).log(Level.SEVERE, null, ex); } catch (SecurityException ex) { Logger.getLogger(Ref.class.getName()).log(Level.SEVERE, null, ex); } catch (IllegalArgumentException ex) { Logger.getLogger(Ref.class.getName()).log(Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { Logger.getLogger(Ref.class.getName()).log(Level.SEVERE, null, ex); } }
Invoke method
public static void invokeMethod() { try { Dog dog = new Dog(); Class<?> dogClass = dog.getClass(); Method setName = dogClass.getDeclaredMethod("setName", new Class[]{String.class}); setName.invoke(dog, new Object[]{"Lucky"}); System.out.println(dog.getName()); } catch (SecurityException ex) { Logger.getLogger(Ref.class.getName()).log(Level.SEVERE, null, ex); } catch (IllegalArgumentException ex) { Logger.getLogger(Ref.class.getName()).log(Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { Logger.getLogger(Ref.class.getName()).log(Level.SEVERE, null, ex); } catch (NoSuchMethodException ex) { Logger.getLogger(Ref.class.getName()).log(Level.SEVERE, null, ex); } catch (InvocationTargetException ex) { Logger.getLogger(Ref.class.getName()).log(Level.SEVERE, null, ex); } }
Invoke proteced methods and get return value
public static void invokeProtectedMethod() { try { Dog dog = new Dog(); Class<?> dogClass = dog.getClass(); Method bark = dogClass.getDeclaredMethod("Bark", new Class[]{String.class}); bark.setAccessible(true); Object ob = bark.invoke(dog, new Object[]{"awwww"}); String value = (String) ob; System.out.println(value); } catch (SecurityException ex) { Logger.getLogger(Ref.class.getName()).log(Level.SEVERE, null, ex); } catch (IllegalArgumentException ex) { Logger.getLogger(Ref.class.getName()).log(Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { Logger.getLogger(Ref.class.getName()).log(Level.SEVERE, null, ex); } catch (NoSuchMethodException ex) { Logger.getLogger(Ref.class.getName()).log(Level.SEVERE, null, ex); } catch (InvocationTargetException ex) { Logger.getLogger(Ref.class.getName()).log(Level.SEVERE, null, ex); } }
Create Instance with constructor
public static void constructor(){ try { Class<?> dogClass=Class.forName("ref.Dog"); Constructor<?> construct=dogClass.getConstructor(); Object dog=construct.newInstance(); Method bark = dogClass.getDeclaredMethod("Bark", new Class[]{String.class}); bark.setAccessible(true); Object ob = bark.invoke(dog, new Object[]{"huuuuu"}); String value = (String) ob; System.out.println(value); } catch (ClassNotFoundException ex) { Logger.getLogger(Ref.class.getName()).log(Level.SEVERE, null, ex); } catch (NoSuchMethodException ex) { Logger.getLogger(Ref.class.getName()).log(Level.SEVERE, null, ex); } catch (SecurityException ex) { Logger.getLogger(Ref.class.getName()).log(Level.SEVERE, null, ex); } catch (InstantiationException ex) { Logger.getLogger(Ref.class.getName()).log(Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { Logger.getLogger(Ref.class.getName()).log(Level.SEVERE, null, ex); } catch (IllegalArgumentException ex) { Logger.getLogger(Ref.class.getName()).log(Level.SEVERE, null, ex); } catch (InvocationTargetException ex) { Logger.getLogger(Ref.class.getName()).log(Level.SEVERE, null, ex); } }
Create Instance with proteced constructor
public static void protectedConstructor(){ try { Class<?> dogClass=Class.forName("ref.Dog"); Constructor<?> construct=dogClass.getConstructor(); construct.setAccessible(true); Object dog=construct.newInstance(); Method bark = dogClass.getDeclaredMethod("Bark", new Class[]{String.class}); bark.setAccessible(true); Object ob = bark.invoke(dog, new Object[]{"awwww huuuuu"}); String value = (String) ob; System.out.println(value); } catch (ClassNotFoundException ex) { Logger.getLogger(Ref.class.getName()).log(Level.SEVERE, null, ex); } catch (NoSuchMethodException ex) { Logger.getLogger(Ref.class.getName()).log(Level.SEVERE, null, ex); } catch (SecurityException ex) { Logger.getLogger(Ref.class.getName()).log(Level.SEVERE, null, ex); } catch (InstantiationException ex) { Logger.getLogger(Ref.class.getName()).log(Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { Logger.getLogger(Ref.class.getName()).log(Level.SEVERE, null, ex); } catch (IllegalArgumentException ex) { Logger.getLogger(Ref.class.getName()).log(Level.SEVERE, null, ex); } catch (InvocationTargetException ex) { Logger.getLogger(Ref.class.getName()).log(Level.SEVERE, null, ex); } }
Get the declared filed of a class
public static void getFields(){ try { Class<?> dogClass=Class.forName("ref.Dog"); Field[] fields=dogClass.getDeclaredFields(); for (Field field : fields) { System.out.println(field.getName()+" "+field.getType().getCanonicalName()); } } catch (ClassNotFoundException ex) { Logger.getLogger(Ref.class.getName()).log(Level.SEVERE, null, ex); } }
Get Declared methods and method parameters of a class
public static void getMethods(){ try { Class<?> dogClass=Class.forName("ref.Dog"); Method[] methods=dogClass.getDeclaredMethods(); for (Method method : methods) { System.out.println(method.getName()+" "+method.getReturnType().getCanonicalName()); for (Class<?> class1 : method.getParameterTypes()) { System.out.println(class1.getCanonicalName()); } } } catch (ClassNotFoundException ex) { Logger.getLogger(Ref.class.getName()).log(Level.SEVERE, null, ex); } }
Download the Source code from here
Add Comment
Comments (0)