CSC302 2006S, Class 19: Introspection and Reflection in Java Admin: * Thoughts on "Growing a Language"? * Presents * New homework: Figure out anonymous inner classes. * Questions on semantics homework? Overview: * What are reflection and introspection? * Why are reflection and introspection? * Applications: * Serializing * Profiling Questions: * Explain input and output * Input: Extract the car of the input sequence down-arrow gets you the first thing some symbol for "all but first" * Output: Add to the output sequence section symbol appends sequences * Explain conditionals If the expression has value 0, use the false part If the expression has non0 value, use the true part * Note on syntax of semantics of Scheme n! = if (n = 0) 1 else n*n-1! vs. 0! = 1 n! = n*(n-1)! if n > 0 What is Reflection? * A process by which the programmer can obtain information about objects, classes, (methods, fields, construtors) at runtime. * And use or change some of them. * Change values of fields. * Can do similar things in non-object-oriented languages (e.g., similar ideas in Scheme and LISP) * Tate considers reflection an important modern feature Why is reflection useful? * One way to patch running code * Most Web servers add new services using reflection * Reflection is nice for factory methods * Detour on factory methods - Suppose * we have two implementations of the same interface * we've written polymorphic code that uses the interface * problem: polymoprhic construction * How does client say "Generate objects using ..."; * Example public Point[] simulate(Point initial, double velocity, ...) { Point[] stuff = ...; ... point[i++] = new Point(...); // Not allowed * What can we do? * Use reflection initial.getClass.getConsturctor(null).runConstructor(...); * Pass in an object that provides a construct method - A factory Problem: I have a large data structure and I want to dump it to a fileand reload it. How? * Example hashMap * For each key, dump the key and the corresponding value separated by commas * For each kind of key and value, write a dump method * Problems: * What if some keys or values are more complex (e.g., what if the values are hash tables) * An awful lot of repetitive coding * Need to deal with shared structures * What if you don't have access to the source code? Note: Most Java programs use the Serializable interface Alternate solution (that works even for non-serializable stuff): Use Reflection write a generic dumpObject method * Grab the class of the object * Grab the field list from the class * Generate and save identifier (for repetition) * Recursively dump the fields for Object fields * Write identifier * Write class * Write fieldName=identifier for field Another problem: Profiling * Count and provide access to how many times each method is called * Suppose you only have to do it for one class * Create a variable for each method * Increment it each time its called * Use a separate class to print those out (or do analysis) *Issues * requires us to modify the class (can be a pain for a lot of classes) * Metaprogramming one: Write a program that does the modification * Reflection: Use a generic Proxy object with an invocation handler that does the profiling public Object invoke(...) increment the counter call the appropriate method (with method.run(...)) return the value