How To Invoke Method Yesteryear Lift Inward Coffee Dynamically Using Reflection
In Java you lot tin invoke whatever method yesteryear its string cite dynamically using reflection API. java.lang.reflect API provides powerful reflection machinery which tin charge classes yesteryear its cite fifty-fifty if classes are non available at compile time, Can acquire all methods including private in addition to world from class in addition to allow you lot to invoke whatever method dynamically using reflection. For those who are novel inwards Java this audio pretty foreign that at runtime you lot render a method cite using string in addition to Java tin run that method without whatever code for calling the method during compilation, but Reflection is such a powerful machinery it allows to do lot of materials dynamically in addition to if you lot been using IDE similar Netbeans or Eclipse , J2EE framework similar Spring in addition to Struts, these all used reflection to render powerful configuration module in addition to several other useful characteristic similar code assist etc. Reflection is rattling comprehensive theme in addition to at that topographic point is a lot to acquire but nosotros volition kickoff amongst elementary Java plan illustration to invoke a method using reflection inwards by providing cite of method equally String value.
This Java article is continuation of my shipping service on roofing basic concepts similar static in addition to dynamic binding inwards Java, when to role Interface inwards Java in addition to why role PreparedStaement inwards Java. If you lot are novel hither or haven’t read them already hence you lot may uncovering them useful.
Java Program to invoke method yesteryear cite dynamically using Reflection
Exception. hither is consummate code illustration of calling method dynamically inwards Java using Reflection:
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;
import java.lang.reflect.Method;
import java.util.List;
/**
* Simple Java plan to invoke method yesteryear providing cite equally String.
* Reflective calls are slower than normal telephone telephone hence calling method using reflection
* should last role carefully.
*/
* Simple Java plan to invoke method yesteryear providing cite equally String.
* Reflective calls are slower than normal telephone telephone hence calling method using reflection
* should last role carefully.
*/
public class MethodInvocationReflection {
public static void main(String args[]) {
Class loadedList = null;
List listing = null;
try {
//loading shape dynamically using reflection
loadedList = Class.forName("java.util.ArrayList");
listing = (List) loadedList.newInstance();
//calling method using interface on reflective instance
list.add("abc");
list.add("bcd");
} catch (InstantiationException ex) {
System.err.println("Not able to do Instance of Class");
} catch (IllegalAccessException ex) {
System.err.println("Not able to access Class");
} catch (ClassNotFoundException ex) {
System.err.println("Not able to uncovering Class");
}
try {
//getting method event reflectively
Method thousand = loadedList.getMethod("size", (Class[]) null);
//calling method inwards coffee using reflection dynamically
Object size = m.invoke(list, (Object[]) null);
System.out.println("Result of dynamically invoking method inwards Java, Size: " + size);
} catch (NoSuchMethodException ex) {
System.err.println("Not able to uncovering Method on class");
ex.printStackTrace();
} catch (SecurityException ex) {
System.err.println("Security Exception raised");
ex.printStackTrace();
} catch (IllegalAccessException ex) {
System.err.println("Not able to access method ");
ex.printStackTrace();
} catch (IllegalArgumentException ex) {
System.err.println("Incorrect supplied arguments");
ex.printStackTrace();
} catch (InvocationTargetException ex) {
System.err.println("Not able to invoke method yesteryear String inwards Java");
ex.printStackTrace();
}
}
}
Important points spell calling Java method using reflection:
Here are few points worth noting spell invoking method yesteryear giving its cite in addition to using reflection, I handle that reflection provides flexibility but it also has closed to disadvantage :
1) Reflection inwards Java has serious functioning issues in addition to calling same method reflectively is to a greater extent than than 2 to iii times slower than normal method telephone telephone fifty-fifty inwards modern JVM, So role reflection entirely if its truly needed in addition to at that topographic point is no other agency around.
2) Invoking method using reflection also has closed to disadvantage similar compile fourth dimension checking of method parameters, order of parameters in addition to return type etc. Since nosotros role method.invoke() to telephone telephone methods we lose all compile fourth dimension checks in addition to whatever typo or mistake volition entirely last reflected inwards run fourth dimension wrapped nether InvocationTargetException.
3) Another work is too much code for invoking method or creating event using reflection equally you lot come across nosotros convey written xx business of code to invoke method which tin last converted into simply ii lines if you lot telephone telephone whatever method normally.
4) Last affair to banker's complaint is using Interface object for calling method instead of using reflection or invoking method yesteryear name. if you lot come across inwards a higher house Java plan to invoked method yesteryear name, we convey used List interface type to refer object created using reflection in addition to called method List.add() similar normal method only List.size() is called reflectively. This is i of best practices spell using reflection.
That's all on how to invoke method yesteryear cite inwards Java dynamically using Reflection. Reflection is powerful but role it amongst caution. Call method amongst it’s interface fifty-fifty if Class is loaded dynamically using reflection, that is improve than calling method yesteryear its string cite using method.invoke().
Further Learning
Complete Java Masterclass
6 JDBC functioning tips for Java application
0 Response to "How To Invoke Method Yesteryear Lift Inward Coffee Dynamically Using Reflection"
Post a Comment