How To Invoke Method Past Times Lift Inwards Coffee Dynamically Using Reflection
Tuesday, October 30, 2018
Add Comment
In Java you lot tin invoke whatever method past times its string mention dynamically using reflection API. java.lang.reflect API provides powerful reflection machinery which tin charge classes past times its mention fifty-fifty if classes are non available at compile time, Can acquire all methods including private in addition to populace from class in addition to allow you lot to invoke whatever method dynamically using reflection. For those who are novel inward Java this audio pretty foreign that at runtime you lot render a method mention 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 really comprehensive topic in addition to in that place is a lot to larn but nosotros volition get-go amongst uncomplicated Java programme illustration to invoke a method using reflection inward by providing mention of method equally String value.
This Java article is continuation of my post service on roofing basic concepts similar static in addition to dynamic binding inward Java, when to purpose Interface inward Java in addition to why purpose PreparedStaement inward Java. If you lot are novel hither or haven’t read them already thence you lot may detect them useful.
Java Program to invoke method past times mention dynamically using Reflection
Exception. hither is consummate code illustration of calling method dynamically inward 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 programme to invoke method past times providing mention equally String.
* Reflective calls are slower than normal telephone phone thence calling method using reflection
* should last purpose carefully.
*/
* Simple Java programme to invoke method past times providing mention equally String.
* Reflective calls are slower than normal telephone phone thence calling method using reflection
* should last purpose carefully.
*/
public class MethodInvocationReflection {
public static void main(String args[]) {
Class loadedList = null;
List listing = null;
try {
//loading degree 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 detect Class");
}
try {
//getting method illustration reflectively
Method thousand = loadedList.getMethod("size", (Class[]) null);
//calling method inward coffee using reflection dynamically
Object size = m.invoke(list, (Object[]) null);
System.out.println("Result of dynamically invoking method inward Java, Size: " + size);
} catch (NoSuchMethodException ex) {
System.err.println("Not able to detect 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 past times String inward Java");
ex.printStackTrace();
}
}
}
Important points piece calling Java method using reflection:
Here are few points worth noting piece invoking method past times giving its mention in addition to using reflection, I handgrip that reflection provides flexibility but it also has or thence disadvantage :
1) Reflection inward 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 phone fifty-fifty inward modern JVM, So purpose reflection solely if its truly needed in addition to in that place is no other agency around.
2) Invoking method using reflection also has or thence disadvantage similar compile fourth dimension checking of method parameters, order of parameters in addition to return type etc. Since nosotros purpose method.invoke() to telephone phone methods we lose all compile fourth dimension checks in addition to whatever typo or mistake volition solely last reflected inward run fourth dimension wrapped nether InvocationTargetException.
3) Another work is too much code for invoking method or creating illustration using reflection equally you lot meet nosotros accept written twenty business of code to invoke method which tin last converted into but 2 lines if you lot telephone phone whatever method normally.
4) Last matter to Federal Reserve annotation is using Interface object for calling method instead of using reflection or invoking method past times name. if you lot meet inward a higher house Java programme to invoked method past times name, we accept 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 piece using reflection.
That's all on how to invoke method past times mention inward Java dynamically using Reflection. Reflection is powerful but purpose it amongst caution. Call method amongst it’s interface fifty-fifty if Class is loaded dynamically using reflection, that is amend than calling method past times its string mention using method.invoke().
Further Learning
Complete Java Masterclass
6 JDBC functioning tips for Java application
0 Response to "How To Invoke Method Past Times Lift Inwards Coffee Dynamically Using Reflection"
Post a Comment