|20-CS-694||Advanced Programming Techniques||Spring 2012|
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 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
|-||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.|
|-||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.|
||-||Run a callback on any method of any class. In this example just a few are illustrated.|
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
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.
|-||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.|
|-||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.|
|-||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.|