Dynamic Class Loading using Java Reflection API
- By Viral Patel on July 3, 2009
One of the reason why Java language has been so useful and used widely is the set of APIs that comes with the language (and 3rd party APIs like iText etc). Using these APIs one do a whole lot unimaginable stuff.
Java Reflection API are one of such APIs that extend the horizon of a Java programmer and enables him to code some really great stuffs.
Reflection is commonly used by programs which require the ability to examine or modify the runtime behavior of applications running in the Java virtual machine. This is a relatively advanced feature and should be used only by developers who have a strong grasp of the fundamentals of the language. With that caveat in mind, reflection is a powerful technique and can enable applications to perform operations which would otherwise be impossible.
Dynamic Java Class loading is an important feature of the Java Virtual Machine because it provides the Java platform with the ability to install software components at run-time. It has a number of unique characteristics. First of all, lazy loading means that classes are loaded on demand and at the last moment possible. Second, dynamic class loading maintains the type safety of the Java Virtual Machine by adding link-time checks, which replace certain run-time checks and are performed only once. Moreover, programmers can define their own class loaders that, for example, specify the remote location from which certain classes are loaded, or assign appropriate security attributes to them. Finally, class loaders can be used to provide separate name spaces for various software components. For example, a browser can load applets from different web pages using separate class loaders, thus maintaining a degree of isolation between those applet classes. In fact, these applets can contain classes of the same name — these classes are treated as distinct types by the Java Virtual Machine.
Let us see an example of Dynamic class loading using Java Reflection API. Following is our DemoClass that needs to be loaded dynamically and method demoMethod() needs to be called.
1
2
3
4
5
6
7
| class DemoClass { public String demoMethod(String demoParam) { System.out.println( "Parameter passed: " + demoParam); return DemoClass. class .getName(); } } |
So to load above class file dynamically following code can be used.
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
| public class DynamicClassLoadingExample { public static void main(String[] args) { try { ClassLoader myClassLoader = ClassLoader.getSystemClassLoader(); // Step 2: Define a class to be loaded. String classNameToBeLoaded = "net.viralpatel.itext.pdf.DemoClass" ; // Step 3: Load the class Class myClass = myClassLoader.loadClass(classNameToBeLoaded); // Step 4: create a new instance of that class Object whatInstance = myClass.newInstance(); String methodParameter = "a quick brown fox" ; // Step 5: get the method, with proper parameter signature. // The second parameter is the parameter type. // There can be multiple parameters for the method we are trying to call, // hence the use of array. Method myMethod = myClass.getMethod( "demoMethod" , new Class[] { String. class }); // Step 6: // Calling the real method. Passing methodParameter as // parameter. You can pass multiple parameters based on // the signature of the method you are calling. Hence // there is an array. String returnValue = (String) myMethod.invoke(whatInstance, new Object[] { methodParameter }); System.out.println( "The value returned from the method is:" + returnValue); } catch (SecurityException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } } } |
The above code is pretty much self explanatory. We have used
ClassLoader.getSystemClassLoader()
method to get instance of class java.lang.ClassLoader. We have loaded our demo class DemoClassusing method loadClass() of ClassLoader and invoked the desired method.
No comments:
Post a Comment