








Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!









!
!
Reflection and Metaprogramming
Definitions of Reflection
Language Support for Reflection
Java’s reflection capabilities are offered as a library API, though of course supported by the language java.lang.Class java.lang.reflect Capabilities:
Obtaining a class object The entry point to reflection operations: java.lang.Class Various means to obtain an object of Class type
Java’s class modifiers
Example 2. Dynamic Method Invocation // Let this be a method of object x public void work(int i, String s) { System.out.printf("Called: i=%d, s=%s%n", i, s); } Class clX = x.getClass(); // To find a method, need array of matching Class types. Class[] argTypes = { int.class, String.class }; // Find a Method object for the given method. Method worker = clX.getMethod("work", argTypes); // To invoke the method, need the invocation arguments, as an Object array. Object[] theData = { 42, "Chocolate Chips" }; // The last step: invoke the method. worker.invoke(x, theData);
Notes about Reflection