Presentation is loading. Please wait.

Presentation is loading. Please wait.

Architecture (simplified view) method object Reflection plane Object plane Class object Method object.

Similar presentations


Presentation on theme: "Architecture (simplified view) method object Reflection plane Object plane Class object Method object."— Presentation transcript:

1 Architecture (simplified view) method object Reflection plane Object plane Class object Method object

2 Architecture

3 Retrieving Class Objects Object.getClass() –From an instance of the object The.class Syntax –No instance available, but type is known Class.forName() –Full name of the class

4 Inspecting a class Methods Fields Constructors Modifiers Generic type parameters Implemented interfaces …

5 LAB - 1 TestReflection (from the site)

6 LAB Calling a remote method in a generic way The client sends to the server –Remote Object ID (integer) –Remote method name (string) –Parameters type (array of Class objects) –Parameter values Server –retrieve remote object from ID –Get a reflecting method class –Receive parameters –Invoke the method

7 Client public class ClientRefl { public static void main (String args[]) { DatagramSocket aSocket = null; try { aSocket = new DatagramSocket(); ByteArrayOutputStream byteStream = new ByteArrayOutputStream(); ObjectOutputStream os = new ObjectOutputStream(byteStream);

8 Client os.writeObject(0); //remote ID os.writeObject("concat"); //method name os.writeObject(new Class[] {String.class}); //types os.writeObject(OK! "); //Parameter os.close(); Client Server !! OK! | String.class | concat | 0

9 Server //Boilerplate code for receiving the message Object [] OT = new Object[1]; OT[0]="OGGETTO REMOTO "; Integer obj_id= (Integer)is.readObject(); //leggo ID oggetto Object obj = OT[(int)obj_id]; Class c = obj.getClass(); //acquisisco classe oggetto... Client Server OK! | String.class | concat | 0

10 Server String methodName = (String)is.readObject(); // concat Class [] parameterTypes = (Class [])is.readObject(); //array di Class List arguments = new ArrayList (); arguments.add(is.readObject()); //or loop according to size of parameterTypes is.close(); Client Server OK! | String.class | concat | 0

11 Server (calling the method) Objcet result = null; Method m; try { //m reflects the concat method… m = c.getMethod(methodName, parameterTypes); result = m.invoke(obj, arguments.toArray()); …

12 LAB: study ClassDeclarationSpy ClassSpy


Download ppt "Architecture (simplified view) method object Reflection plane Object plane Class object Method object."

Similar presentations


Ads by Google