20-CS-694 Advanced Programming Techniques Spring 2012
Reflection

Interfaces, Exceptions, Graphics, Animation, Threads, Reflection, Networking, RMI, JDBC, JNI

All lectures
Examples

This series shows how to use many of the classes associated with reflection. Click on the class names in the following table to see class details.

Class Method Constructor Field Modifier Array ClassLoader

1.   ReflectionTest.java
Hello.java
Slide.java
Reflect.java
RunHello.java

  -   Class encryption: class files with sensitive data may be encrypted. When these files are loaded for execution a specialized class loader decrypts them on-the-fly so they can be run. One applet (Slide.java) encrypts a file (Hello.class) that is downloaded from gauss using a selected key (0 means no encryption). The encrypted file is saved in your home directory for inspection. Another applet (ReflectionTest.java) reads this file, decrypts it as it is loaded according to a selected key, and runs it. If the keys do not match, an error message is printed. Compile Hello.java and RunHello.java and then run java RunHello to see what this class file is supposed to do.
 
2.   Reflect.java
Client.java
Hello.java
Invictus.java
Tay_Bridge.java
Receiver.java

  -   A server is in a directory with no class information other than that of its own classes. The client is in a directory with its own classes plus has access to three additional ones named Hello, Invictus, and Tay_Bridge. Each of these classes has a single method with no arguments and a String return value. The server starts and waits for connections to be initiated by the client. The client chooses one of the classes and sends it to the server. When the "Run the method" button is clicked the server invokes the method of the sent class and displays the output string in a JTextArea.
 
3.   Reflect.java
Transmitter.java
Weather.java
Sudoku.java
sudoku.3.inp
Receiver.java

  -   Choose the Weather or Sudoku classes to send to the Receiver. The Receiver contains no information about these classes other than they are applets. Hit the "Start the Applet" button on the receiver to launch the received applet. The classes that are sent are actually obtained remotely, by default, from gauss.ececs.uc.edu. The transmitter can read classes from files as well by choosing "Local" in the JComboBox on the right.
 
4.   CallBackTest.java

  -   Run a callback on any method of any class. In this example just a few are illustrated.
 
5.   M3Player.java
Class hierarchy
Player class

  -   Play MP3 files from a remote Player library Player is a class that supports playing MP3 files It is part of library jl1.0.1.jar from javazoom that resides on gauss.ececs.uc.edu

Click 'Choose' to open a JFileChooser. Select an MP3 file (sometimes the applet frame moves behind another window - I do not know why). Click 'Play' to play the tune. The MP3 player is obtained remotely from gauss.ececs.uc.edu using java.lang.reflect classes.

 
6.   Reflect.java
Hello.java
Client.java
Server.java

  -   Send a file to a server where it is turned into a class one method of which is invoked. Start the server then send the Hello class.
 
7.   HammingClient.java
Stream.java
GetStream.java
IntObject.java

Server.java
HammingImpl.java
TimesImpl.java
MergeImpl.java
Monitor.java


  -   A client starts an RMI served method through a GetStream object using its getStream method. Only one call to getStream is made by the client. The arguments to getStream are an array of parameter types and and array of initial values of parameters (namely a list of prime numbers and a multiplier). The GetStream object sends those objects plus a classname (HammingImpl) and a binding name (HammingServer1_3_5_11) to the Server. The Server creates a new object of the HammingImpl class, starts its thread, and serves its methods from RMI. The important method is next. After the first call to next the HammingImpl code, in run, requires instantiating at least one new Hamming stream, one new Times stream and maybe a second new Hamming stream and a new Merge stream through GetStream in a manner similar to that above but with different parameters. Further calls to GetStream result in an increasingly greater network of RMI served methods from the Hamming, Times, and Merge classes. The client never realizes that its repeated calls to a single next causes so many methods to be served.
 
8.   File4.java
test4.fil

  -   Objects are read from a file. Their classes are obtained using the getClass method of the Object class. Next, specific methods of those classes are obtained using the getMethod method of the Class class. Finally, the methods are invoked on the read objects, with appropriate parameters. The test file test4.fil has two objects: the first is a hash table, the second is a vector with the hash table as element 3. The code File4.java finds the second object, invokes the elementAt method to get the hashtable. Then invokes the get method on the hashtable to get the hashtable elements.